Controlling a Stepper Motor with Arduino Uno
Welcome back to Maker Tech Lab! In this tutorial, we will show you how to control a stepper motor using an Arduino Uno. You’ll learn how to make precise movements using a stepper motor driver.
Materials Needed
- Arduino Uno
- Stepper motor
- Stepper motor driver (e.g., A4988)
- Power supply (appropriate for your stepper motor)
- Breadboard
- Jumper wires
Circuit Diagram
Let’s set up the circuit. Follow the diagram below:
- Connect the Stepper Motor Driver: Connect the stepper motor driver to the stepper motor. The driver typically has pins for power (VCC, GND), motor connections (A+, A-, B+, B-), and control pins (STEP, DIR).
- Connect the Driver to Arduino: Connect the STEP pin of the driver to a digital pin on the Arduino (e.g., pin 9) and the DIR pin to another digital pin (e.g., pin 8). Connect the GND of the driver to the GND of the Arduino.
- Connect the Power Supply: Connect the power supply to the VCC and GND of the driver. Make sure the voltage matches the requirements of your stepper motor.
The Code
Let’s move on to the code. We’ll use the Arduino IDE to write a program that controls the stepper motor’s rotation.
[dm_code_snippet]const int stepPin = 9; // Pin for the STEP signal
const int dirPin = 8; // Pin for the DIR signal
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// Rotate the stepper motor clockwise
digitalWrite(dirPin, HIGH);
for (int i = 0; i < 200; i++) { // Number of steps (e.g., 200 for one full rotation)
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000); // Adjust delay for speed
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Wait for 1 second
// Rotate the stepper motor counterclockwise
digitalWrite(dirPin, LOW);
for (int i = 0; i < 200; i++) { // Number of steps (e.g., 200 for one full rotation)
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000); // Adjust delay for speed
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Wait for 1 second
}
[/dm_code_snippet]
Explanation
- Pin Setup: We define the pins where the STEP and DIR signals are connected.
- Setup Function: We initialize the STEP and DIR pins as outputs.
- Loop Function: This function rotates the stepper motor clockwise and counterclockwise. It performs 200 steps for one full rotation with a delay that controls the speed.
Uploading the Code
- Open the Arduino IDE.
- Copy and paste the code into the IDE.
- Select the correct board and port from the Tools menu.
- Click the upload button.
Conclusion
That’s it! You’ve successfully created a project that controls a stepper motor using an Arduino Uno. This project introduces stepper motor control, which is essential for precise movements in robotics and automation projects. Stay tuned for more tutorials and exciting projects at Maker Tech Lab!
Affiliate Links
Here are some recommended components for this project:
- Arduino Uno
- Stepper motor
- Stepper motor driver (A4988)
- Power supply
- Breadboard and jumper wires
Happy making!