The circuit in question is designed to control a four-wheeled vehicle using an Arduino UNO microcontroller and an L298N DC motor driver. The vehicle is equipped with four motors and wheels, and it receives commands via a Bluetooth module (HC-05) to move forward, backward, turn left, and turn right. The Arduino UNO is programmed to interpret the Bluetooth commands and drive the motors accordingly. The power for the motor driver is supplied by a 12V battery, which also powers the Arduino through a voltage regulator.
#include <AFMotor.h>
// Initialize motors
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
char command;
void setup()
{
Serial.begin(9600); // Set the baud rate for Bluetooth module communication.
}
void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop(); // Initialize with motors stopped
// Execute command received via Bluetooth
switch(command){
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
}
}
}
void forward()
{
// Set motors to move forward at maximum speed
motor1.setSpeed(255);
motor1.run(FORWARD);
motor2.setSpeed(255);
motor2.run(FORWARD);
motor3.setSpeed(255);
motor3.run(FORWARD);
motor4.setSpeed(255);
motor4.run(FORWARD);
}
void back()
{
// Set motors to move backward at maximum speed
motor1.setSpeed(255);
motor1.run(BACKWARD);
motor2.setSpeed(255);
motor2.run(BACKWARD);
motor3.setSpeed(255);
motor3.run(BACKWARD);
motor4.setSpeed(255);
motor4.run(BACKWARD);
}
void left()
{
// Set motors to turn left
motor1.setSpeed(255);
motor1.run(BACKWARD);
motor2.setSpeed(255);
motor2.run(BACKWARD);
motor3.setSpeed(255);
motor3.run(FORWARD);
motor4.setSpeed(255);
motor4.run(FORWARD);
}
void right()
{
// Set motors to turn right
motor1.setSpeed(255);
motor1.run(FORWARD);
motor2.setSpeed(255);
motor2.run(FORWARD);
motor3.setSpeed(255);
motor3.run(BACKWARD);
motor4.setSpeed(255);
motor4.run(BACKWARD);
}
void Stop()
{
// Stop all motors
motor1.setSpeed(0);
motor1.run(RELEASE);
motor2.setSpeed(0);
motor2.run(RELEASE);
motor3.setSpeed(0);
motor3.run(RELEASE);
motor4.setSpeed(0);
motor4.run(RELEASE);
}
This code is designed to be uploaded to an Arduino UNO microcontroller. It includes a setup function to initialize serial communication for the HC-05 Bluetooth module and a loop function that listens for incoming commands to control the motors. The commands 'F', 'B', 'L', and 'R' correspond to forward, backward, left, and right movements, respectively. The motor control functions set the speed and direction of the motors to achieve the desired movement.