Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.

SG90 Servo Motor with Raspberry PI Pico and MicroPython

raspberry pi pico sg90 servo motor featured image
5
(1)

Servo Motors are common electronics and diy components able to perform rotations (usually in a range between 0 and 180 degrees). You can use SG90 with Raspberry Pi Pico with easy MicroPython code as only PWM is required

In this tutorial, I’m going to show you how to use an SG90 servo motor with Raspberry PI pico and Micropython.

Elegoo SG90 Servo Motor

Servo motor is a compact kind of DC motor equipped also with a set of reduction gears. Usually, it can rotate only in a range between 0 and 180 degrees with its “horn”, even if this accessory can be replaced with the 2 additional inserts available from its standard packaging.

They commonly come with a 3-wire cabling, representing following:

  • the orange one for data
  • the red one for Vcc
  • the brown one for ground

Please note that servo motors require a 5V Vcc power, which is available from Raspberry PI Pico only when connected to USB (taking this voltage from VBUS, according to Raspberry Pi Pico pinout) or with a proper external power supply. We’ll use a 50Hz PWM signal. Its duty cycle will result in driving the servo motor to a specific position: the “high” time of our signal corresponds to the angle performed:

Time for high level SG90 angle
0.5 ms0 degree
1 ms45 degree
1.5 ms90 degree
2 ms135 degree
2.5 ms180 degree

Finally, consider that SG90 servo motors are power-hungry devices, so consider this when dimensioning power supply packages in your battery-powered projects.

What We Need

As usual, I suggest adding from now to your favourite e-commerce shopping cart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from the shopping cart. So, hardware will be only:

raspberry pi pico microcontroller

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon Raspberry Pi Pico box
amazon sg90 servo motor box
Amazon Breadboard box
Amazon Dupont Wiring box

Wiring Diagram

Please find below my wiring diagram schema:

raspberry pi pico sg90 servo motor wiring

Following pictures include some cabling details:

raspberry pi pico sg90 servo motor overall
raspberry pi pico sg90 servo motor pico cabling
raspberry pi pico sg90 servo motor details

Step-by-Step Procedure

Connect RPI Pico to Thonny (you can refer to my tutorial about First steps with Raspberry PI Pico). Download my picoSg90servo.py script on your computer and open it with Thonny.

Following paragraphs will describe my code line by line.

As usual, we start importing required libraries at the first lines of our script:

from machine import Pin, PWM
from time import sleep

ServoPIN variable links a PWM instance on Pin 16 (as with our wiring). Its frequency is set to 50Hz:

servoPin = PWM(Pin(16))
servoPin.freq(50)

Then we define our main function. The servo function, which requires as input the position angle to reach (in degrees), checks that input ranges between 0 and 180. We’ll cut outer values to stay in this range:

def servo(degrees):
    if degrees > 180: degrees=180
    if degrees < 0: degrees=0

Even if the duty cycle for Raspberry PI Pico PWM can be set in a range between 0 and 65536 (as it results from a 16-bits logical level), the servo motor performs all the available angles between 1000 and 9000. maxDuty and minDuty make this explicit:

    maxDuty=9000
    minDuty=1000

Degrees value (0-180) is proportioned to 1000-9000 range with following formula. Please note that the resulting newDuty is a floating value:

    newDuty=minDuty+(maxDuty-minDuty)*(degrees/180)

Finally, the new duty value is set to our PWM pin with the duty_u16 function. As it requires an integer input, the int() function will convert from floating to integer:

    servoPin.duty_u16(int(newDuty))

With this, our servo function is complete. This script also includes a while loop testing our servo. This while loop has 2 nested loops with for statement. The first “for” statement tests the servo motor with an increasing degree value. It sets the SG90 position, waits a few time (to avoid this movement from being too fast) and then prints on Thonny shell current cycle (increasing) and angle:

while True:
  for degree in range(0,180,1):
    servo(degree)
    sleep(0.001)
    print("increasing -- "+str(degree))

Second loop performs same movement, in reverse direction:

  for degree in range(180, 0, -1):
    servo(degree)
    sleep(0.001)
    print("decreasing -- "+str(degree))

Running the picoSg90servo Script

Run this script in your Thonny IDE (F5) and you will start seeing your servo moving from one side to the other (and vice versa) while Thonny shell will print logging info:

MicroPython v1.15 on 2021-04-18; Raspberry Pi Pico with RP2040

Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT
increasing -- 0
increasing -- 1
increasing -- 2
increasing -- 3
increasing -- 4
increasing -- 5
increasing -- 6
increasing -- 7
increasing -- 8
....
....
increasing -- 177
increasing -- 178
increasing -- 179
decreasing -- 180
decreasing -- 179
decreasing -- 178
decreasing -- 177
....

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy