You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/week-5/stepper-motor.md
+38-3Lines changed: 38 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,16 +49,51 @@ Components required:
49
49
</Tabs>
50
50
:::
51
51
52
-
## Programming your blank
52
+
## Programming your stepper motor
53
+
54
+
As with the servo motor, to control and program our stepper motor, we have to interface with the Stepper library. We can include the library in our sketch using `#include <Stepper.h>` at the very top of our program.
55
+
56
+
A stepper motor moves in small increments. The `28BYJ-48` motor, when used with the `ULN2003` driver, typically needs 2048 steps to complete one full rotation.
57
+
58
+
```cpp
59
+
#defineSTEPS_PER_REV 2048
60
+
```
61
+
62
+
Also, we have to tell the Arduino which pins are connected to the driver’s inputs. The order of the pins matters — follow the sequence exactly as wired in the circuit section.
63
+
64
+
```cpp
65
+
Stepper myStepper(STEPS_PER_REV, 11, 9, 10, 8);
66
+
```
67
+
68
+
We can then use the `setSpeed()` and the `step()` functions to make our stepper motor rotate. The completed example sketch is as follows:
69
+
70
+
```cpp
71
+
#include<Stepper.h>
72
+
73
+
#defineSTEPS_PER_REV 2048
74
+
75
+
Stepper myStepper(STEPS_PER_REV, 11, 9, 10, 8);
76
+
77
+
void setup() {
78
+
myStepper.setSpeed(10); // 10 RPM
79
+
}
80
+
81
+
void loop() {
82
+
myStepper.step(1024); // Half rotation forward
83
+
delay(1000); // Wait 1 second
84
+
myStepper.step(-1024); // Half rotation backward
85
+
delay(1000); // Wait 1 second
86
+
}
87
+
```
53
88
54
89
## Assignment
55
90
56
91
:::info Your Turn
57
-
1.
92
+
1. Connect a potentiometer to an analog input pin. Map the potentiometer value to a range between -2048 and 2048 steps using the `map()` function we learned about earlier. Move the stepper motor accordingly.
58
93
:::
59
94
60
95
## Next Steps
61
96
62
97
This section includes links to help you dive deeper into the topics from this lesson. It's optional, so don't worry if you choose to skip it.
63
98
64
-
-
99
+
- [Read this to understand why we need a motor driver.](https://orbray.com/magazine_en/archives/3659)
0 commit comments