This circuit is designed to control a car via Bluetooth using an Arduino UNO microcontroller. The Arduino UNO interfaces with an L298N DC motor driver to control the direction and speed of the motors attached to the wheels. The circuit also includes two LM393 modules, which could be used for sensing applications, and a power supply consisting of a 2 x AA battery mount with a rocker switch for power control.
5V
connected to the VCC of both LM393 modules.GND
connected to the GND of both LM393 modules.D11
connected to ENA on the L298N motor driver.D10
connected to IN1 on the L298N motor driver.D9
connected to IN2 on the L298N motor driver.D6
connected to IN3 on the L298N motor driver.D5
connected to IN4 on the L298N motor driver.D3
connected to ENB on the L298N motor driver.A0
connected to A0 on the first LM393 module.A1
connected to A0 on the second LM393 module.D4
connected to D0 on the second LM393 module.D2
connected to D0 on the first LM393 module.OUT1
and OUT2
connected to the first motor and wheels.OUT3
and OUT4
connected to the second motor and wheels.12V
connected to the 2
pin on the Rocker Switch.GND
connected to the -
pin on the 2 x AA Battery Mount.vcc
and GND
, which are connected to the corresponding OUT pins on the L298N motor driver.VCC
connected to 5V
on the Arduino UNO.GND
connected to GND
on the Arduino UNO.A0
connected to analog pins A0
and A1
on the Arduino UNO for the first and second modules, respectively.D0
connected to digital pins D2
and D4
on the Arduino UNO for the first and second modules, respectively.+
connected to the 1
pin on the Rocker Switch.-
connected to GND
on the L298N motor driver.1
connected to the +
pin on the 2 x AA Battery Mount.2
connected to the 12V
pin on the L298N motor driver.//Arduino Bluetooth Controlled Car
//Before uploading the code you have to install the necessary library
//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install
//Now You Can Upload the Code without any problem but make sure the bt module isn't connected with Arduino while uploading code
#include <AFMotor.h>
//initial motors pin
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 to your Bluetooth module.
}
void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop(); //initialize with motors stopped
//Change pin mode only if new command is different from previous.
switch(command){
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
}
}
}
void forward()
{
motor1.setSpeed(255); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(255);//Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(255);//Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void back()
{
motor1.setSpeed(255); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor anti-clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(255); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
void left()
{
motor1.setSpeed(255); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor anti-clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(255); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void right()
{
motor1.setSpeed(255); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(255); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
void Stop()
{
motor1.setSpeed(0); //Define minimum velocity
motor1.run(RELEASE); //stop the motor when release the button
motor2.setSpeed(0); //Define minimum velocity
motor2.run(RELEASE); //stop the motor when release the button
motor3.setSpeed(0); //Define minimum velocity
motor3.run(RELEASE); //stop the motor when release the button
motor4.setSpeed(0); //Define minimum velocity
motor4.run(RELEASE); //stop the motor when release the button
}
This code is designed to receive commands via Bluetooth and control the motors accordingly to move the car forward, backward, left, or right. The AFMotor
library is used to interface with the motor driver, and the speed of each motor is set to its maximum value when moving. The Stop
function stops all motors by setting their speed to zero and releasing them.