Control a DC Motor with Arduino Uno


Materials





Arduino Uno Arduino Uno Buy at Amazon
Breadboard Breadboard Buy at Amazon
Jumper Wires Jumper Wires Buy at Amazon
DC Motor DC Motor Buy at Amazon
L298N Motor Driver Module L298N Motor Driver Module Buy at Amazon

Instructions

  1. Connect the DC motor to the L298N motor driver module.
  2. Wire the motor driver to the Arduino and the external power supply as per the module’s pinout.
  3. 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.

Leave a Comment

Introducing the logitech mk335 wireless keyboard and mouse combo.