How to Control Stepper Motor with Raspberry Pi Pico and A4988 Driver in Wokwi - Complete Step-by-Step Tutorial

How to Control Stepper Motor with Raspberry Pi Pico on Wokwi | MakeMindz
🔗 Part of the 20 Raspberry Pi Pico Projects series — View all on MakeMindz →
Intermediate Project
⚙️ Project 9 of 20

How to Control a Stepper Motor with Raspberry Pi Pico on Wokwi

⚙️ Intermediate 📅 February 2026 ⏱ 15 min read 🛠 A4988 + NEMA 17

Control a NEMA 17 stepper motor using a Raspberry Pi Pico and A4988 driver — entirely in your browser with Wokwi. Covers motor wiring, microstepping, direction reversal, and speed control with Arduino C++. Full diagram.json and live simulation included.

Launch Live Simulation on Wokwi

What This Project Demonstrates

This guide shows how to use a Raspberry Pi Pico to drive a bipolar stepper motor via an A4988 driver module — a combination used in 3D printers, CNC routers, and robotics. You'll master precise step control, variable speed, and direction switching, all without physical hardware.

🔄Stepper motor operation principles
🔌A4988 driver configuration
📟GPIO step & direction control
Speed & direction programming
🎯Microstepping techniques
🖥Wokwi motor simulation

Understanding Stepper Motors

A stepper motor is a brushless DC motor that divides a full 360° rotation into equal discrete steps. Unlike regular DC motors, steppers move in precise, repeatable increments — no feedback sensors needed.

Key Characteristics

PropertyValue / Detail
Step Angle1.8° — 200 steps per full rotation (NEMA 17)
Motor TypeBipolar — 4 wires (2 coils)
Holding TorqueMotor locks in position when powered
PositioningOpen-loop — no encoder required
Driver RequiredYes — A4988 handles coil switching

Common Applications

🖨 3D Printers 🔩 CNC Machines 🤖 Robotics 📷 Camera Sliders 🪟 Automated Curtains 🔬 Lab Equipment

Understanding the A4988 Driver

The A4988 is the industry-standard stepper driver. It simplifies control to just two signals: STEP (pulse to move one step) and DIR (direction). It also handles all coil-switching logic and supports microstepping up to 1/16 resolution.

PinFunctionDescription
VMOTMotor Power8–35V supply for motor coils
GNDGroundMotor power ground
VDDLogic Power3–5.5V from Pico (3.3V)
GNDLogic GroundShared with Pico GND
1A / 1BMotor Coil AConnect to stepper coil A
2A / 2BMotor Coil BConnect to stepper coil B
STEPStep PulseRising edge = one step
DIRDirectionHIGH = CW, LOW = CCW
ENABLEDriver EnableLOW = active, HIGH = off
MS1/MS2/MS3Microstep SelectSets stepping resolution
RESETResetKeep HIGH for normal operation
SLEEPSleep ModeKeep HIGH to stay active

Step-by-Step Wokwi Setup

  1. 1

    Create a New Raspberry Pi Pico Project

    Go to wokwi.com, click "New Project" and select "Raspberry Pi Pico". The Pico board appears on the canvas and MicroPython is selected by default — we'll switch to Arduino/C++ for this project.

  2. 2

    Add the A4988 Stepper Driver

    Click the blue "+" button, search for "A4988" and select "A4988 Stepper Motor Driver". Place it to the right of the Pico. Rotate it 270° to align the pin labels logically with the Pico's GPIO pins.

    💡

    In Wokwi, right-click the A4988 and choose Rotate if needed to match the diagram.json layout.

  3. 3

    Add the Stepper Motor

    Click "+" again, search for "Stepper Motor" or "NEMA 17", and place it above the A4988. In the component attributes panel, set display: angle to see the shaft angle indicator during simulation.

  4. 4

    Wire the Control Signals (Pico → A4988)

    From (Pico)To (A4988)ColorPurpose
    VBUS (5V)VDDRedLogic power
    GND.8GND.2BlackCommon ground
    GP3STEPOrangeStep pulse signal
    GP2DIRVioletDirection control
    ⚠️

    SLEEP ↔ RESET bridge: Connect A4988's SLEEP pin to its own RESET pin to keep the driver awake and active. This avoids needing extra GPIO pins.

  5. 5

    Wire the Motor Coils (A4988 → Stepper Motor)

    Motor WireA4988 PinColorCoil
    B− (Coil B minus)1BBlackCoil 1
    B+ (Coil B plus)1AGreenCoil 1
    A+ (Coil A plus)2ABlueCoil 2
    A− (Coil A minus)2BRedCoil 2
  6. 6

    Microstepping Configuration (Optional)

    For this tutorial, leave MS1, MS2, MS3 unconnected — this defaults to full step mode (200 steps/rev). For smoother motion, connect the MS pins according to this table:

    MS1MS2MS3ResolutionSteps/Rev
    LOWLOWLOWFull step200
    HIGHLOWLOW½ step400
    LOWHIGHLOW¼ step800
    HIGHHIGHLOW⅛ step1,600
    HIGHHIGHHIGH1/16 step3,200
  7. 7

    Enter the Code and Run

    Click the code editor tab in Wokwi, paste the Arduino C++ code below, then press the green ▶ Play button. The stepper motor shaft indicator will rotate — clockwise for one full revolution, then counter-clockwise at double speed.

