element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products & Partners
    Products & Partners
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products & Partners
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Raspberry Pi Pico Control a Servo Motor with MicroPython (Beginner's Guide)
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mearm.ben
  • Date Created: 11 Nov 2026 1:41 PM Date Created
  • Views 69 views
  • Likes 3 likes
  • Comments 2 comments
  • robotics
  • raspberry pi pico
  • servo
  • raspberry pi
  • servo motors
  • raspberry_pi_projects
Related
Recommended

Raspberry Pi Pico Control a Servo Motor with MicroPython (Beginner's Guide)

mearm.ben
mearm.ben
11 Nov 2026

image

Arguably one of the most fun things you can do with a Raspberry Pi Pico is to control a servo motor. If you're planning on taking over the world, I'd suggest this is a good place to start. 

In this short tutorial we'll cover the electrical connections, software, and commands that you need to get a hobby servo motor working with MicroPython. 

  • In this beginner-friendly tutorial, you'll learn how to:
  • Connect up a hobby servo motor
  • Control the servo using MicroPython
  • Lay the foundations for controlling a robot from your Raspberry Pi

What You'll Need

  • Raspberry Pi Pico
  • USB cable
  • PC or Mac
  • Hobby Servo Motor
  • Connecting Cables
  • External Power Supply (optional unless you want to lift things)

On the software side we'll use Thonny, which is one of the easiest ways to program a Raspberry Pi Pico with MicroPython. If you've already used a Pico before, you're probably ready to skip ahead to the code.

Otherwise, install Thonny, connect your Pico, and we'll get everything set up before writing our servo control program.

Once your software is installed we can do the first interesting part. Installing the servo library. 

A library is a set of code that does a bunch of stuff behind the scenes for you. In this case it sets up the PWM duty cycle, timers, and converts pulses to angles for you. What does all that mean you ask? Well because it's a library it means you don't need to know for now, we just want to get on with the good stuff. Sure we can look into all of that at a later date. 

For now, in Thonny Click on Tools > Manage Packages... and type micropython-servo into the search box. Click enter and select micropython-servo from the list and click install.

image

Congratulations, you've installed a library! The details of what this basic library can do, along with a short example is available here.

Now we're going to paste in this code

import time
from servo import Servo
my_servo = Servo(pin_id=15)
my_servo.write(30)
time.sleep(2.0)
my_servo.write(60)
time.sleep(2.0)
my_servo.write(90)

What this code does is

  1. Sets up the bits of software it will need (time and servo)
  2. Tells the pico there is a servo on pin 15
  3. Moves the servo to a 30 degree position
  4. Waits 2 seconds
  5. Moves the servo to a 60 degree position
  6. Waits 2 seconds
  7. Moves the servo to a 90 degree position
  8. that's it

Before we run this code however we need to wire our servo up. Fortunately this too is pretty simple. 

image

Since we're only watching the servo motor turn and it's not carrying any weight we can wire it to our power from the Raspberry Pi Pico. Here we're connecting the power, which is normally red to the VSYS on the pico, this takes power from the USB supply. The black (or sometimes brown) wire goes to ground, and the yellow or orange wire we call our signal wire and that is going to Pin15. 

PLEASE NOTE: USB can't supply lots of power, so if you want to use your servo to lift something you'll need an external power supply, like a 4xAA battery pack. You can connect this up as follows:

image

Here the power goes to the battery and the ground is connected to both the battery and the Raspberry Pi Pico. We call this having a "common ground". 

Now that we have connected up the Pico and the servo we can upload the code.

Our servo should spring to life and move through the three positions. 

Now we've got it moving, but ideally we don't want to have to change the code every time we want to move our servo motor. Wouldn't it be better if we could just send a signal somehow? I have another tutorial that'll get round to publishing on here soon! But for now if we hash two bits of code together we can get a USB Serial controlled servo motor...


import sys, select, time
from machine import Pin
from servo import Servo

#say something
print("PICO READY to move your servo")
print("what angle should I move to?")

#set up our input method 
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
buf = ""

#set up the LED

led = Pin(25, Pin.OUT)

#set up the servo
my_servo = Servo(pin_id=15)

while True:
    
    if poll.poll(0):
        ch = sys.stdin.read(1)
        if ch:
            if ch == "\n":
                line = buf.strip()
                buf = ""
                if not line:
                    continue

                try:
                    print("You said: ",line," watch me move!")
                    my_servo.write(int(line))
                    led.toggle()
                except Exception as e:
                    print("ERROR", e, ". You Wrote: ", line, "which means you didn't give me a whole number")
            else:
                buf += ch
time.sleep(0.005)

Now we can throw values into the serial monitor and watch the servo motor move to position. I've taken a short video of this. 

So what next? Obviously we'll want to control more servo motors, but we could also control them more easily. Perhaps we could make a little graphical user interface (GUI) with a slider that could send the data over serial for us? Watch out for the next in the series. 

In this tutorial you've learned how to control a servo motor with MicroPython, you've also learned how to hook up servos, and how to control one over a serial connection. These are hugely useful tools for people planning to take over the world using robots.

 

  • Sign in to reply
  • mearm.ben
    mearm.ben 23 hours ago in reply to cstanton

    There's a lot of thinking between making one servo move and a good application for that challenge! The dev board looks complex. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • cstanton
    cstanton 23 hours ago

    What benefit might you get from using the kit that's part of the Rise of the Robots design challenge?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube