Arduino UNO Controlled Robotic Car with L298N Motor Driver and LM393 Sensors
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
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.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
L298N DC Motor Driver
- A high-power motor driver module capable of driving up to two DC motors.
- It has inputs for motor control signals and power supply connections.
Motors and Wheels (x2)
- DC motors connected to wheels for providing motion to the car.
LM393 Modules (x2)
- Voltage comparator modules that can be used for various sensing applications.
2 x AA Battery Mount
- A battery holder for two AA batteries, providing power to the circuit.
Rocker Switch
- A switch to control the power supply to the circuit.
Wiring Details
Arduino UNO
5Vconnected to the VCC of both LM393 modules.GNDconnected to the GND of both LM393 modules.D11connected to ENA on the L298N motor driver.D10connected to IN1 on the L298N motor driver.D9connected to IN2 on the L298N motor driver.D6connected to IN3 on the L298N motor driver.D5connected to IN4 on the L298N motor driver.D3connected to ENB on the L298N motor driver.A0connected to A0 on the first LM393 module.A1connected to A0 on the second LM393 module.D4connected to D0 on the second LM393 module.D2connected to D0 on the first LM393 module.
L298N DC Motor Driver
OUT1andOUT2connected to the first motor and wheels.OUT3andOUT4connected to the second motor and wheels.12Vconnected to the2pin on the Rocker Switch.GNDconnected to the-pin on the 2 x AA Battery Mount.
Motors and Wheels
- Each motor has two connections,
vccandGND, which are connected to the corresponding OUT pins on the L298N motor driver.
LM393 Modules
VCCconnected to5Von the Arduino UNO.GNDconnected toGNDon the Arduino UNO.A0connected to analog pinsA0andA1on the Arduino UNO for the first and second modules, respectively.D0connected to digital pinsD2andD4on the Arduino UNO for the first and second modules, respectively.
2 x AA Battery Mount
+connected to the1pin on the Rocker Switch.-connected toGNDon the L298N motor driver.
Rocker Switch
1connected to the+pin on the 2 x AA Battery Mount.2connected to the12Vpin on the L298N motor driver.
Documented Code
//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.