diagram.json — Complete Circuit

Paste this into the diagram.json tab in your Wokwi project to instantly load the full circuit — Pico, A4988 driver, and stepper motor, all pre-wired.

JSON — diagram.json
{
  "version": 1,
  "author": "Uri Shaked",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-pi-pico",
      "id": "pico",
      "top": 63.95,
      "left": 86.4,
      "attrs": {}
    },
    {
      "type": "wokwi-a4988",
      "id": "drv1",
      "top": 91.4,
      "left": 226,
      "rotate": 270,
      "attrs": {}
    },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper1",
      "top": -179.4,
      "left": 141.6,
      "attrs": { "display": "angle" }
    }
  ],
  "connections": [
    [ "drv1:1B",    "stepper1:B-", "black",  [ "v0" ] ],
    [ "drv1:1A",    "stepper1:B+", "green",  [ "v0" ] ],
    [ "stepper1:A+", "drv1:2A",     "blue",   [ "v0" ] ],
    [ "stepper1:A-", "drv1:2B",     "red",    [ "v0" ] ],
    [ "drv1:SLEEP",  "drv1:RESET",  "green",
        [ "v15.92", "h-9.9" ] ],
    [ "drv1:STEP",   "pico:GP3",    "orange",
        [ "v130.8", "h-203.87", "v-163.2" ] ],
    [ "drv1:DIR",    "pico:GP2",    "violet",
        [ "v140.4", "h-223.07", "v-182.4" ] ],
    [ "drv1:GND.2", "pico:GND.8", "black",  [ "v0" ] ],
    [ "pico:VBUS",   "drv1:VDD",    "red",    [ "h0" ] ]
  ]
}

How to use: In your Wokwi project, click the diagram.json tab at the top of the editor, select all text, and paste this JSON. The circuit loads instantly.

Arduino C++ — Motor Control Program

This code rotates the motor 200 steps (one full revolution) clockwise over 1 second, pauses, then rotates 200 steps counter-clockwise at double speed (0.4 seconds), then repeats.

Arduino C++ — sketch.ino
// Raspberry Pi Pico + A4988 Stepper Motor Control
// MakeMindz.com — Project 9 of 20

#define DIR_PIN  2   // GP2 → A4988 DIR
#define STEP_PIN 3   // GP3 → A4988 STEP

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN,  OUTPUT);
  digitalWrite(STEP_PIN, LOW);
}

void loop() {
  // ── Clockwise: 200 steps in ~1 second ──
  digitalWrite(DIR_PIN, HIGH);
  for (int i = 0; i < 200; i++) {
    digitalWrite(STEP_PIN, HIGH);
    digitalWrite(STEP_PIN, LOW);
    delay(5);  // 5ms × 200 = 1.0 second
  }

  delay(500);  // Pause 500ms between directions

  // ── Counter-clockwise: 200 steps in ~0.4 second ──
  digitalWrite(DIR_PIN, LOW);
  for (int i = 0; i < 200; i++) {
    digitalWrite(STEP_PIN, HIGH);
    digitalWrite(STEP_PIN, LOW);
    delay(2);  // 2ms × 200 = 0.4 seconds
  }

  delay(1000);  // Pause 1 second before repeating
}
📌

Speed control: The delay() value controls RPM. Decrease it for faster rotation, increase for slower. At 5ms/step: 1000ms ÷ (5ms × 200 steps) = 1 revolution/second = 60 RPM.

All 20 Raspberry Pi Pico Projects

Comments

try for free