Skip to content

Commit bf89632

Browse files
committed
Complete stepper motor lesson base
1 parent c7b20dd commit bf89632

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

docs/week-5/img/stepper-wiring.png

365 KB
Loading

docs/week-5/stepper-motor.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,51 @@ Components required:
4949
</Tabs>
5050
:::
5151

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+
#define STEPS_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+
#define STEPS_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+
```
5388
5489
## Assignment
5590
5691
:::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.
5893
:::
5994
6095
## Next Steps
6196
6297
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.
6398
64-
-
99+
- [Read this to understand why we need a motor driver.](https://orbray.com/magazine_en/archives/3659)

0 commit comments

Comments
 (0)