Control a 28BYJ-48 stepper motor with ULN2003

FunduinoPublished on

2048 Schritte pro Umdrehung – ohne externe Spannungsversorgung

Der 28BYJ-48 eignet sich besonders für kleine Anwendungen am Arduino: Er läuft ohne externe Spannungsversorgung und entwickelt trotzdem ein relativ hohes Drehmoment. Möglich macht das ein Getriebe im Metallgehäuse vor dem eigentlichen Schrittmotor. Dadurch teilt sich eine ganze Umdrehung der Antriebswelle in 2048 Einzelschritte auf. Der Nachteil: Schnell dreht er nicht.

Den Schrittmotor schließt du an eine Motorsteuerungsplatine an. Sie versorgt ihn mit genug Energie, sodass die Leistung nicht über die digitalen Pins des Arduino-Boards laufen muss. Die Platine gibt es in zwei Versionen – die seitlichen Pins ragen entweder nach oben oder nach unten heraus. Die Anschlussbelegung ist bei beiden gleich.

Stepper motor 28BYJ-48 and ULN2003 driver board side by side
28BYJ-48 stepper motor with ULN2003 driver board

You need this

28BYJ-48 stepper motor - with ULN2003 driver board, 5 V 28BYJ-48 stepper motor - with ULN2003 driver board, 5 V 2,60 € *Item no. F23107464About the product — 28BYJ-48 stepper motor - with ULN2003 driver board, 5 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 3 parts together 15,46 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Wiring on the Arduino

  • IN1 of the motor driver board to pin 6
  • IN2 to pin 5
  • IN3 to pin 4
  • IN4 to pin 3
  • GND to a GND pin on the Arduino board
  • VCC to the 5V pin on the Arduino board

If the pins on your motor driver board point downward, you can also plug it directly into the microcontroller board.

Pinout of the ULN2003 Driver Board
Pinout of the ULN2003 driver board
ULN2003 board mounted on the Arduino
Driver board plugged directly into the microcontroller board
1 / 2

The program code

This sample code makes the motor rotate forward and backward by 2048 steps in turn - each corresponding to one full rotation.

One revolution forward and back
#include <Stepper.h> // Add the program library.
int SPU = 2048; // Steps per revolution.
Stepper Motor(SPU, 3,5,4,6); // The stepper motor is named "Motor"; the pins it is connected to are listed afterward.
void setup() // The setup begins here.
{
Motor.setSpeed(5); // Set the speed in revolutions per minute.
}
void loop() {
Motor.step(2048); // The motor takes 2048 steps, which is one revolution.
delay(1000); // This pause keeps the motor stopped for one second after it turns.
Motor.step(-2048); // The minus sign makes the motor take 2048 steps in the opposite direction.
delay(1000); // Pause for another second.
}

Rotation without large numbers of steps

Commands with a large number of steps - Motor.step(x) - cause problems in larger sketches: While the command is running, the microcontroller cannot receive or send any other signals. This is similar to the problem with long delays. So it's better to execute the rotation in a loop.

For example: The motor should rotate until a button is pressed - for instance, as a stop function in a machine. The motor should then stop immediately, rather than waiting for its rotation to finish. With a large number of steps, you couldn't stop it at an arbitrary point; it would only stop once it had finished rotating.

Rotate until a button is pressed
#include <Stepper.h>
int SPU = 2048;
Stepper Motor(SPU, 3,5,4,6);
int Taster = 7; // Pushbutton on pin 7
int Tasterstatus = 0; // Variable for the pushbutton status
void setup()
{
Motor.setSpeed(4);
pinMode(Taster, INPUT);
}
void loop()
{
Tasterstatus = digitalRead(Taster); // First, the pushbutton is checked.
while(Tasterstatus == HIGH) // As long as the pushbutton is pressed …
 {
 delay(1); // … the sketch jumps into these braces and stays here.
 Tasterstatus = digitalRead(Taster); // The pushbutton must be checked repeatedly inside the loop so the microcontroller can detect a change. This is the only way to exit the loop when the button is released.
 } // Once the pushbutton is no longer pressed, the sketch returns to the regular loop.
Motor.step(1); // … turn the motor by a single step.
} // Now the sketch starts over. As long as the pushbutton is not pressed, the while loop does not run, and the motor continues to turn one step at a time.
Rate the instructions

Comments

2 posts
Team Funduino

Hallo @Joachim Zeising,

cool, dass dein Schrittmotor jetzt einen Bagger antreiben soll! Die Welle des 28BYJ-48 ist rund mit zwei abgeflachten Seiten, Lego-Achsen haben ein Kreuzprofil. Direkt passt das also nicht. Zwei Wege:

3D-gedruckter Adapter: Auf Plattformen wie Thingiverse oder Printables findest du mit der Suche „28BYJ-48 Lego“ Adapter. Sie nehmen auf der einen Seite die Motorwelle und auf der anderen eine Lego-Achse auf.
Ohne 3D-Drucker: Ein Lego-Zahnrad oder eine Achsverbindung vorsichtig aufbohren und auf die Welle kleben. Das ist nicht elegant, reicht für erste Tests aber oft.
Wichtig: Befestige den Motor selbst fest am Modell. Sonst dreht sich der Motor statt der Schaufel. Bei schweren Baggerarmen hilft zusätzlich eine Zahnrad-Untersetzung.

Mit freundlichen Grüßen
Das Funduino Team

Joachim Zeising

klappt alles, aber wie kann ich den Schrittmotor mit einem Legobagger verbinden? Man braucht eine Art Verbindungsstück zur Antriebswelle, so ähnlich wie beim Servo-Motor.

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.