In this lesson, you will learn a fun and easy way to drive a stepper motor.
The stepper we are using comes with its own driver board making it easy to connect to our ESP32.
(1) x Elegoo ESP32
(2) x 400 tie-points breadboard
(1) x ULN2003 stepper motor driver module
(1) x Stepper motor
(1) x 9V 1A Adapter
(1) x Power supply module
(6) x F-M wires (Female to Male DuPont wires)
(2) x M-M wire (Male to Male jumper wire)
A stepper motor is an electromechanical device which converts electrical pulses into discrete mechanical movements. The shaft or spindle of a stepper motor rotates in discrete step increments when electrical command pulses are applied to it in the proper sequence.
The motors rotation has several direct relationships to these applied input pulses. The sequence of the applied pulses is directly related to the direction of motor shafts rotation.
The speed of the motor shafts rotation is directly related to the frequency of the input pulses and the length of rotation is directly related to the number of input pulses applied.
One of the most significant advantages of a stepper motor is its ability to be accurately controlled in an open loop system. Open loop control means no feedback information about position is needed. This type of control eliminates the need for expensive sensing and feedback devices such as optical encoders.
Your position is known simply by keeping track of the input step pulses.
Stepper motor 28BYJ-48 Parameters
| Parameter | Value/Specification | Parameter | Value/Specification | |
|---|---|---|---|---|
| Model | 28BYJ-48 | In-traction Torque | >34.3mN.m(120Hz) | |
| Rated voltage | 5VDC | Self-positioning Torque | >34.3mN.m | |
| Number of Phase | 4 | Friction torque | 600-1200 gf.cm | |
| Speed Variation Ratio | 1/64 | Pull in torque | 300 gf.cm | |
| Stride Angle | 5.625°/64 | Insulated resistance | >10MΩ(500V) | |
| Frequency | 100Hz | Insulated electricity power | 600VAC/1mA/1s | |
| DC resistance | 50Ω±7%(25℃) | Insulation grade | A | |
| Idle In-traction Frequency | > 600Hz | Rise in Temperature | <40K(120Hz) | |
| Idle Out-traction Frequency | > 1000Hz | Noise | <35dB(120Hz,No load,10cm) |
Interfacing circuits
The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar steppers have no common center connection. They have two independent sets of coils instead. You can distinguish them from unipolar steppers by measuring the resistance between the wires. You should find two pairs of wires with equal resistance. If you’ve got the leads of your meter connected to two wires that are not connected (i.e. not attached to the same coil), you should see infinite resistance (or no continuity).
·Use ULN2003 driver chip, 500mA
·A. B. C. D LED indicating the four phase stepper motor working condition.
·White jack is the four phase stepper motor standard jack.
·Power pins are separated
·We kept the rest pins of the ULN2003 chip for your further prototyping.
The simplest way of interfacing a unipolar stepper to Arduino is to use a breakout for ULN2003A transistor array chip. The ULN2003A contains seven Darlington transistor drivers and is somewhat like having seven TIP120 transistors all in one package. The ULN2003A can pass up to 500 mA per channel and has an internal voltage drop of about 1V when on. It also contains internal clamp diodes to dissipate voltage spikes when driving inductive loads. To control the stepper, apply voltage to each of the coils in a specific sequence.
The sequence would go like this:
| LeadWire Color | --> CW Direction (1-2 Phase) | |||||||
|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
| 4 ORG | - | - | ||||||
| 3 YEL | ◇ | - | ||||||
| 2 PIK | - | - | ||||||
| 1 BLU | - | - | - | |||||
You can click the blue text link to download the program file to your local device, and double-click the file to open it after the download is complete. Please note: Before opening the file, ensure that you have installed the Arduino IDE development environment and completed the installation of relevant components such as the board support package and driver corresponding to the ESP32 development board. If you have any questions about this operation process, you can refer to the "part 1" chapter of the document for detailed guidance.
Click the Serial Monitor button to turn on the serial monitor. The basics about the serial monitor are introduced in details in tutorial 4 in part 2.
#include <Stepper.h>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int rolePerMinute = 10; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm// initialize the stepper library on pins :
Stepper myStepper(stepsPerRevolution, 21,18,19,5);//ln1-ln3-ln2-ln4
Library and Hardware Configuration:
#include <Stepper.h>: Includes the Arduino Stepper library for stepper motor controlconst int stepsPerRevolution = 2048: Defines the number of steps per full revolution for the 28BYJ-48 stepper motorconst int rolePerMinute = 10: Defines the motor speed in revolutions per minute (RPM)Stepper myStepper(stepsPerRevolution, 21,18,19,5): Creates a Stepper object with specified pins
void setup() {
myStepper.setSpeed(rolePerMinute);
// initialize the serial port:
Serial.begin(9600);
}
setup() Function:Initializes the system and configures the stepper motor.
myStepper.setSpeed(rolePerMinute): Sets the motor speed in revolutions per minute (10 RPM)Serial.begin(9600): Initializes serial communication at 9600 baud rate for operation feedbackStepper Speed Setting:
loop() Function Overview:The main loop that makes the stepper motor rotate alternately in clockwise and counterclockwise directions.
1. Clockwise Rotation Module:
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
myStepper.step(stepsPerRevolution) moves the motor 2048 steps (one full revolution)2. Counterclockwise Rotation Module:
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
myStepper.step(-stepsPerRevolution) moves the motor -2048 steps (one full revolution in reverse)Stepper Motor Operation:
Pin Configuration (ESP32 to ULN2003 Driver):
| ESP32 Pin | ULN2003 Driver Pin | Motor Phase |
|---|---|---|
| GPIO 21 | IN1 | Phase 1 |
| GPIO 18 | IN3 | Phase 3 |
| GPIO 19 | IN2 | Phase 2 |
| GPIO 5 | IN4 | Phase 4 |
| 5V | VCC | Power supply |
| GND | GND | Ground |
Serial Monitor Output:
Troubleshooting:
Stepper Library Functions:
Stepper(steps, pin1, pin2, pin3, pin4): Creates a 4-wire stepper motor objectsetSpeed(rpm): Sets the motor speed in revolutions per minutestep(steps): Moves the motor a specified number of steps