This circuit is designed to control two DC motors using an L298N DC motor driver module, which is interfaced with an Arduino UNO microcontroller. The circuit includes pushbuttons for directional control of the motors, allowing for forward, backward, left, and right movements. A 9V battery provides the power source for the motor driver, while the Arduino UNO is powered through its 5V pin connected to the motor driver's 5V output.
// Define pins for L298N connections
const int ENA = 6; // Enable pin for Motor A
const int IN1 = 2; // Input 1 for Motor A
const int IN2 = 3; // Input 2 for Motor A
const int IN3 = 4; // Input 1 for Motor B
const int IN4 = 5; // Input 2 for Motor B
const int ENB = 7; // Enable pin for Motor B
// Define pins for push buttons
const int forwardButton = 8;
const int backwardButton = 9;
const int leftButton = 10;
const int rightButton = 11;
void setup() {
// Set motor control pins as output
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
// Set button pins as input with pull-up resistors
pinMode(forwardButton, INPUT_PULLUP);
pinMode(backwardButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
}
void loop() {
// Read the state of each button
bool forward = digitalRead(forwardButton) == LOW;
bool backward = digitalRead(backwardButton) == LOW;
bool left = digitalRead(leftButton) == LOW;
bool right = digitalRead(rightButton) == LOW;
// Forward
if (forward) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, 255); // Full speed
analogWrite(ENB, 255); // Full speed
}
// Backward
else if (backward) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA, 255);
analogWrite(ENB, 255);
}
// Left
else if (left) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, 255);
analogWrite(ENB, 255);
}
// Right
else if (right) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA, 255);
analogWrite(ENB, 255);
}
// Stop if no buttons are pressed
else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
}
This code is designed to run on the Arduino UNO microcontroller. It sets up the pins connected to the L298N motor driver and the pushbuttons. In the loop
function, it reads the state of the pushbuttons and controls the motors accordingly. The motors can be driven forward, backward, turned left, or right based on the button pressed. If no buttons are pressed, the motors are stopped.