Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO and L298N Motor Driver Based Bluetooth Controlled Car

Image of Arduino UNO and L298N Motor Driver Based Bluetooth Controlled Car

Circuit Documentation

Summary of the Circuit

This circuit is designed to control a car with two DC motors using an Arduino UNO microcontroller and an L298N DC motor driver. The Arduino UNO receives commands via Bluetooth (not explicitly shown in the parts list but implied by the code comments) and controls the motor driver, which in turn drives the motors attached to the wheels. The circuit also includes two LM393 modules, which could be used for sensing purposes, and a 2 x AA battery mount with a rocker switch to provide power to the system.

Component List

L298N DC Motor Driver

  • Description: A motor driver module capable of driving 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.

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0.

Motor and Wheels (x2)

  • Description: DC motors connected to wheels for the car's movement.
  • Pins: vcc, GND.

LM393 (x2)

  • Description: A dual differential comparator module.
  • Pins: VCC, GND, D0, A0.

2 x AA Battery Mount

  • Description: A battery holder for two AA batteries.
  • Pins: -, +.

Rocker Switch

  • Description: A switch to control the power supply to the circuit.
  • Pins: 1, 2.

Wiring Details

L298N DC Motor Driver

  • OUT1 connected to Motor and Wheels (Motor 1 Vcc)
  • OUT2 connected to Motor and Wheels (Motor 1 GND)
  • OUT3 connected to Motor and Wheels (Motor 2 GND)
  • OUT4 connected to Motor and Wheels (Motor 2 Vcc)
  • 12V connected to Rocker Switch Pin 2
  • GND connected to 2 x AA Battery Mount (-)
  • ENA connected to Arduino UNO Pin D11
  • IN1 connected to Arduino UNO Pin D10
  • IN2 connected to Arduino UNO Pin D9
  • IN3 connected to Arduino UNO Pin D6
  • IN4 connected to Arduino UNO Pin D5
  • ENB connected to Arduino UNO Pin D3

Arduino UNO

  • D11 connected to L298N DC Motor Driver ENA
  • D10 connected to L298N DC Motor Driver IN1
  • D9 connected to L298N DC Motor Driver IN2
  • D6 connected to L298N DC Motor Driver IN3
  • D5 connected to L298N DC Motor Driver IN4
  • D3 connected to L298N DC Motor Driver ENB
  • 5V connected to LM393 Modules VCC (both)
  • GND connected to LM393 Modules GND (both)
  • A0 connected to LM393 Module A0 (Module 2)
  • A1 connected to LM393 Module A0 (Module 1)
  • D4 connected to LM393 Module D0 (Module 1)
  • D2 connected to LM393 Module D0 (Module 2)

Motor and Wheels

  • Motor 1 Vcc connected to L298N DC Motor Driver OUT1
  • Motor 1 GND connected to L298N DC Motor Driver OUT2
  • Motor 2 GND connected to L298N DC Motor Driver OUT3
  • Motor 2 Vcc connected to L298N DC Motor Driver OUT4

LM393 Modules

  • VCC connected to Arduino UNO 5V
  • GND connected to Arduino UNO GND
  • Module 1 A0 connected to Arduino UNO A1
  • Module 1 D0 connected to Arduino UNO D4
  • Module 2 A0 connected to Arduino UNO A0
  • Module 2 D0 connected to Arduino UNO D2

2 x AA Battery Mount

  • (-) connected to L298N DC Motor Driver GND
  • (+) connected to Rocker Switch Pin 1

Rocker Switch

  • Pin 1 connected to 2 x AA Battery Mount (+)
  • Pin 2 connected to L298N DC Motor Driver 12V

Documented Code

//Arduino Bluetooth Controlled Car
//Before uploading the code you have to install the necessary library
//Note - Disconnect the Bluetooth Module before hitting the upload button otherwise you'll get compilation error message.
//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install 
//After downloading the library open Arduino IDE >> go to sketch >> Include Library >> ADD. ZIP Library >> Select the downloaded 
//ZIP File >> Open it >> Done
//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.   
    //Serial.println(command);
    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 control a four-wheel car using the Adafruit Motor Shield library. It listens for commands from a Bluetooth module and drives the motors accordingly to move