Materials
Arduino Uno Buy at Amazon | |
Breadboard Buy at Amazon | |
Jumper Wires Buy at Amazon | |
DC Motor Buy at Amazon | |
L298N Motor Driver Module Buy at Amazon |
Instructions
- Connect the DC motor to the L298N motor driver module.
- Wire the motor driver to the Arduino and the external power supply as per the module’s pinout.
- Upload the following code to your Arduino:
#define MOTOR_PIN1 3
#define MOTOR_PIN2 4
void setup() {
pinMode(MOTOR_PIN1, OUTPUT);
pinMode(MOTOR_PIN2, OUTPUT);
}
void loop() {
digitalWrite(MOTOR_PIN1, HIGH);
digitalWrite(MOTOR_PIN2, LOW);
delay(1000);
digitalWrite(MOTOR_PIN1, LOW);
digitalWrite(MOTOR_PIN2, HIGH);
delay(1000);
}
Explanation
This code alternates the direction of the DC motor by switching the state of the pins connected to the motor driver. The motor will spin in one direction for 1 second and then reverse direction for another second.
Conclusion
With this setup, you can control the direction of a DC motor using an Arduino Uno. This project is a great starting point for more complex motor control tasks.