Loading...

Arduino Bluetooth Car Code


Hello everyone! Welcome to Arduino Geek. Today we will discuss about the Arduino code for bluetooth controlled car. So let's get started.

Arduino Bluetooth Car Code -

Here is a sample code for controlling a car with an HC05 Bluetooth module using an Arduino:

Arduino Code - 

#include <SoftwareSerial.h>  // include the software serial library
SoftwareSerial BTSerial(2, 3);  // create a software serial port for the HC05 Bluetooth module

int leftMotor1 = 4;  // set the pins for the left motor
int leftMotor2 = 5;
int rightMotor1 = 6;  // set the pins for the right motor
int rightMotor2 = 7;

void setup() {
  pinMode(leftMotor1, OUTPUT);  // set the motor pins as outputs
  pinMode(leftMotor2, OUTPUT);
  pinMode(rightMotor1, OUTPUT);
  pinMode(rightMotor2, OUTPUT);
  
  Serial.begin(9600);  // initialize the serial port
  BTSerial.begin(9600);  // initialize the Bluetooth serial port
}

void loop() {
  if (BTSerial.available()) {  // check if there is data available from the Bluetooth module
    char data = BTSerial.read();  // read the incoming data
    
    switch(data) {
      case 'F':  // move forward
        digitalWrite(leftMotor1, HIGH);
        digitalWrite(leftMotor2, LOW);
        digitalWrite(rightMotor1, HIGH);
        digitalWrite(rightMotor2, LOW);
        break;
      case 'B':  // move backward
        digitalWrite(leftMotor1, LOW);
        digitalWrite(leftMotor2, HIGH);
        digitalWrite(rightMotor1, LOW);
        digitalWrite(rightMotor2, HIGH);
        break;
      case 'L':  // turn left
        digitalWrite(leftMotor1, LOW);
        digitalWrite(leftMotor2, HIGH);
        digitalWrite(rightMotor1, HIGH);
        digitalWrite(rightMotor2, LOW);
        break;
      case 'R':  // turn right
        digitalWrite(leftMotor1, HIGH);
        digitalWrite(leftMotor2, LOW);
        digitalWrite(rightMotor1, LOW);
        digitalWrite(rightMotor2, HIGH);
        break;
      case 'S':  // stop
        digitalWrite(leftMotor1, LOW);
        digitalWrite(leftMotor2, LOW);
        digitalWrite(rightMotor1, LOW);
        digitalWrite(rightMotor2, LOW);
        break;
    }
  }
}

Arduino Code Description - 

This code sets up a software serial port for the HC05 Bluetooth module on pins 2 and 3. It also sets up four output pins for the two motors on the car. In the loop() function, the code checks if there is data available from the Bluetooth module. If there is, it reads the incoming data and performs a certain action based on the received character:

'F': move forward
'B': move backward
'L': turn left
'R': turn right
'S': stop
To use this code, you will need to connect the HC05 Bluetooth module to pins 2 and 3 on your Arduino board, and connect the left and right motors to the corresponding output pins (leftMotor1, leftMotor2, rightMotor1, and rightMotor2). You will also need to pair your Bluetooth device with the HC05 module to send commands to the car.
Once the connections are made and the code is uploaded to the Arduino board, you can use a Bluetooth terminal app on your phone or computer to send the characters 'F', 'B', 'L', 'R', or 'S' to control the car. For example, sending 'F' will make the car move forward, while sending 'S' will stop the car.
Note that you will need to pair your HC05 module with your device before you can send commands to the Arduino. The default pairing code is usually "1234" or "0000".

 
close