Chapter 19 Stepper Motor
In this project, we will learn how to drive a stepper motor, and understand its working principle.
Project 19.1 Stepper Motor
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
|
Breadboard x1
|
||
Stepper Motor x1
|
Jumper F/M x6 |
ULN2003 Stepper Motor Driver x1 |
Component knowledge
Stepper Motor
Stepper motors are an open-loop control device, which converts an electronic pulse signal into angular displacement or linear displacement. In a non-overload condition, the speed of the motor and the location of the stops depends only on the pulse signal frequency and number of pulses and is not affected by changes in load as with a DC motor. A small four-phase deceleration stepper motor is shown here:
The electronic schematic diagram of a four-phase stepper motor is shown below:
The outside case or housing of the stepper motor is the stator and inside the stator is the rotor. There are a specific number of individual coils, usually an integer multiple of the number of phases the motor has, when the stator is powered ON, an electromagnetic field will be formed to attract a corresponding convex diagonal groove or indentation in the rotor’s surface. The rotor is usually made of iron or a permanent magnet. Therefore, the stepper motor can be driven by powering the coils on the stator in an ordered sequence (producing a series of “steps” or stepped movements).
A common driving process is as follows:
In the course above, the stepper motor rotates a certain angle once, which is called a step. By controlling the number of rotation steps, you can control the stepper motor rotation angle. By controlling the time between two steps, you can control the stepper motor rotation speed. When rotating clockwise, the order of coil powered on is: A -> B -> C -> D -> A -> …… . And the rotor will rotate in accordance with the order, step by step down, called four steps four pats. If the coils is powered on in the reverse order, D -> C -> B -> A -> D -> … , the rotor will rotate in anti-clockwise direction.
There are other methods to control stepper motors, such as: connect A phase, then connect A B phase, the stator will be located in the center of A B, which is called a half-step. This method can improve the stability of the stepper motor and reduces noise. The sequence of powering the coils looks like this: A -> AB -> B -> BC -> C -> CD -> D -> DA -> A -> …… , the rotor will rotate in accordance to this sequence at a half-step at a time, called four-steps, eight-part. Conversely, if the coils are powered ON in the reverse order the stepper motor will rotate in the opposite direction.
The stator in the stepper motor we have supplied has 32 magnetic poles. Therefore, to complete one full revolution requires 32 full steps. The rotor (or output shaft) of the stepper motor is connected to a speed reduction set of gears and the reduction ratio is 1:64. Therefore, the final output shaft (exiting the stepper motor’s housing) requires 32 X 64 = 2048 steps to make one full revolution.
ULN2003 Stepper motor driver
A ULN2003 stepper motor driver is used to convert weak signals into more powerful control signals in order to drive the stepper motor. In the illustration below, the input signal IN1-IN4 corresponds to the output signal A-D, and 4 LEDs are integrated into the board to indicate the state of these signals. The PWR interface can be used as a power supply for the stepper motor. By default, PWR and VCC are connected.
Circuit
When building the circuit, note that rated voltage of the stepper motor is 5V, and we need to use the breadboard power supply independently. Additionally, the breadboard power supply needs to share Ground with ESP32.
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Sketch
This code uses the four-step, four-part mode to drive the stepper motor in the clockwise and anticlockwise directions.
Sketch_19.1_Drive_Stepper_Motor
Compile and upload the code to the ESP32-WROVER, the stepper motor will rotate 360° clockwise and stop for 1s, and then rotate 360° anticlockwise and stop for 1s. And it will repeat this action in an endless loop.
The following is the program code:
1/**********************************************************************
2 Filename : Drive Stepper Motor
3 Description : Use ULN2003 to drive the stepper motor.
4 Auther : www.freenove.com
5 Modification: 2024/06/19
6**********************************************************************/
7// Conncet the port of the stepper motor driver
8int outPorts[] = {14, 27, 26, 25};
9
10void setup() {
11 // set pins to output
12 for (int i = 0; i < 4; i++) {
13 pinMode(outPorts[i], OUTPUT);
14 }
15}
16
17void loop()
18{
19 // Rotate a full turn
20 moveSteps(true, 32 * 64, 3);
21 delay(1000);
22 // Rotate a full turn towards another direction
23 moveSteps(false, 32 * 64, 3);
24 delay(1000);
25}
26
27//Suggestion: the motor turns precisely when the ms range is between 3 and 20
28void moveSteps(bool dir, int steps, byte ms) {
29 for (unsigned long i = 0; i < steps; i++) {
30 moveOneStep(dir); // Rotate a step
31 delay(constrain(ms,3,20)); // Control the speed
32 }
33}
34
35void moveOneStep(bool dir) {
36 // Define a variable, use four low bit to indicate the state of port
37 static byte out = 0x01;
38 // Decide the shift direction according to the rotation direction
39 if (dir) { // ring shift left
40 out != 0x08 ? out = out << 1 : out = 0x01;
41 }
42 else { // ring shift right
43 out != 0x01 ? out = out >> 1 : out = 0x08;
44 }
45 // Output singal to each port
46 for (int i = 0; i < 4; i++) {
47 digitalWrite(outPorts[i], (out & (0x01 << i)) ? HIGH : LOW);
48 }
49}
50
51void moveAround(bool dir, int turns, byte ms){
52 for(int i=0;i<turns;i++)
53 moveSteps(dir,32*64,ms);
54}
55void moveAngle(bool dir, int angle, byte ms){
56 moveSteps(dir,(angle*32*64/360),ms);
57}
In this project, we define four pins to drive stepper motor.
1// Conncet the port of the stepper motor driver
2int outPorts[] = {14, 27, 26, 25};
moveOneStep Function is used to drive the stepper motor to rotate clockwise or counterclockwise. The parameter “dir” indicates the direction of rotation. If “dir” returns true, the stepper motor rotates clockwise, otherwise the stepper motor rotates counterclockwise.
void moveOneStep(bool dir) {
......
}
Define a static byte variable, calculate the value of the variable according to the rotation direction of the motor, and use the keyword static to save the position status of the previous step of the stepper motor. Use the four low bits of the variable to control the output state of the four pins.
1// Define a variable, use four low bit to indicate the state of port
2static byte out = 0x01;
3// Decide the shift direction according to the rotation direction
4if (dir) { // ring shift left
5 out != 0x08 ? out = out << 1 : out = 0x01;
6}
7else { // ring shift right
8 out != 0x01 ? out = out >> 1 : out = 0x08;
9}
Make the pin to output corresponding level based on the value of the variable.
1// Output singal to each port
2for (int i = 0; i < 4; i++) {
3 digitalWrite(outPorts[i], (out & (0x01 << i)) ? HIGH : LOW);
4}
The moveSteps function can control the direction of the stepper motor, the number of rotation steps, and the speed of rotation. According to the previous knowledge, the stepper motor needs 32*64 steps for one revolution. The speed of rotation is determined by the parameter ms. The larger the ms is, the slower the rotation speed is. There is a range for the speed of the motor, which is determined by the motor itself and according to our test, the value of ms is limited to 3-20.
1//Suggestion: the motor turns precisely when the ms range is between 3 and 20
2void moveSteps(bool dir, int steps, byte ms) {
3 for (unsigned long i = 0; i < steps; i++) {
4 moveOneStep(dir); // Rotate a step
5 delay(constrain(ms,3,20)); // Control the speed
6 }
7}
The function moveTurns() is a further package of moveSteps(), which is used to control the stepper motor to rotate a specified number of turns. The parameter “turns” represents the number of turns that need to be rotated.
1void moveAround(bool dir, int turns, byte ms){
2 for(int i=0;i<turns;i++)
3 moveSteps(dir,32*64,ms);
4}
The function moveAround () is a further package of moveSteps (), which is used to control the stepper motor to rotate by a specified angle, and the parameter “angle” represents the angle to be rotated.
1void moveAngle(bool dir, int angle, byte ms){
2 moveSteps(dir,(angle*32*64/360),ms);
3}
In the loop function, call the moveSteps function to loop the stepper motor: rotate clockwise one turn and stop for 1s, then rotate counterclockwise one turn and stop for 1s.
1void loop()
2{
3 // Rotate a full turn
4 moveSteps(true, 32 * 64, 3);
5 delay(1000);
6 // Rotate a full turn towards another direction
7 moveSteps(false, 32 * 64, 3);
8 delay(1000);
9}







