How to Control Servo Motor with Raspberry Pi Pico in Wokwi: Complete PWM Tutorial

This tutorial shows you how to wire and program a servo motor with Raspberry Pi Pico using PWM (Pulse Width Modulation) in the Wokwi simulator. Perfect for robotics, automation, and mechanical control projects!

What is a Servo Motor?

A servo motor is a rotary actuator that allows precise control of angular position (usually 0° to 180°). Unlike regular DC motors, servos can move to and hold a specific angle, making them ideal for robotic arms, steering mechanisms, camera gimbals, and more.

Types of Servo Motors

Standard Servo (180° rotation):

  • Rotates from 0° to 180°
  • Most common type
  • Used in this tutorial

Continuous Rotation Servo:

  • Rotates continuously like a DC motor
  • Speed and direction controlled by PWM

360° Digital Servo:

  • Full 360° positioning
  • Higher precision and torque

Getting Started with Wokwi

Step 1: Open Wokwi and Create New Project

  • Go to https://wokwi.com
  • Click "New Project"
  • Select "Raspberry Pi Pico" or "Raspberry Pi Pico W" as your board

Step 2: Add Servo Motor Component

Add Servo:

  • Click the blue "+" button
  • Search for "servo" or "servo motor"
  • Select "Servo Motor"
  • Click to add it to your workspace
  • The default is typically a standard 180° servo

Step 3: Understanding Servo Motor Pins

A standard servo has 3 wires:

Wire ColorFunctionConnection
Brown/BlackGround (GND)Connect to GND on Pico
RedPower (VCC)Connect to VBUS (5V) on Pico
Orange/Yellow/WhiteSignal (PWM)Connect to GPIO pin on Pico

Note: Wire colors may vary by manufacturer:

  • Signal: Orange, Yellow, or White
  • Power: Red
  • Ground: Brown or Black

Step 4: Wire the Circuit in Wokwi

{
  "version": 1,
  "author": "Gabriel Barrionuevo",
  "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+", "green", [ "h0" ] ],
    [ "pico:GP15", "servo1:PWM", "green", [ "h0" ] ]
  ],
  "dependencies": {}
}

Based on your diagram, make these connections:

Servo Motor Connections:

  1. Brown/Black wire (GND)GND on Raspberry Pi Pico
  2. Red wire (VCC)VBUS (5V) on Raspberry Pi Pico
  3. Orange/Yellow wire (Signal)GP15 on Raspberry Pi Pico

Wiring Steps:

  1. Click the GND (brown/black) pin on servo → drag to GND on Pico
  2. Click the VCC (red) pin on servo → drag to VBUS on Pico
  3. Click the Signal (orange) pin on servo → drag to GP15 on Pico

Why GP15?

  • You can use any GPIO pin that supports PWM
  • GP15 is commonly used but GP0-GP28 all support PWM
  • The example code uses GP15, but you can change it

Step 5: Understanding Servo Control with PWM

How Servo Motors Work:

Servos are controlled by PWM signals:

  • Frequency: 50 Hz (20ms period)
  • Pulse Width determines angle:
    • 1ms (5% duty) = 0° (minimum angle)
    • 1.5ms (7.5% duty) = 90° (center)
    • 2ms (10% duty) = 180° (maximum angle)

CODE:

from machine import Pin, PWM
import time
servo = PWM (Pin (15))
servo.freq (50)

while True:
    servo.duty_ns (500000)
    time. sleep_ms(500)
    servo.duty_ns (1500000)
    time. sleep_ms(500)
    servo.duty_ns (2500000)
    time. sleep_ms(500)
    servo.duty_ns (1500000)
    time. sleep_ms(500)


 

Comments