Interfacing servo motor with RPi
Updated: Dec 1, 2020
The current era is the period of technology that would make things easy and simple. This article of interfacing a servo motor with RPi is also one such example. A servomotor can drive or revolve an object. That is if one wants to rotate an object at some specific angles or distance, then the servo motor comes in use. A servo motor is basically a combination of a DC motor, position control system, and gears.
Servo motors find applications in toy cars, RC helicopters, and planes, Robotics, Machine, etc. The article covers the working and configuration of servo motor with #RPi in detail along with codes.
The list of components used are shown below;
1. Servo motor
A servo motor is actually a rotary actuator which gives a precise control in terms of its angular position, acceleration, and velocity. A servo motor works on the principle of PWM (Pulse width modulation). This implies that depending on the width of the pulse that is applied the rotor will turn in the desire position. For this interfacing, an SG90 servo motor is used. The white piece in the picture is a rotor and is controlled by a bunch of gears inside. If a DC source powers the motor then it is called a DC servo motor and if powered by an AC source then it is called an AC servo motor.
An #SG90 servo motor is specifically a 180-degree servo motor. To vary the specific voltages from the signal output to the motor the concept of the #duty cycle is used. Based on the duty cycle the position of the servo motor is mapped. How the mapping is done will see in the code section.
The position to duty ratio is as given below. All these are calculations based on the frequency of the servo motor. The frequency of the servo motor is #50Hz. Every 5 unit change in the duty ratio would cause a 90-degree change in the motor's rotor.
2. Jumper wires:- Jumper wires are used to establish a connection between different elements in the circuit and #Raspberrypi board. There are two types of wire, male and female. The male wires have the ends that can be connected to other ends and the female wires are the ones that have the receiving end.
3. Raspberry Pi 3 Model B+:
We are using Raspberrypi for the Interfacing of a servo motor with the RPi module and display the output. The Raspberry Pi is a series of small single-board computers. Here 3 Model B+ indicates the generation of the RPi board. The Raspberry Pi is also written as RPI or RPi or Raspberry Pie. The Raspberry Pi Model B+ is of the nearby size of a credit card. It contains several I/O pins using which we can connect the module and then by coding the Raspberrypi, we will receive the commands/signals that are sent to the servo motor.
4. USB Cable
This cable is used to connect the RaspberryPi to the power supply. It also helps in the transmission of data.
In general, a servo motor comes with red, brown, and orange wire. The 5v pin is connected to the red wire. The ground to the brown wire and the #PWM to the port pin 12 or the GPi 18 pin on the Raspberrypi is connected to the orange wire on the module.
Once this connection is done signals are sent to the board Pin 12, these signals will be pulse width modulated. This means there will be variable voltages coming on the signal which in turn will cause the motor to rotate in different directions.
import RPi.GPIO as GPIO
The RPi.GPIO is initialized for setting the modes and giving them the declaration as input mode or output mode.
Next, the time module is initialized, to provide a delay in the function.
The mode is set for the GPi pin.
The warning is set as false.
The GPIO pin 12 is set as output.
p = GPIO.PWM(12, 50)
The PWM function is initialized for the pin 12, which sends the PWM signal to the pin 12 with 50Hz frequency.
The servo motor is initialized with a specific angle of 2.5.
try: while True:
The next is the try loop, with a while loop inside it that contains the program for the rotation of the rotor at specific angles.
p.ChangeDutyCycle(2.5) # turn towards 0 degree time.sleep(1) # sleep 1 second
We start rotating the motor and turn towards 0 degrees is the first command. For this notice, the duty cycle is at 2.5.
p.ChangeDutyCycle(5) # turn towards 45 degrees time.sleep(1) # sleep 1 second
Next, the duty cycle is changed to 5 to rotate the motor form 0 to 45 degrees.
p.ChangeDutyCycle(7.5) # turn towards 90 degrees time.sleep(1) # sleep 1 second
From 45 degrees to 90 degrees and so on up to 180 degrees.
After the motor has reached 180 degrees, the program traceback, that is 135 then 90 then 45, and lasts back to 0 degrees. In this way, a servo motor is interfaced using a Raspberrypi. After every rotation of 45 degrees, a delay of 1 sec is initiated. This is done to visualize the motion in a better way.
With this command, the signal that is applied is commanded to stop.
Lastly, the GPIO pin is cleaned to remove any garbage value.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(12, GPIO.OUT) p = GPIO.PWM(12, 50) p.start(2.5) try: while True: p.ChangeDutyCycle(2.5) # turn towards 0 degree time.sleep(1) # sleep 1 second p.ChangeDutyCycle(5) # turn towards 45 degrees time.sleep(1) # sleep 1 second p.ChangeDutyCycle(7.5) # turn towards 90 degrees time.sleep(1) # sleep 1 second p.ChangeDutyCycle(10) # turn towards 135 degrees time.sleep(1) # sleep 1 second p.ChangeDutyCycle(12.5) # turn towards 180 degrees time.sleep(1) # sleep 1 second p.ChangeDutyCycle(10) # turn towards 135 degrees time.sleep(1) # sleep 1 second p.ChangeDutyCycle(7.5) # turn towards 90 degrees time.sleep(1) # sleep 1 second p.ChangeDutyCycle(5) # turn towards 45 degrees time.sleep(1) # sleep 1 second p.ChangeDutyCycle(2.5) # turn towards 0 degrees time.sleep(1) # sleep 1 second except KeyboardInterrupt: p.stop() GPIO.cleanup()
Download the codes from here
The working is quite simple, as soon as the codes are applied the #rotor of the servo motor starts to move from 0 degrees then 45 degrees, and so on up to 180 degrees with a pause of 1 sec at every 45 degrees change. Once the rotor has reached 180 degrees the rotation is revered that is 180 degrees then 135 degrees, then 90 degrees, and so on up to 0 degrees. The image below shows the hardware connection without codes applied.
When the hardware is powered up it looks like as shown in the image.
Images of the servo motor for various angles. For 0 Degrees.
For 45 Degrees.
For 90 Degrees.
For 135 Degrees.
For 180 Degrees.
Video explanation for a better understanding.
The article covers the basic interfacing of servo #motor with Raspberrypi. With this essential concept, one can build may other projects as servo motor finds numerous applications in home electronics, toys, robotics, airplane, etc.