Drive Servo Motor with Raspberry Pi


Drive Servo Motor with Raspberry Pi

In this project, we will use Raspberry Pi to send command through GPIO to Servo Motor and control its rotation action.

If you don’t know what is GPIO layout, check our tutorial How to read Raspberry Pi i/o pin diagram (GPIO pin graph)


1 pc Raspberry Pi 2/3/zero raspi
1 pc 8GB MicroSD memory card preinstalled Raspbian OS. 8gtf
1 pc Servo Motor SG90
1 pc breadboard BREADBOARD
1 pc GPIO breakout kit(optional) GPIO to Breadboard 40-pin breakout interface


1)Raspbian should be upgraded to latest version in order to support RPI.GPIO module
Please run following commands in shell:

  • sudo apt-get update
  • sudo apt-get upgrade

2)Enable I2C and SPI protocol
To enable the protocol, run shell command

sudo raspi-config

Then select Advance Options and enable I2C and SPI

You need to reboot to effect the configuration.

Circuit Graph:

Caution: Unlike Arduino board 5V input voltage, Raspberry GPIO pin accept only 3 Volt. Wrong voltage input might damage the Pi board. Please be very careful!

download python code
Please typing following shell commands to download python files(
wget –no-check-certificate

Finally, run following command in shell window:

sudo python

You will see following manual in terminal window:
l = move to the left
r = move to the right
m = move to the middle
t = test sequence
q = stop and exit

You can type above 5 options(l,r,m,t,q) and control the servo motion accordingly.

Video Demo:

About the Author

admin administrator

You must be logged in to post a comment