This circuit is designed to control two DC motors using an Arduino UNO microcontroller and an L298N DC motor driver. The Arduino UNO is responsible for generating control signals that are sent to the L298N driver, which in turn drives the motors. The motors can move forward or backward at varying speeds. A 12V battery provides the necessary power for the motors and the motor driver, while the Arduino is powered through its Vin pin.
// DEFINE motor driver pins
const int MotorPin1 = 9; // IN1
const int MotorPin2 = 8; // IN2
const int MotorPin3 = 7; // IN3
const int MotorPin4 = 6; // IN4
const int ENA = 10; // Enable A
const int ENB = 11; // Enable B
void setup() {
// Initialize pins for output
pinMode(MotorPin1, OUTPUT);
pinMode(MotorPin2, OUTPUT);
pinMode(MotorPin3, OUTPUT);
pinMode(MotorPin4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Test sequence
Serial.println("Beginning test, good luck");
// Move forward at half speed
moveForward(128);
delay(2000);
// Move backward at full speed
moveBackward(255);
delay(2000);
// Stop motor
stopMotor();
delay(1000);
}
void moveForward(int speed) {
// Set motor speed and direction for forward motion
analogWrite(ENA, speed);
analogWrite(ENB, speed);
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin2, LOW);
digitalWrite(MotorPin3, HIGH);
digitalWrite(MotorPin4, LOW);
Serial.println("Motor moving forward");
}
void moveBackward(int speed) {
// Set motor speed and direction for backward motion
analogWrite(ENA, speed);
analogWrite(ENB, speed);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin2, HIGH);
digitalWrite(MotorPin3, LOW);
digitalWrite(MotorPin4, HIGH);
Serial.println("Motor moving backward");
}
void stopMotor() {
// Stop motor by disabling ENA and ENB
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
Serial.println("Motor stopped");
}
This code is designed to control the speed and direction of two DC motors using the L298N motor driver. The moveForward
and moveBackward
functions control the direction of the motors by setting the appropriate pins high or low. The stopMotor
function stops the motors by setting the enable pins to low. The analogWrite
function is used to control the speed of the motors by varying the voltage on the enable pins.