Arduino-Controlled Bluetooth Robotic Car with L298N Motor Driver
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 car is powered by a 12V battery and features four motors with wheels. The motors are driven by an L298N DC motor driver, which allows for the control of speed and direction. The HC-05 Bluetooth module is used to receive commands wirelessly from a Bluetooth-enabled device, such as a smartphone. The Arduino UNO interprets these commands and controls the motors accordingly, enabling the car to move forward, backward, turn left, and turn right.
Component List
L298N DC Motor Driver
- Description: A motor driver module that can drive up to two DC motors.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
HC-05 Bluetooth Module
- Description: A Bluetooth module for wireless communication.
- Pins: EN, VCC, GND, TXD, RXD, STATE
12V Battery
- Description: A power source for the circuit.
- Pins: +, -
Motors and Wheels (4x)
- Description: DC motors connected to wheels for the car's movement.
- Pins: vcc, GND
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
Wiring Details
L298N DC Motor Driver
- 12V: Connected to the positive terminal of the 12V battery.
- GND: Connected to the GND of Arduino UNO and the negative terminal of the 12V battery.
- 5V: Connected to the Vin of Arduino UNO.
- OUT1, OUT2: Connected to the vcc and GND of two motors and wheels, respectively.
- OUT3, OUT4: Connected to the vcc and GND of the other two motors and wheels, respectively.
- ENA, ENB: Connected to D6 and D5 of Arduino UNO for enabling motor control.
- IN1, IN2, IN3, IN4: Connected to D4, D7, D8, and D9 of Arduino UNO for motor direction control.
HC-05 Bluetooth Module
- VCC: Connected to the 5V of Arduino UNO.
- GND: Connected to the GND of Arduino UNO.
- TXD: Connected to D0 (RX) of Arduino UNO.
- RXD: Connected to D1 (TX) of Arduino UNO.
12V Battery
- +: Connected to the 12V input of the L298N DC motor driver.
- -: Connected to the GND of the L298N DC motor driver.
Motors and Wheels
- vcc: Connected to OUT1 and OUT4 of the L298N DC motor driver for two motors, and to OUT2 and OUT3 for the other two motors.
- GND: Connected to OUT2 and OUT3 of the L298N DC motor driver for two motors, and to OUT1 and OUT4 for the other two motors.
Arduino UNO
- GND: Connected to the GND of the L298N DC motor driver and the HC-05 Bluetooth module.
- Vin: Connected to the 5V of the L298N DC motor driver.
- D0-D9: Connected to the HC-05 Bluetooth module and the L298N DC motor driver for control signals.
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 be uploaded to an Arduino UNO microcontroller. It includes a library for motor control and defines functions for moving the car forward, backward, left, and right based on commands received via Bluetooth. The Stop function is used to stop the motors. The Serial object is used for Bluetooth communication, and the motors are controlled using the AFMotor library.