Control a Servo Motor with Raspberry Pi Pico W using PWM
Master precision motor control with MicroPython. Build robotic arms, automation systems, and smart devices — simulated in Wokwi, no hardware needed.
Learn how to control a servo motor with Raspberry Pi Pico W using PWM (Pulse Width Modulation) in this complete beginner-friendly tutorial. Using the Wokwi simulator, you can experiment with robotics and automation without any physical hardware.
This project is an essential foundation for building robotic arms, smart devices, CNC systems, and IoT automation systems using MicroPython.
What is a Servo Motor?
A servo motor is a precision rotary actuator that allows accurate control of angular position — typically between 0° and 180°. Unlike standard DC motors, a servo can move to a specific angle and hold that position steadily.
Servo motors are widely used in:
- Robotic arms & grippers
- Steering systems in RC vehicles
- Camera gimbals & pan-tilt mounts
- Smart automation & door locks
- DIY robotics & STEM projects
Types of Servo Motors
Hardware Used in This Tutorial
Create a New Project in Wokwi
- Go to wokwi.com and sign in
- Click New Project
- Select Raspberry Pi Pico or Raspberry Pi Pico W
- Add a Servo Motor component from the parts panel
Servo Motor Pin Configuration
A standard servo motor has 3 wires:
| Wire Color | Function | Connect To |
|---|---|---|
| Brown / Black | Ground (GND) | GND on Pico |
| Red | Power (VCC) | VBUS / 3V3 on Pico |
| Orange / Yellow / White | Signal (PWM) | GP15 on Pico |
Wire the Circuit — diagram.json
Servo Connections Summary:
- GND → GND on Pico pico:GND.8
- VCC → 3V3 on Pico pico:3V3
- Signal → GP15 on Pico pico:GP15
📄 diagram.json — Copy & Paste into Wokwi
{
"version": 1,
"author": "MakeMindz",
"editor": "wokwi",
"parts": [
{
"type": "board-pi-pico-w",
"id": "pico",
"top": -22.45,
"left": -102.05,
"attrs": { "env": "micropython-20241129-v1.24.1" }
},
{
"type": "wokwi-servo",
"id": "servo1",
"top": -30.8,
"left": 48,
"attrs": {}
}
],
"connections": [
[ "servo1:GND", "pico:GND.8", "black", [ "h0" ] ],
[ "pico:3V3", "servo1:V+", "red", [ "h0" ] ],
[ "pico:GP15", "servo1:PWM", "orange", [ "h0" ] ]
],
"dependencies": {}
}
Understanding Servo Control with PWM
Servo motors operate using PWM (Pulse Width Modulation). The key parameters are:
- Frequency: 50 Hz (one pulse every 20ms)
- Pulse Width determines the servo angle
| Pulse Width (ns) | Pulse Width (ms) | Servo Angle |
|---|---|---|
| 500 000 | 0.5 ms | 0° |
| 1 500 000 | 1.5 ms | 90° (centre) |
| 2 500 000 | 2.5 ms | 180° |
Write the MicroPython Code
This script uses PWM on GP15 at 50 Hz to sweep the servo through 0°, 90°, 180°, and back to 90° — repeating continuously.
from machine import Pin, PWM import time # Set up PWM on GP15 at 50Hz (required for servo) servo = PWM(Pin(15)) servo.freq(50) while True: # Move to 0° — pulse width: 500,000 ns (0.5ms) servo.duty_ns(500000) time.sleep_ms(500) # Move to 90° (centre) — pulse width: 1,500,000 ns (1.5ms) servo.duty_ns(1500000) time.sleep_ms(500) # Move to 180° — pulse width: 2,500,000 ns (2.5ms) servo.duty_ns(2500000) time.sleep_ms(500) # Return to 90° (centre) servo.duty_ns(1500000) time.sleep_ms(500)
What This Code Does
- Sets PWM frequency to 50 Hz — the standard required for servo motors
- Uses duty_ns() to set pulse width in nanoseconds
- Moves the servo through: 0° → 90° → 180° → 90°
- 500ms delay between each position
- Loops continuously until interrupted
Run the Simulation
- Paste the diagram.json into the Wokwi editor
- Paste the MicroPython code into main.py
- Click the ▶ green Play button
- Watch the servo arm sweep from 0° → 90° → 180° → 90°
Why Learn Servo Control with Raspberry Pi Pico?
Mastering PWM servo control with Raspberry Pi Pico opens doors to building advanced robotics and automation systems. It's one of the most important hardware skills in the maker ecosystem.
Comments
Post a Comment