Arduino RC Servo

An RC servo is a motor/gearbox assembly which uses a pulse-width modulated input signal to set the desired position, and feedback to determine the position of the shaft. The difference between these two is the error. A motor turns the output shaft until there is no error. The feedback is typically a potentiometer attached to the output shaft.

Developed for use in radio controlled model airplanes, they may be found in applications as diverse as telescope drives, satellite receiver feed horns, and robots.

For this tutorial we are going to hook up a servo to a Freaduino Uno and cycle it back and forth using a potentiometer as an input device. I chose the Freaduino because it has servo connectors built in. Others do as well, but I had the Freaduino. If you don't have servo connectors you will need to use a breadboard to build the circuit, since there is only one 5V output on the Arduino Uno, and you need one for the pot and one for the servo. You could also use one of the Servo/Sensor shields available from many sources.


#include <Servo.h> 

#define POT_PIN A0
#define SERVO_PIN 2

Servo servo;

int angle = 0;

void setup() {
  servo.attach(SERVO_PIN);
}

void loop() {
  angle = analogRead(POT_PIN)/(1024/180);
  servo.write(angle);
  delay(15);
}
	

The video shows a pot connected to A0 and the servo connected to D2. As the pot is turned, the servo follows it, at least for the 180° range of the servo driver.

What's the point of this circuit? Two things: To show that a servo follows an input by applying feedback to an otherwise uncontrolled motor, and to demonstrate that a servo can amplify force. I turn the pot a using a little force, and the servo turns at the same speed, but with much higher torque - it is not possible for me to block the motion of the servo with my hand.

Servos of a similar type (but quite a bit larger) kept the big guns of a battleship pointing at the target even though the ship was rolling with the waves. They used a gyroscope as input and the gun position as feedback. The gyroscope would remain level as the ship would roll, generating an error voltage that would drive motors to point the guns up or down to compensate.

	
#include <Servo.h> 
 
Servo servo;

int angle = 0;
int delayTime;

void setup() {
  servo.attach(2);	// Servo is on pin 2.
}

void loop() {
  if (++angle > 180)
  {
    angle = 0;		// Send it all the way back.
    delayTime = 750;	// Give it time to get there.
  } else {
    delayTime = 15;
  }
  servo.write(angle);	// Tell the servo to go.
  delay(delayTime);	// Wait for it to move.
  
}
	

The video shows a servo connected to D2. The program ramps the position to 180°, then resets it to 0° in a loop. The time delay is different for the two moves because it takes more time for the servo to travel 180° than to travel 1°.

This exercise also has a point. The servo could be used to position solar panels throughout the day so that the sunlight is maximized on the panels. The Arduino would position the panels based on the time of day. A more impressive solar tracker might use photoresistors to track the sun, and the servo to position the panels.

Another use might be to perform some mechanical action, such as open a vent in the greenhouse, when the temperature gets too high.

If you don't have access to a Freaduino Uno or a sensor shield, you can use an Arduino Uno by wiring the pot and servo as shown in the schematic diagram. The only reason you might have problems is the requirement for two 5V connections, which can be made on a breadboard for simplicity. Everything else can be wired up directly.

Servos are motors, and so require a bit of power. Running this MG996R servo off of the 5V output of an Arduino is really going to require an external power supply, because the USB can't deliver quite enough current for the motor. If the servo is not powered properly it will growl quite a bit more than if it is sufficiently powered.

Arduino Board Logo

 

Arduino-Board is the go-to source for information on many available Arduino and Arduino-like boards, tutorials and projects.

Help and Support

Arduino-Board

Stay updated

Sign up if you would like to receive our once monthly newsletter.