Arduino UNO and L298N Motor Driver Controlled DC Motor with Speed and Direction Control
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a DC motor using an Arduino UNO and an L298N motor driver. The motor is connected to the L298N motor driver, which is powered by a 9V battery. The Arduino UNO is used to control the motor's speed and direction through the L298N motor driver.
Component List
L298N DC Motor Driver
- Description: A dual H-Bridge motor driver that allows control of the speed and direction of two DC motors.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
9V Battery
- Description: A standard 9V battery used to power the motor driver.
- Pins: -, +
Arduino UNO
- Description: A microcontroller board based on the ATmega328P, used to control the motor driver.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
DC Motor
- Description: A standard DC motor.
- Pins: pin 1, pin 2
Wiring Details
L298N DC Motor Driver
- GND connected to GND of the 9V Battery
- GND connected to GND of the Arduino UNO
- 5V connected to + of the 9V Battery
- OUT3 connected to pin 2 of the DC Motor
- OUT4 connected to pin 1 of the DC Motor
- IN3 connected to D5 of the Arduino UNO
- IN4 connected to D4 of the Arduino UNO
9V Battery
- - connected to GND of the Arduino UNO
- - connected to GND of the L298N DC Motor Driver
- + connected to 5V of the L298N DC Motor Driver
Arduino UNO
- GND connected to - of the 9V Battery
- GND connected to GND of the L298N DC Motor Driver
- D5 connected to IN3 of the L298N DC Motor Driver
- D4 connected to IN4 of the L298N DC Motor Driver
DC Motor
- pin 2 connected to OUT3 of the L298N DC Motor Driver
- pin 1 connected to OUT4 of the L298N DC Motor Driver
Code Documentation
Arduino Sketch
/*
* This Arduino sketch controls a DC motor using an L298N motor driver.
* The motor is connected to the OUT3 and OUT4 pins of the L298N driver.
* The motor will accelerate in the clockwise direction, then decelerate,
* and then accelerate in the counter-clockwise direction, and decelerate.
*/
const int IN3 = 5; // Pin connected to IN3 on L298N
const int IN4 = 4; // Pin connected to IN4 on L298N
void setup() {
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop() {
// Accelerate motor clockwise (CW)
for (int speed = 0; speed <= 255; speed += 5) {
analogWrite(IN3, speed);
analogWrite(IN4, 0);
delay(50);
}
// Decelerate motor clockwise (CW)
for (int speed = 255; speed >= 0; speed -= 5) {
analogWrite(IN3, speed);
analogWrite(IN4, 0);
delay(50);
}
delay(1000); // Stop for 1 second
// Accelerate motor counter-clockwise (CCW)
for (int speed = 0; speed <= 255; speed += 5) {
analogWrite(IN3, 0);
analogWrite(IN4, speed);
delay(50);
}
// Decelerate motor counter-clockwise (CCW)
for (int speed = 255; speed >= 0; speed -= 5) {
analogWrite(IN3, 0);
analogWrite(IN4, speed);
delay(50);
}
delay(1000); // Stop for 1 second
}
This code initializes the pins connected to the L298N motor driver and controls the speed and direction of the DC motor. The motor accelerates and decelerates in both clockwise and counter-clockwise directions.