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 librarySoftwareSerial BTSerial(2, 3); // create a software serial port for the HC05 Bluetooth moduleint leftMotor1 = 4; // set the pins for the left motorint leftMotor2 = 5;int rightMotor1 = 6; // set the pins for the right motorint rightMotor2 = 7;void setup() {pinMode(leftMotor1, OUTPUT); // set the motor pins as outputspinMode(leftMotor2, OUTPUT);pinMode(rightMotor1, OUTPUT);pinMode(rightMotor2, OUTPUT);Serial.begin(9600); // initialize the serial portBTSerial.begin(9600); // initialize the Bluetooth serial port}void loop() {if (BTSerial.available()) { // check if there is data available from the Bluetooth modulechar data = BTSerial.read(); // read the incoming dataswitch(data) {case 'F': // move forwarddigitalWrite(leftMotor1, HIGH);digitalWrite(leftMotor2, LOW);digitalWrite(rightMotor1, HIGH);digitalWrite(rightMotor2, LOW);break;case 'B': // move backwarddigitalWrite(leftMotor1, LOW);digitalWrite(leftMotor2, HIGH);digitalWrite(rightMotor1, LOW);digitalWrite(rightMotor2, HIGH);break;case 'L': // turn leftdigitalWrite(leftMotor1, LOW);digitalWrite(leftMotor2, HIGH);digitalWrite(rightMotor1, HIGH);digitalWrite(rightMotor2, LOW);break;case 'R': // turn rightdigitalWrite(leftMotor1, HIGH);digitalWrite(leftMotor2, LOW);digitalWrite(rightMotor1, LOW);digitalWrite(rightMotor2, HIGH);break;case 'S': // stopdigitalWrite(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".