In the intro to servos post, I describe how to setup, center and point a 180-degree, positional servo. In this post we are going to extend that knowledge and explore how to use continuous rotation, 360-degree servos.
Both servos accept the same signal from Arduino, but they behave in very different ways. These differences arise from the servo hardware itself. Positional servos have physical stops that prevent movement beyond 180 degrees and accept signals as position. Continuous servos rotate 360 degrees and accept signals as indicators of speed and direction.
Required Parts
- positional (180-degree) servo (check label — ends with 9g)
- continuous (360-degree) servo (check label — it says 360 at end)
- arduino
- breadboard
- wire ( dupont or hookup)
The Circuit
Here is a circuit diagram and simulation of the work we are about to do physically.
https://www.tinkercad.com/things/bCym5XkRIqY-servos-180-and-360
Video
Code
Upload the code once with line 24 active.
pos = 90; // center servos -- do this once then comment out
The line sets the servo pos to 90 for each loop. This will stop the continuous servo and center the positional servo. You can now attach the servos horns.
Now, comment out line 24, allowing the increment to change position.
// continuous and postional servos
#include <Servo.h>
Servo positional_180;
Servo continuous_360;
int pos = 90;
int increment = 1;
void setup()
{
positional_180.attach(3);
continuous_360.attach(4);
Serial.begin(9600);
}
void loop()
{
// movement from 90 to 180
pos = 90; // center servos -- do this once then comment out
positional_180.write(pos);
continuous_360.write(pos);
delay(50); // it takes time to physically move
pos+=increment;
// use the following one at a time
// 90 to 0 (right side, clockwise)
if ((pos <= 0) || (pos>=91)) {increment *= -1; delay(1000); }
// 90 to 180 left side, counter clockwise
//if ((pos >= 180) || (pos<=90)) {increment *= -1; delay(1000); }
// full swing 0-180, start at 90
//if ((pos <= 0) || (pos>=180)) {increment *= -1; delay(1000); }
Serial.println(pos);
}