Since many students and hobbyists are using RC servo in their projects, let’s take a look at the RC servo (servos for short). The purpose of this information is to give an overview of how servos operate and how to communicate with them.
What is Servos?
Radio Control (RC) hobby servos are small actuators designed for remotely operating model vehicles such as cars, airplanes, and boats. Nowadays, servos are becoming more popular in robotics, creating humanoid robot, biologically inspired robot, robotic arm and etc. This is because its size is small when taken into consideration the built-in circuitry. Moreover, its ability to rotate and maintain at certain position or angle according to control pulses from a single signal wire also make servo motor popular in robot building. Inside a typical servo, it contains a small motor and gearbox to do the work, a potentiometer to measure the position of the output gear, and an electronic circuit that controls the motor to make the output gear move to the desired position. This is the “servo system” which will be elaborated in the next section. A servo has three wires; for power (+5V), ground and the signal wire. Since all these components are packaged in a compact, low-cost unit, servos are great actuators for robots.
Servos are controlled by sending them a pulse of variable width. As long as the signal pulse exists on the signal line, the servo will maintain the angular position of the shaft after it has rotated to that position. As the signal pulse changes, the angular position of the shaft will change. This works because of the servo system which makes the servo motor so famous against other actuator. In practical terms, servo means that you can set and forget, and it will adjusts itself during continued operation through feedback.
A servo motor consists of several main parts; the motor and gearbox, a position sensor, an error amplifier and motor driver and a circuit to decode the requested position. Photo below contains a block diagram of a typical servo motor unit.
The radio control receiver system (or other controller) generates a pulse of varying length approximately every 20 milliseconds. The pulse is normally between 1 and 2 milliseconds long. The length of the pulse is used by the servo to determine the position it should rotate to. The control pulse is fed to a pulse width to voltage converter. This circuit charges a capacitor at a constant rate while the pulse is high. When the pulse goes low the charge on the capacitor is fed to the output via a suitable buffer amplifier. This essentially produces a voltage related to the length of the applied pulse.
The circuit is tuned to produce a useful voltage over a 1ms to 2ms period. The output voltage is buffered and so does not decay significantly between control pulses so the length of time between pulses is not critical. The current rotational position of the servo motor output shaft is read by a sensor. This is normally a potentiometer (variable resistor) which produces a voltage that is related to the absolute angle of the output shaft.
The potentiometer then feeds its current value into the Error Amplifier which compares the current position with the commanded position from the pulse width to a voltage converter.
The error amplifier is an operational amplifier with negative feedback. It will always try to minimize the difference between the inverting (negative) and non-inverting (positive) inputs by driving its output in the correct direction. The output of the error amplifier is either a negative or positive voltage representing the difference between its inputs. The greater the difference, the greater the voltage. The error amplifier output is used to drive the motor. If it is positive the motor will turn in one direction and if it is negative, it will turn to another direction. This allows the error amplifier to reduce the difference between its inputs (thus closing the negative feedback loop) and so makes the servo goes to the commanded position.
Servos are controlled by sending them a pulse of variable width. The signal wire is used to send this pulse. The parameters for this pulse are that it has a minimum pulse, a maximum pulse, and a repetition rate. Given the rotation constraints of the servo, neutral is defined to be the position where the servo has exactly the same amount of potential rotation in the clockwise direction as it does in the anti-clockwise direction. It is important to note that different servos will have different constraints on their rotation but they all have a neutral position, and that position is always around 1.5 milliseconds (ms).
The angle is determined by the duration of a pulse that is applied to the signal wire. This is called Pulse Width Modulation. The servo expects to see a pulse every 20 ms. The length of the pulse will determine how far the motor turns. For example, a 1.5 ms pulse will make the motor turns to a 90 degree position (neutral position).
When these servos are commanded to move, they will move to the position and hold that position. If an external force pushes against the servo while the servo is holding a position, the servo will resist from moving out of that position. The maximum amount of force the servo can exert is the torque rating of the servo. Servos will not hold their position forever though; the position pulse must be repeated to instruct the servo to stay in position.
When a pulse is sent to a servo that is less than 1.5 ms the servo rotates to a position and holds its output shaft some number of degrees anti-clockwise from the neutral point. When the pulse is wider than 1.5 ms the opposite occurs. The minimal width and the maximum width of pulse that will command the servo to turn to a valid position are functions of each servo. Different brands, and even different servos of the same brand, will have different maximum and minimum. Generally the minimum pulse will be about 1 ms wide (some servos iare 0.5ms) and the maximum pulse will be 2 ms wide (some servo are 2.5ms).
Position of RC servo with the signal
Another parameter that varies from servo to servo is the turn rate. This is the time it takes for the servo to change from one position to another. The worst case turning time is when the servo is holding at the minimum rotation and it is commanded to go to maximum rotation. This can take several seconds on very high torque servos.
Controlling a servo motor with Microcontroller
Before writing a program to control the servo, we must first get ready the circuit. As mentioned before, servo has 3 wires; each wire should be connected as shown in the sample schematic below.
Sample circuit to control RC servo
In order to control the servo motor, we must be able to generate a pulse approximately every 20ms although the actual time between pulses is not critical. The pulse width however must be accurate to ensure that we can accurately set the position of the servo. There are a few methods in writing the signal pulse for servo.
This method is the easiest and the simplest way to control the servo. It is suitable for beginners who are just about to learn the servo. The algorithm is easy; first, the output pin of servo is set to high, and then waits for the time you want the servo to rotate, for example, wait for 1.5ms to let the servo be at the neutral position. After 1.5ms, output pin of servo is set to low for 20ms-1.5ms which is 18.5ms so that the total period is 20ms. Repeat the step to get the continuous pulse. After a while, if you intend to rotate the servo to other position for example 180 degree, then you should repeat the step with the delay 2ms (depends on servo spec) after setting the servo pin to high and delay 18ms after the servo pin goes low.
Sample code of controlling RC servo using delay
The continuous signal pulse for servo can actually be regarded as PWM (Pulse Width Modulation) where the frequency is 50Hz and the duty cycle is from 5% to 10% (1ms/20ms to 2ms/20ms). Many microcontrollers are equipped with PWM generators and most people initially consider using these to generate the control signals.Unfortunately they are not vey suitable. The problem is we need a relatively accurate short pulse then a long delay; and generally you only have one or two PWM generator, as a result you only can control one or two servos unless you use switching components outside the microcontroller to share with several servos and these will complicate the hardware. The PWM generator is designed to generate an accurate pulse between 0% and 100% duty cycle, but we need something in the order of 5% to 10% duty cycle (1ms/20ms to 2ms/20ms). If a typical PWM generator is 8 or 10 bits, then we can only use a small fraction of the bits to generate the pulse width we need and so we loose a lot of accuracy and the resolution would become worst.
Another way of controlling servo is by using timer interrupts. This way is more beneficial as you need not waste time waiting for the delay routine. The timer is configured so that you have plenty of accuracy over the 1 to 2 millisecond pulse time. You only need to set the timer to interrupt for the time you want, and then drive the servo pin either to high or low at the suitable interrupt’s time routine. For example, first you drive the servo pin to high and configure the timer to interrupt at the next 1ms. When the interrupt is executed at 1ms, you drive the servo pin to low and configure again for the timer to interrupt at next 19ms in order to get a complete 20ms period which most servos required. Repeating this cycle will generate a continuous pulse for servo. By doing this, you save a lot of time! While waiting, you can do other thing like processing your input. This method makes your project more effective. In order to control more servos, you can run a timer at a faster rate and do a single servo at a time, followed by the next and the next etc. Most of the servo controller projects use this approach