Stepper Motor – Raspberry PICO

A stepper motor allows electrical pulses to be converted into angular movement.

There are three main types of stepper motors:

  • Variable reluctance motor

  • Permanent magnet motor

  • Hybrid motor, which combines both technologies

For this project with the Raspberry Pi Pico, we will use a permanent magnet motor, specifically the 28BYJ-48 model.

Stepper motors are ideal for precise angular positioning, such as in printers, scanners, and hard drives. They offer several advantages: they are affordable, easy to use, hold their position when stopped, can rotate continuously in either direction, and allow precise control of rotation.

Here is the wiring diagram for connecting the stepper motor to a Raspberry Pi Pico:

// 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
}