Raspberry Pi Pico Servo Motor Control Tutorial Using PWM (Wokwi Simulator)

Servo Motor Control with Raspberry Pi Pico W using PWM | MakeMindz
🤖 Raspberry Pi Pico W Tutorial

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.

⚡ PWM Control 🐍 MicroPython 🖥️ Wokwi Simulator 🔰 Beginner Friendly 🦾 Robotics Ready

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 MOTOR
90 degrees
Pulse: 1500000 ns
← Click angles to see servo move →

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

🔄
Standard Servo (180°)
Rotates between 0° and 180°. Most common type, ideal for beginner robotics projects.
✓ Used in This Tutorial
Continuous Rotation Servo
Rotates continuously like a DC motor. PWM controls speed and direction instead of position.
🎯
360° Digital Servo
Full 360° positioning capability. Higher precision and torque for advanced automation.
🛠

Hardware Used in This Tutorial

🎛️
Raspberry Pi Pico WMicrocontroller
⚙️
Standard 180° ServoActuator
🖥️
Wokwi SimulatorOnline Platform
1

Create a New Project in Wokwi

  1. Go to wokwi.com and sign in
  2. Click New Project
  3. Select Raspberry Pi Pico or Raspberry Pi Pico W
  4. Add a Servo Motor component from the parts panel
💡
TipWokwi will automatically place the Pico W on a breadboard ready for connections.
2

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
⚠️
NoteWire colors may vary by manufacturer. Always check your servo's datasheet.
3

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
ℹ️
Why GP15? All GPIO pins (GP0–GP28) on the Pico support PWM. GP15 is used here, but you can use any PWM-capable pin.

📄 diagram.json — Copy & Paste into Wokwi

diagram.json
{
  "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
500 000 ns
1ms ON / 19ms OFF
90°
1 500 000 ns
1.5ms ON / 18.5ms OFF
180°
2 500 000 ns
2.5ms ON / 17.5ms OFF
PWM @ 50Hz — Longer pulse = larger angle
Pulse Width (ns) Pulse Width (ms) Servo Angle
500 000 0.5 ms
1 500 000 1.5 ms 90° (centre)
2 500 000 2.5 ms 180°
4

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.

main.py — MicroPython
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
5

Run the Simulation

  1. Paste the diagram.json into the Wokwi editor
  2. Paste the MicroPython code into main.py
  3. Click the ▶ green Play button
  4. Watch the servo arm sweep from 0° → 90° → 180° → 90°
Expected ResultThe servo horn will continuously sweep between positions every 500ms — just like a real servo motor in a robotics project.
▶ Open Free Simulation on Wokwi Test the servo motor project instantly — no hardware needed
🚀 Launch Simulation
🎓

Why Learn Servo Control with Raspberry Pi Pico?

🦾
Robotics
Control arms, grippers, and joints in robot builds
🏭
Automation
Drive valves, locks, and actuators in smart systems
🖨️
CNC & 3D Print
Precise positioning for machine axes
🌐
IoT Devices
Remote-controlled smart home actuators

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.

🔰 Beginner Projects
⚙️ Intermediate Projects
🚀 Advanced Projects

Hands-on Electronics & MicroPython Tutorials for Makers, Students & Engineers

www.makemindz.com

Comments

try for free