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

 


Project Overview

This comprehensive guide demonstrates how to control a stepper motor using a Raspberry Pi Pico microcontroller and an A4988 stepper motor driver in the Wokwi online simulator. Learn precise motor control, microstepping, direction changes, and speed adjustment for robotics, CNC, and automation projects.

What You'll Learn

  • Stepper motor operation principles
  • A4988 driver module configuration
  • Raspberry Pi Pico GPIO control
  • Motor speed and direction programming
  • Microstepping techniques
  • Wokwi simulation for motor projects

Components Required in Wokwi

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" ] ]
  ]
}

Hardware (Virtual):

  • 1x Raspberry Pi Pico board
  • 1x A4988 Stepper Motor Driver module
  • 1x NEMA 17 Stepper Motor (or similar bipolar stepper)
  • Power supply (12V for motor - simulated in Wokwi)
  • Jumper wires (automatic in Wokwi)

Software:

  • MicroPython firmware
  • Motor control library

Understanding Stepper Motors

What is a Stepper Motor?

A stepper motor is a brushless DC motor that divides a full rotation into equal steps. Unlike regular DC motors, steppers move in precise, repeatable increments.

Key Characteristics:

  • Step Angle: Typically 1.8° (200 steps per revolution)
  • Bipolar vs Unipolar: This project uses bipolar (4 wires)
  • Holding Torque: Motor stays in position when powered
  • Precise Positioning: No feedback sensors needed

Common Applications:

  • 3D printers
  • CNC machines
  • Robotics
  • Camera sliders
  • Automated curtains

Understanding the A4988 Driver

The A4988 is a popular stepper motor driver that:

  • Controls motor current
  • Enables microstepping (1/16 step resolution)
  • Protects against overcurrent
  • Simplifies control to just STEP and DIR signals

A4988 Pin Configuration:

Pin NameFunctionDescription
VMOTMotor Power8-35V power supply for motor
GNDGroundPower ground
1A, 1BMotor Coil 1Connect to motor coil A
2A, 2BMotor Coil 2Connect to motor coil B
VDDLogic Power3-5.5V logic power (from Pico)
GNDLogic GroundLogic ground
STEPStep InputPulse to move one step
DIRDirectionHIGH = CW, LOW = CCW
ENABLEEnableLOW = enabled, HIGH = disabled
MS1, MS2, MS3Microstep SelectSet stepping resolution
RESETResetKeep HIGH for normal operation
SLEEPSleep ModeKeep HIGH for operation

Step-by-Step Wokwi Setup Instructions

Step 1: Create New Raspberry Pi Pico Project

  1. Navigate to https://wokwi.com
  2. Click "New Project"
  3. Select "Raspberry Pi Pico"
  4. Pico board appears on canvas

Step 2: Add A4988 Stepper Driver Module

  1. Click the blue "+" (Add Part) button
  2. Search for "A4988" or "Stepper Driver"
  3. Select "A4988 Stepper Motor Driver"
  4. Place it to the right of the Pico

Note: If A4988 isn't available, search for generic stepper driver modules.

Step 3: Add Stepper Motor

  1. Click the "+" button
  2. Search for "Stepper Motor" or "NEMA 17"
  3. Select "Stepper Motor"
  4. Place it above the A4988 driver

Step 4: Understanding the Wiring Connections

Complete Connection Table:

A4988 PinRaspberry Pi Pico PinWire ColorNotes
VDD3V3 (OUT)RedLogic power 3.3V
GNDGNDBlackLogic ground
STEPGPIO 2BlueStep pulse signal
DIRGPIO 3GreenDirection control
ENABLEGNDBlackAlways enabled (grounded)
RESETVDDRedKeep high (connect to VDD)
SLEEPVDDRedKeep awake (connect to VDD)
VMOTExternal 12V+RedMotor power supply
GNDExternal GNDBlackMotor power ground

Motor to A4988 Connections:

Motor WireA4988 PinCoil
Coil A+ (Red)1ACoil 1
Coil A- (Green)1BCoil 1
Coil B+ (Blue)2ACoil 2
Coil B- (Yellow)2BCoil 2

Microstepping Configuration (Optional):

MS1MS2MS3Step Resolution
LOWLOWLOWFull step
HIGHLOWLOW1/2 step
LOWHIGHLOW1/4 step
HIGHHIGHLOW1/8 step
HIGHHIGHHIGH1/16 step

For this tutorial, leave MS1, MS2, MS3 unconnected (defaults to full step).

Step 5: Wire the Circuit in Wokwi

Power Connections:

  1. Logic Power (Pico to A4988):
    • Pico 3V3 (OUT) → A4988 VDD (red wire)
    • Pico GND → A4988 GND (black wire)
  1. Motor Power (External Supply to A4988):
    • In Wokwi, click "+" and add "Power Supply"
    • Power Supply +12V → A4988 VMOT (red wire)
    • Power Supply GND → A4988 GND (black wire)
    • Also connect Power Supply GND to Pico GND (common ground)

Control Signal Connections:

  1. Pico GPIO 2 → A4988 STEP (blue wire)
  2. Pico GPIO 3 → A4988 DIR (green wire)
  3. A4988 ENABLE → Pico GND (enables driver)
  4. A4988 RESET → A4988 VDD (keeps driver active)
  5. A4988 SLEEP → A4988 VDD (prevents sleep mode)

Motor Connections:

  1. Stepper Motor Coil A+ (Red) → A4988 1A
  2. Stepper Motor Coil A- (Green) → A4988 1B
  3. Stepper Motor Coil B+ (Blue) → A4988 2A
  4. Stepper Motor Coil B- (Yellow) → A4988 2B

Wokwi Tip: Colors are suggested - Wokwi automatically assigns colors. Click each pin and drag to create connections.

CODE:

// Raspberry Pi Pico + Stepper Motor Example

#define DIR_PIN 2
#define STEP_PIN 3

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

void loop() {
  // Move 200 steps (one rotation) CW over one second
  digitalWrite(DIR_PIN, HIGH);
  for (int i = 0; i < 200; i++) {
    digitalWrite(STEP_PIN, HIGH);
    digitalWrite(STEP_PIN, LOW);
    delay(5); // 5 ms * 200 = 1 second
  }

  delay(500); // Wait half a second

  // Move 200 steps (one rotation) CCW over 400 millis
  digitalWrite(DIR_PIN, LOW);
  for (int i = 0; i < 200; i++) {
    digitalWrite(STEP_PIN, HIGH);
    digitalWrite(STEP_PIN, LOW);
    delay(2); // 2 ms * 200 = 0.4 seconds
  }

  delay(1000); // Wait another second
}



Comments