Bluetooth-Controlled Robotic Vehicle

Hardware Logic & Serial Comm
Bluetooth-controlled robotic vehicle

Built a Bluetooth-controlled robotic vehicle using Arduino hardware to receive directional commands from a smartphone controller app. Integrated an HC-SR04 ultrasonic sensor to detect obstacles and override movement commands for collision prevention. Motor control was handled through an L298N motor driver.

Control Flow

The system relies on Serial (UART) communication. A smartphone application transmits single-character payloads over a Bluetooth link. The microcontroller parses these characters sequentially while running a non-blocking sensor check in its main loop.

Phone App (Generic Controller)
Bluetooth Command (F/B/L/R/S)
Arduino Serial Input Buffer
Movement Logic + Distance Check
L298N Motor Driver
DC Motors
Sensor Loop: HC-SR04 → Pulse Calculation → Collision Override

Firmware

The core logic ensures that the ultrasonic sensor acts as a hard safety override. If the distance falls below 20cm, the robot will refuse to execute a 'Forward' (F) command, protecting the hardware from collision.

main.ino
#define IN1 5 #define IN2 6 #define IN3 9 #define IN4 10 #define TRIG 7 #define ECHO 8 char command; long getDistance() { digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); long duration = pulseIn(ECHO, HIGH); long distance = duration * 0.034 / 2; return distance; } void setup() { Serial.begin(9600); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); } void stopMotors() { digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); } void forward() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } void loop() { if (Serial.available()) { command = Serial.read(); } long distance = getDistance(); // Collision Prevention Override if(distance < 20 && command == 'F') { stopMotors(); } else { if(command == 'F') forward(); else if(command == 'S') stopMotors(); } }
View Firmware