Raspberry Pi Pico - Servo Motor

This guide shows you how to operate a servo motor with a Raspberry Pi Pico. We will cover:

Raspberry Pi Pico Servo Motor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Servo Motor
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Servo Motor

A servo motor is a part that can rotate its handle, typically from 0 to 180 degrees. It is used to adjust the position of an object.

Pinout

This example shows how to use a servo motor that has three pins:

  • VCC pin: Attach the red wire to VCC (5 volts).
  • GND pin: Connect the black or brown wire to GND (0 volts).
  • Signal pin: Connect the yellow or orange wire to get the PWM control signal from a Raspberry Pi Pico pin.
Servo Motor Pinout

Wiring Diagram

Online diagrams might show a connection between the VCC pin of a servo motor and the VBUS pin of the Raspberry Pi Pico board. Avoid doing this as it can damage the Raspberry Pi Pico board.

The wiring diagram between Raspberry Pi and Pico Servo

This image is created using Fritzing. Click to enlarge image

To keep your Raspberry Pi Pico board safe, use a separate power supply for the servo motor. The diagram below explains how to connect the servo motor to this power source.

The wiring diagram between Raspberry Pi and Pico Servo Motor

This image is created using Fritzing. Click to enlarge image

Ensure that you connect the GND (ground) from the external power supply to the GND of the Raspberry Pi Pico board. This is crucial for it to function properly.

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-servo-motor """ from DIYables_MicroPython_Servo import Servo import utime # Create a Servo object servo = Servo(28) # The Raspberry Pi Pico pin GP28 connected to the servo motor while True: # Move servo to different angles servo.move_to_angle(0) utime.sleep(2) servo.move_to_angle(90) utime.sleep(2) servo.move_to_angle(180) utime.sleep(2)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the servo motor according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-Servo”, then find the Servo Motor library created by DIYables.
  • Click on DIYables-MicroPython-Servo, then click Install button to install Servo Motor library.
Raspberry Pi Pico Servo Motor library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Observe the action: The servo motor gradually rotates from 0 to 180 degrees and then gradually rotates back from 180 to 0 degrees.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Code Explanation

You can find the explanation in the comments section of the above Raspberry Pi Pico code.

How to Control Speed of Servo Motor

The below MicroPython script controls a servo on the Raspberry Pi Pico to smoothly transition between angles non-blockingly. It uses utime for timing, updating the servo's position periodically without pausing the program. This approach keeps the main loop responsive to other operations.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-servo-motor """ from DIYables_MicroPython_Servo import Servo import utime # Create a Servo object servo = Servo(28) # The Raspberry Pi Pico pin GP28 connected to the servo motor # Define the start and stop angles and the duration for the movement start_angle = 30 stop_angle = 90 moving_time = 3000 # Duration in milliseconds # Function to map the progress to an angle def map_value(x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min # Initial setup with timer move_start_time = utime.ticks_ms() # Main loop while True: progress = utime.ticks_diff(utime.ticks_ms(), move_start_time) # Calculate elapsed time since start if progress <= moving_time: # Calculate intermediate servo angle angle = map_value(progress, 0, moving_time, start_angle, stop_angle) servo.move_to_angle(angle) # Set servo position to calculated angle else: # Optionally, restart the motion or break the loop move_start_time = utime.ticks_ms() # Reset the timer to start the motion again

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!