How to Control Stepper Motor with Raspberry Pi Pico and A4988 Driver in Wokwi - Complete Step-by-Step Tutorial
How to Control a Stepper Motor with Raspberry Pi Pico on Wokwi
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.
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.
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
| Property | Value / Detail |
|---|---|
| Step Angle | 1.8° — 200 steps per full rotation (NEMA 17) |
| Motor Type | Bipolar — 4 wires (2 coils) |
| Holding Torque | Motor locks in position when powered |
| Positioning | Open-loop — no encoder required |
| Driver Required | Yes — A4988 handles coil switching |
Common Applications
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.
| Pin | Function | Description |
|---|---|---|
| VMOT | Motor Power | 8–35V supply for motor coils |
| GND | Ground | Motor power ground |
| VDD | Logic Power | 3–5.5V from Pico (3.3V) |
| GND | Logic Ground | Shared with Pico GND |
| 1A / 1B | Motor Coil A | Connect to stepper coil A |
| 2A / 2B | Motor Coil B | Connect to stepper coil B |
| STEP | Step Pulse | Rising edge = one step |
| DIR | Direction | HIGH = CW, LOW = CCW |
| ENABLE | Driver Enable | LOW = active, HIGH = off |
| MS1/MS2/MS3 | Microstep Select | Sets stepping resolution |
| RESET | Reset | Keep HIGH for normal operation |
| SLEEP | Sleep Mode | Keep HIGH to stay active |
Step-by-Step Wokwi Setup
-
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
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
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: angleto see the shaft angle indicator during simulation. -
4
Wire the Control Signals (Pico → A4988)
From (Pico) To (A4988) Color Purpose VBUS (5V) VDD Red Logic power GND.8 GND.2 Black Common ground GP3 STEP Orange Step pulse signal GP2 DIR Violet Direction 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
Wire the Motor Coils (A4988 → Stepper Motor)
Motor Wire A4988 Pin Color Coil B− (Coil B minus) 1B Black Coil 1 B+ (Coil B plus) 1A Green Coil 1 A+ (Coil A plus) 2A Blue Coil 2 A− (Coil A minus) 2B Red Coil 2 -
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:
MS1 MS2 MS3 Resolution Steps/Rev LOW LOW LOW Full step 200 HIGH LOW LOW ½ step 400 LOW HIGH LOW ¼ step 800 HIGH HIGH LOW ⅛ step 1,600 HIGH HIGH HIGH 1/16 step 3,200 -
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.
{
"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.
// 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.
Comments
Post a Comment