Servomotors (not to be confused with stepper motors, which we will discuss in another section) include a small DC motor, a gearbox, and a control circuit containing a variable resistor that allows the output shaft of the servomotor to be set at a specific angle.
Servomotors are therefore very useful for projects that require very fast and relatively precise movement of a work item.
Servomotors
While continuous servo motors that can rotate continuously can be found, most servo motors (also often referred to as servos or servo motors) can only rotate their output shaft within approximately 180°. They are commonly used in radio-controlled car models to steer the wheels or in radio-controlled aircraft models to control surfaces (ailerons). The figure below shows two servo motors of different sizes.
The servo motor on the right is a so-called standard servo motor. This is the most common type of servo motor. Such servo motors often have identical sizes and mounting hole distances. The much smaller (and lighter) servo motor on the left is intended for aircraft. These servo motors are called 9g servos.
Servos with higher build quality and higher torque have metal gear reducers instead of nylon. Most servo motors operate at a nominal voltage of about 5 V within an allowable voltage range of 4 to 7 V. Connecting hobbyist servo motors is usually done through wires ending in a 3-pin connector: power, ground, and control signal.
Large and sometimes quite powerful servo motors are also available for use, but they are not as standardized as hobbyist low-power servos.
Servo Motor Mechanism
A servo motor consists of a direct current (DC) motor that drives a reducer, which reduces the motor's rotation speed while increasing the torque on the shaft. To control the position of the output shaft, it is connected to a position sensor (typically a variable resistor). To control the power and the direction in which the servo motor turns, the control circuitry uses an input signal from the position sensor in conjunction with a control signal specifying the desired position.
The control unit, upon receiving the desired position value from the control signal, subtracts the actual position value, producing an "error signal" that can be positive or negative. This "error signal" is then fed into the motor's power supply, causing it to change the position of the shaft in the required direction. The greater the difference between the desired and actual positions of the output shaft, the faster the motor will rotate towards the desired position. As the error value (discrepancy) approaches zero, the motor's power supply decreases.
The servo motor on the right is a so-called standard servo motor. This is the most common type of servo motor. Such servo motors often have identical sizes and mounting hole distances. The much smaller (and lighter) servo motor on the left is intended for aircraft. These servo motors are called 9g servos.
Servos with higher build quality and higher torque have metal gear reducers instead of nylon. Most servo motors operate at a nominal voltage of about 5 V within an allowable voltage range of 4 to 7 V. Connecting hobbyist servo motors is usually done through wires ending in a 3-pin connector: power, ground, and control signal.
Large and sometimes quite powerful servo motors are also available for use, but they are not as standardized as hobbyist low-power servos.
Servo Motor Mechanism
A servo motor consists of a direct current (DC) motor that drives a reducer, which reduces the motor's rotation speed while increasing the torque on the shaft. To control the position of the output shaft, it is connected to a position sensor (typically a variable resistor). To control the power and the direction in which the servo motor turns, the control circuitry uses an input signal from the position sensor in conjunction with a control signal specifying the desired position.
The control unit, upon receiving the desired position value from the control signal, subtracts the actual position value, producing an "error signal" that can be positive or negative. This "error signal" is then fed into the motor's power supply, causing it to change the position of the shaft in the required direction. The greater the difference between the desired and actual positions of the output shaft, the faster the motor will rotate towards the desired position. As the error value (discrepancy) approaches zero, the motor's power supply decreases.
Last edited by Oleg on 05 Oct 2023, 07:46, edited 1 time in total.
The control signal for a servo motor is not a voltage, as one might expect, but a pulse-width modulation (PWM) signal. This signal is standard for all hobbyist servo motors and looks as depicted in the figure.
A servo motor expects a control pulse to arrive every 20 ms. A 1.5 ms pulse duration will set the servo motor to its central position, corresponding to a 90° rotation of the output shaft. Shorter pulses at 1.0 ms will set the output shaft to the initial position at 0°, while longer pulses at 2.0 ms will position it at the extreme 180° angle. In reality, this range may be slightly less than the full 180°, with no pulse shortening at one end and pulse lengthening at the other. It's not uncommon for a 0° position to require a 0.5 ms pulse, while 180° might need a 2.5 ms pulse.
A servo motor expects a control pulse to arrive every 20 ms. A 1.5 ms pulse duration will set the servo motor to its central position, corresponding to a 90° rotation of the output shaft. Shorter pulses at 1.0 ms will set the output shaft to the initial position at 0°, while longer pulses at 2.0 ms will position it at the extreme 180° angle. In reality, this range may be slightly less than the full 180°, with no pulse shortening at one end and pulse lengthening at the other. It's not uncommon for a 0° position to require a 0.5 ms pulse, while 180° might need a 2.5 ms pulse.
In this experiment, we will learn how to rotate the output shaft of a servo motor to a specified angle using both Arduino and Raspberry Pi.
For the Arduino board, there is a servo motor control library capable of generating pulses on any of its pins, so you don't have to use a special pin marked as PWM output. The servo motor is controlled through the serial interface monitor window, where you send the angle value to which you want to rotate the motor's shaft.
Generating precise pulse durations on Raspberry Pi is more complex than on Arduino. The Arduino board is equipped with hardware timers that generate the required pulses, while on Raspberry Pi, you have to generate pulses through software. Since Raspberry Pi has an operating system that can run multiple processes competing for processor time, the PWM pulse duration can sometimes be longer than required, causing slight trembling of the servo motor's output shaft. Therefore, in cases where high precision is needed, external PWM devices should be used (see "Project: Dancing Pepe Doll on Raspberry Pi").
Equipment
A remarkable feature of servo motors is that their motor control electronics are enclosed inside the drive housing, eliminating the need for separate H-bridges or transistor motor control blocks. All you need to do is connect the 5 or 6V power supply to the motor power contacts and provide low-level control pulses from the digital output of your Raspberry Pi or Arduino.
The diagram below shows the connection of the servo motor to Raspberry Pi. In this case, one of the servo motor horns is already attached (they come with the servo motor in a small package), allowing you to see the position of the servo motor shaft.
Components
For this experiment, you will need the following components to work with Arduino and Raspberry Pi:
- 9g servo motor
- Male-to-male jumper wires
- Female-to-male jumper wires (only for Raspberry Pi)
A small 9V servo motor should work just fine, powered by 5V directly from the Raspberry Pi or Arduino board. However, if you plan to control a more powerful servo motor, you will need an external power source, such as a 6V battery compartment used in previous experiments.
Arduino Connection
To connect the 3-pin servo motor connector to Arduino, use male-to-male jumper wires.
Servo Motor Connection
In the close-up image below, the servo motor and its connector are shown. As with many other servo motors, such a connector is practically a standard for all servo motor manufacturers. The purpose of the wires connected to the servo motor is determined by their color.
Wires almost always follow this order:
- Brown (sometimes black) - Ground (GND)
- Red - Positive power supply voltage
- Orange (sometimes yellow) - Control signal
Arduino Program
The servo library for Arduino is included with the Arduino IDE and takes care of all the heavy lifting when it comes to working with servo motors.
Here's the Arduino sketch for this experiment:
NOTE
You can, of course, use PWM and the `analogWrite` command to generate a pulse of the required duration for controlling the servo motor. However, this approach would involve changing the PWM frequency and restrict the options for controlling the servo motor to only those outputs on the Arduino board that can operate in PWM mode. This method is described later in the "Raspberry Pi Program" section, but on Arduino, it's simpler and better to use the `Servo` library.
Let's clarify some points in the sketch using the comments provided:
1. After including the library, the `servoPin` constant is defined as the output to which the servo motor control signal will be sent.
2. A variable `servo` of type `Servo` is declared. This variable is used whenever you need to change the position of the servo motor.
3. The outputs to be used for generating pulses are set using `servo.attach`.
4. The initial angle of the servo motor shaft is set to 90° (middle position).
5. Serial communication is started, allowing you to set the servo motor's angle through the serial port monitor.
6. In the main program loop, the sketch waits for the arrival of an angle value for the servo motor. Once received, it is parsed into an integer.
7. A new angle for the servo motor is set using `servo.write`.
Uploading and Running the Program
Upload the sketch to Arduino, and the servo motor's horn on the shaft will immediately take the middle position (90°). Open the serial port monitor window and try entering angle values between 0° and 180°. The servo motor will move to the new position whenever a new angle value is entered.
For the Arduino board, there is a servo motor control library capable of generating pulses on any of its pins, so you don't have to use a special pin marked as PWM output. The servo motor is controlled through the serial interface monitor window, where you send the angle value to which you want to rotate the motor's shaft.
Generating precise pulse durations on Raspberry Pi is more complex than on Arduino. The Arduino board is equipped with hardware timers that generate the required pulses, while on Raspberry Pi, you have to generate pulses through software. Since Raspberry Pi has an operating system that can run multiple processes competing for processor time, the PWM pulse duration can sometimes be longer than required, causing slight trembling of the servo motor's output shaft. Therefore, in cases where high precision is needed, external PWM devices should be used (see "Project: Dancing Pepe Doll on Raspberry Pi").
Equipment
A remarkable feature of servo motors is that their motor control electronics are enclosed inside the drive housing, eliminating the need for separate H-bridges or transistor motor control blocks. All you need to do is connect the 5 or 6V power supply to the motor power contacts and provide low-level control pulses from the digital output of your Raspberry Pi or Arduino.
The diagram below shows the connection of the servo motor to Raspberry Pi. In this case, one of the servo motor horns is already attached (they come with the servo motor in a small package), allowing you to see the position of the servo motor shaft.
Components
For this experiment, you will need the following components to work with Arduino and Raspberry Pi:
- 9g servo motor
- Male-to-male jumper wires
- Female-to-male jumper wires (only for Raspberry Pi)
A small 9V servo motor should work just fine, powered by 5V directly from the Raspberry Pi or Arduino board. However, if you plan to control a more powerful servo motor, you will need an external power source, such as a 6V battery compartment used in previous experiments.
Arduino Connection
To connect the 3-pin servo motor connector to Arduino, use male-to-male jumper wires.
Servo Motor Connection
In the close-up image below, the servo motor and its connector are shown. As with many other servo motors, such a connector is practically a standard for all servo motor manufacturers. The purpose of the wires connected to the servo motor is determined by their color.
Wires almost always follow this order:
- Brown (sometimes black) - Ground (GND)
- Red - Positive power supply voltage
- Orange (sometimes yellow) - Control signal
Arduino Program
The servo library for Arduino is included with the Arduino IDE and takes care of all the heavy lifting when it comes to working with servo motors.
Here's the Arduino sketch for this experiment:
Code: Select all
#include <Servo.h>
const int servoPin = 9; // (1)
Servo servo; // (2)
void setup() {
servo.attach(servoPin); // (3)
servo.write(90);
Serial.begin(9600); // (4)
Serial.println("Enter angle in degrees");
}
void loop() { // (5)
if (Serial.available()) {
int angle = Serial.parseInt();
servo.write(angle); // (6)
}
}
NOTE
You can, of course, use PWM and the `analogWrite` command to generate a pulse of the required duration for controlling the servo motor. However, this approach would involve changing the PWM frequency and restrict the options for controlling the servo motor to only those outputs on the Arduino board that can operate in PWM mode. This method is described later in the "Raspberry Pi Program" section, but on Arduino, it's simpler and better to use the `Servo` library.
Let's clarify some points in the sketch using the comments provided:
1. After including the library, the `servoPin` constant is defined as the output to which the servo motor control signal will be sent.
2. A variable `servo` of type `Servo` is declared. This variable is used whenever you need to change the position of the servo motor.
3. The outputs to be used for generating pulses are set using `servo.attach`.
4. The initial angle of the servo motor shaft is set to 90° (middle position).
5. Serial communication is started, allowing you to set the servo motor's angle through the serial port monitor.
6. In the main program loop, the sketch waits for the arrival of an angle value for the servo motor. Once received, it is parsed into an integer.
7. A new angle for the servo motor is set using `servo.write`.
Uploading and Running the Program
Upload the sketch to Arduino, and the servo motor's horn on the shaft will immediately take the middle position (90°). Open the serial port monitor window and try entering angle values between 0° and 180°. The servo motor will move to the new position whenever a new angle value is entered.