top of page

Interfacing servo motor with RPi

Updated: Apr 5

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.


Hardware used

The list of components used are shown below;


List of Components
List of Components

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.


A SG90 servo motor
A SG90 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.


Jumper Wires
Jumper Wires

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.


Raspberry Pi board
Raspberry Pi board

4. USB Cable

This cable is used to connect the RaspberryPi to the power supply. It also helps in the transmission of data.


USB Cable
USB Cable


Circuit Diagram

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.


Download the codes from here



Working


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.


Hardware unpower
Hardware unpower

When the hardware is powered up it looks like as shown in the image.

Hardware powerup
Hardware powerup

Images of the servo motor for various angles. For 0 Degrees.

0 Degrees
0 Degrees

For 45 Degrees.

45 Degrees
45 Degrees

For 90 Degrees.


90 Degrees
90 Degrees

For 135 Degrees.


135 Degrees
135 Degrees

For 180 Degrees.


180 Degrees
180 Degrees

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.


 

To discover more

335 views2 comments

Related Posts

See All

2 Comments


Anushka Gupta
Anushka Gupta
Jul 27, 2023

📚 This blog post by Learn Electronics India not only teaches you about servo motors and Raspberry Pi interfacing but also keeps you engaged throughout the entire read. I appreciate the effort put into making complex concepts accessible to beginners like me. Keep up the excellent work.

Like

Short and effective! LearnElectronics India never disappoints.

Like
bottom of page