RoboticsA Car That Avoids Obstacles
A two-motor robot car drives forward, spots a wall from afar with its ultrasonic “eye”, stops, looks left and right and turns where the way is clear.
RoboticsAn HC-05 Bluetooth module links the car to your phone. Drive it forward and back and turn it left and right with the buttons of an Android app, and change its speed too.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
Wire the chassis, motors, L298N and power the same way as in “A Car That Avoids Obstacles”: ENA 5, IN1 7, IN2 8, ENB 6, IN3 4, IN4 2.
Connect the HC-05's VCC to 5V and GND to ground. The module's TX goes to the board's RX, and the module's RX to the board's TX through a voltage divider: a 1 kΩ resistor from the board's pin to the HC-05's RX, and a 2 kΩ from there to ground. That way the module gets about 3.3 V instead of 5 V.
On the Uno R3 the board's RX is pin 10 and TX pin 11; on the Uno R4 RX is pin 0 and TX pin 1; on the Mega RX1 is pin 19 and TX1 pin 18. The code recognises the board by itself. The HC-05 sends a 3.3 V signal: enough for the Uno R3 and the Mega, but the Uno R4 may not read it. If no commands arrive on the R4, put a logic level shifter between the HC-05's TX and pin 0. No 2 kΩ resistor in your kit? Use two 1 kΩ in series.
Commands are single letters: F forward, B back, L left, R right, S stop, and the digits 0 to 9 set the speed.
// A car driven from an Android app through an HC-05 Bluetooth module.
#if defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_UNOR4_MINIMA) || defined(ARDUINO_AVR_MEGA2560)
#define BT Serial1 // a spare hardware port: pins 0/1 on the R4, 19/18 on the Mega
#else
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11); // Uno R3: RX = 10, TX = 11
#endif
// L298N: take the jumpers off ENA and ENB and wire them to PWM pins
const int ENA = 5, IN1 = 7, IN2 = 8; // left motor
const int ENB = 6, IN3 = 4, IN4 = 2; // right motor
int speed = 200; // 0-255
void drive(int left, int right) { // -255..255 for each side
digitalWrite(IN1, left > 0 ? HIGH : LOW);
digitalWrite(IN2, left < 0 ? HIGH : LOW);
digitalWrite(IN3, right > 0 ? HIGH : LOW);
digitalWrite(IN4, right < 0 ? HIGH : LOW);
analogWrite(ENA, abs(left));
analogWrite(ENB, abs(right));
}
void setup() {
int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4};
for (int pin : pins) pinMode(pin, OUTPUT);
BT.begin(9600); // the HC-05's factory speed
}
void loop() {
if (!BT.available()) return;
char c = BT.read();
int slow = speed / 3;
switch (c) {
case 'F': drive(speed, speed); break; // forward
case 'B': drive(-speed, -speed); break; // back
case 'L': drive(-speed, speed); break; // spin left
case 'R': drive(speed, -speed); break; // spin right
case 'G': drive(slow, speed); break; // forward left
case 'I': drive(speed, slow); break; // forward right
case 'H': drive(-slow, -speed); break; // back left
case 'J': drive(-speed, -slow); break; // back right
case 'S': drive(0, 0); break; // stop
case 'q': speed = 255; break; // full speed
default:
if (c >= '0' && c <= '9') speed = 100 + (c - '0') * 15; // 100..235
}
}Turn on Bluetooth in Android's settings and add HC-05; the PIN is 1234 or 0000. Install a Bluetooth car control app from Google Play, for example “Arduino Bluetooth RC Car”, and choose HC-05 in it. An iPhone can't see the HC-05: it needs a BLE module (HM-10).

Illustration, made with AI
Illustration: on a terrace, a phone with arrow buttons in hand, and a robot car with an HC-05 Bluetooth module nearby.
Step 5: Pair your phone
RoboticsA two-motor robot car drives forward, spots a wall from afar with its ultrasonic “eye”, stops, looks left and right and turns where the way is clear.
RoboticsTwo infrared sensors see a black line on the floor and the car follows it on its own, round the bends too. On a wide black strip it stops, as if at a finish line.
NewRoboticsFive infrared sensors measure how far the robot has drifted off the line, and a PID controller corrects the motors smoothly and fast. Built for competitions: the track, the sensor bar, the code and how to tune it.