Continuous Rotation Servo + Arduino UNO: a Tutorial
by AustinS89 in Circuits > Arduino
35176 Views, 5 Favorites, 0 Comments
Continuous Rotation Servo + Arduino UNO: a Tutorial
I'm going to show you how to get started using a continuous rotation servo motor with the Arduino UNO. A continuous rotation servo can spin continuously either in a forward or reverse direction, at a speed that you can specify. These servos are especially useful for projects such as a robotic car.
Before getting started, I've attached a video that shows the circuit working, and also gives a high level walk through of the steps I'm going to discuss here. I'd also like to mention that the circuit diagram and code in this tutorial were built using Cirkit Designer. I've uploaded the Cirkit Designer circuit project here so you can download and use the circuit design yourself.
Video Demo
Here's a video demo of the working circuit, as well as a high level summary of the steps discussed here. Feel free to skip over or come back to the video after reading the remaining steps.
Parts
For this tutorial, we'll need the following parts:
- Continuous Rotation Servo Motor: Digi-Key link
- 100 nF ceramic capacitor: Digi-Key link
- 1 uF electrolytic capacitor: Digi-Key link
- 5V LDO voltage regulator: Digi-Key link
- DC Power Adapter: Digi-key link
- 12V Wall Power Adapter: Digi-key link
- Arduino UNO: Digi-key link
Wiring
We can see how to connect the Arduino to the continuous rotation servo in the circuit diagram generated with Cirkit Designer, which is a great tool for circuit design and diagramming.
Also make sure to connect your Arduino UNO to your computer via USB.
I'd also like to take a moment to explain why we're powering the servo from a separate power supply, rather than directly from the 5V line on the Arduino.
The Arduino's 5V line can supply up to ~450mA, which means that whenever we're building a circuit that could use anywhere near that level of current, we should power it separately from the Arduino's 5V line. In this case, we're using a voltage regulator, which takes 12V as input from the wall adapter, and outputs 5V and up to 1.5A of current.
Code
Now it's time to write some code for the Arduino!
The following sketch will spin the servo forward for 1 second, pause for 1 second, spin in reverse for 1 second, and pause for 1 second on repeat.
- For this step, you'll need to have the Arduino IDE installed.
- Next, copy and paste the following code into a new Sketch file inside the Arduino IDE.
- Select Upload to compile and upload this sketch to the Arduino UNO.
- You should now see the servo spinning.
/** * This example demonstrates how to control a continuous rotation servo motor. * In this example, the servo spins forward for 1 second, pauses for 1 second, * spins backwards for 1 second, and pauses for 1 second in a loop. * * This example was written by Cirkit Design LLC. */ #include Servo myservo; int pos = 0; void setup() { // The servo control wire is connected to Arduino D2 pin. myservo.attach(2); // Servo is stationary. myservo.write(90); } void loop() { // Servo spins forward at full speed for 1 second. myservo.write(180); delay(1000); // Servo is stationary for 1 second. myservo.write(90); delay(1000); // Servo spins in reverse at full speed for 1 second. myservo.write(0); delay(1000); // Servo is stationary for 1 second. myservo.write(90); delay(1000); }