AR-189ArduinoBeginner

Arduino Uno + HC-05 Bluetooth Car (L298N)

Drive a two-wheel car over Bluetooth with the joystick on your phone; it is built with an Arduino Uno, HC-05, L298N and two motors, no internet needed. Android only: iPhones can't connect to the HC-05, use an HM-10 for iPhone. Pair the HC-05 in your phone's Bluetooth settings, then go to Workshop › Devices › Add a device › Bluetooth, pick the Arduino Uno + HC-05/06 board and Car remote, and upload the code from Get the code.

0.0(0)
Arduino Uno + HC-05 Bluetooth Car (L298N)
If you have an Arduino Uno and an HC-05 module, you can turn your phone into the remote for a car you built yourself. The HC-05 uses classic Bluetooth, so this project works on Android only: iPhones can't connect to the HC-05, so use an HM-10 for iPhone. The module goes on D2 and D3; its RXD pin wants 3.3 V, so a 1 kΩ and 2 kΩ voltage divider sits between it and D3. D0 and D1 stay free, so you don't have to unplug the module while you upload. First pair your phone with the HC-05 in its Bluetooth settings (the PIN is usually 1234 or 0000). Then add a new Bluetooth device in the app, pick Arduino Uno + HC-05/06 as the board and the Car remote template, upload the code from Get the code with the Arduino IDE, then choose your HC-05 in the list and connect. Drag the joystick and the car drives, let go and it stops; there are arrow keys and a speed slider too. The Set up as a Bluetooth panel button on this page opens the wizard with this template. HC-05 support comes with the new app version, so update the app if you can't see the option. Learning: receiving commands over classic Bluetooth with SoftwareSerial, motor direction and PWM speed with the L298N, joystick mixing and a 500 ms safety stop.

Video

Circuit Diagram

Arduino Uno + HC-05 Bluetooth Car (L298N) - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ┌────────────────────────────────────┐
2// │ BlueGrays · bluegrays.com          │
3// │ Bluetooth · Arduino Uno + HC-05/06 │
4// │ BG-Araba                           │
5// └────────────────────────────────────┘
6// Car remote
7// Arduino IDE board: Arduino Uno
8// HC-05/06 is classic Bluetooth: Android only, iPhones can't connect. Its name is changed in AT mode (optional).
9
10#include <SoftwareSerial.h>
11
12const char BLE_NAME[] = "BG-Araba";  // the board advertises with this name, the app finds it in the list by it
13
14// Pins: change them here if you wired differently
15const int ENA = 5, IN1 = 7, IN2 = 8;  // Motor A (ENA, IN1, IN2) is the left wheel, motor B (ENB, IN3, IN4) the right one
16const int ENB = 6, IN3 = 9, IN4 = 10;
17const unsigned long COMMAND_TIMEOUT_MS = 500;  // Safety: the motors stop if no command arrives for 500 ms (the app repeats it while you drive)
18
19void btWrite(const char *text);
20void onValue(const char *key, const char *value);
21void sendAll();
22void onLinkLost();
23
24// ── The app sends "key=value" lines; on "?" the board sends all its values ──
25char line[65];
26uint8_t lineLen = 0;
27unsigned long lastRxMs = 0;
28
29bool is(const char *a, const char *b) { return strcmp(a, b) == 0; }
30bool isOn(const char *v) { return v[0] == '1'; }
31
32void sendText(const char *key, const char *value) {
33  char out[72];
34  snprintf(out, sizeof(out), "%s=%s\n", key, value);
35  btWrite(out);
36}
37
38void sendInt(const char *key, long value) {
39  char text[12];
40  snprintf(text, sizeof(text), "%ld", value);
41  sendText(key, text);
42}
43
44void sendFloat(const char *key, float value) {
45  char text[16];
46  dtostrf(value, 1, 1, text);
47  sendText(key, text);
48}
49
50void handleLine(char *text) {
51  char *eq = strchr(text, '=');
52  if (eq == nullptr) {
53    if (text[strlen(text) - 1] == '?') {
54      btWrite("@v=1.0\n");  // the "@v" line tells the app the code version
55      sendAll();
56    }
57    return;
58  }
59  *eq = '\0';
60  onValue(text, eq + 1);
61}
62
63void feed(char c) {
64  lastRxMs = millis();
65  if (c == '\n' || c == '\r') {
66    if (lineLen == 0) return;
67    line[lineLen] = '\0';
68    lineLen = 0;
69    handleLine(line);
70  } else if (lineLen < sizeof(line) - 1) {
71    line[lineLen++] = c;
72  }
73}
74
75// ── Bluetooth (SoftwareSerial) ──
76SoftwareSerial bt(2, 3);  // module TXD → D2, RXD → D3 (through a 1k/2k divider), 9600 baud
77
78void btWrite(const char *text) { bt.print(text); }
79
80void btBegin() { bt.begin(9600); }
81
82void btPoll() {
83  while (bt.available()) feed(bt.read());
84}
85
86// ── Arduino Uno + HC-05/06 ──
87int speedLimit = 80;  // speed limit from the app (0–100 %)
88unsigned long lastCommandMs = 0;
89bool moving = false;
90
91// if the car drives backwards, swap that motor's two wires
92void motor(int en, int a, int b, int power) {
93  power = constrain(power, -100, 100);
94  digitalWrite(a, power > 0 ? HIGH : LOW);
95  digitalWrite(b, power < 0 ? HIGH : LOW);
96  analogWrite(en, abs(power) * speedLimit * 255L / 10000);
97}
98
99void drive(int left, int right) {
100  motor(ENA, IN1, IN2, left);
101  motor(ENB, IN3, IN4, right);
102  moving = left != 0 || right != 0;
103  lastCommandMs = millis();
104}
105
106void onValue(const char *key, const char *value) {
107  if (is(key, "joy")) {  // Joystick: y is forward/back, x turns; mixed into left and right wheel speeds
108    const char *comma = strchr(value, ',');
109    int x = atoi(value), y = comma ? atoi(comma + 1) : 0;
110    drive(constrain(y + x, -100, 100), constrain(y - x, -100, 100));
111  } else if (is(key, "yon")) {
112    if (is(value, "ileri")) drive(100, 100);
113    else if (is(value, "geri")) drive(-100, -100);
114    else if (is(value, "sol")) drive(-70, 70);
115    else if (is(value, "sag")) drive(70, -70);
116    else drive(0, 0);
117  } else if (is(key, "hiz")) {
118    speedLimit = constrain(atoi(value), 0, 100);
119  }
120}
121
122void sendAll() {
123  sendInt("hiz", speedLimit);
124}
125
126void onLinkLost() {
127  drive(0, 0);  // Connection lost: the motors stop
128}
129
130void setup() {
131  Serial.begin(115200);
132  int pins[] = { ENA, IN1, IN2, ENB, IN3, IN4 };
133  for (int pin : pins) pinMode(pin, OUTPUT);
134  drive(0, 0);
135  btBegin();
136  Serial.print(F("BlueGrays Bluetooth: "));
137  Serial.println(BLE_NAME);
138}
139
140void loop() {
141  btPoll();
142  if (moving && millis() - lastCommandMs > COMMAND_TIMEOUT_MS) drive(0, 0);
143}
144

FAQ

Does it work with an iPhone?

No, this project works on Android only. The HC-05 uses classic Bluetooth, and iPhones can't connect to classic Bluetooth modules. For iPhone, use an HM-10: in the app you pick Arduino Uno + HM-10 as the board. The Bluetooth car guide built with an ESP32, which has Bluetooth built in, also works on iPhone. On Android, first pair the HC-05 in your phone's Bluetooth settings (the PIN is usually 1234 or 0000), then connect from the app.

How is it different from the older L298N Bluetooth car project?

In the older project #80, the L298N Bluetooth controlled car (HC-05, 4 motors), the car took one-letter commands such as F, B, L and R from a third-party remote app. This project works with BlueGrays' own Bluetooth panel: smooth turns with the joystick, a speed limit slider and a safety stop when the commands stop. The app prepares the code for the board you pick.

How do I power the Uno? Can USB and the battery be connected at the same time?

The 2× 18650 batteries (7.4 V) go to the L298N's 12V input, and the L298N's 5V output feeds the Uno's 5V pin; for that the 5V regulator jumper on the L298N must stay on. All GNDs must be connected. If USB is plugged in while you upload, switch the battery off or pull the 5V wire coming from the L298N, or the two supplies will clash. The HC-05 sits on D2 and D3, so you don't have to unplug it while you upload.

What is the range? What does the car do if the connection drops?

The HC-05 reaches about 10 metres in the open; walls and people in the way shorten that. While you drive, the app keeps repeating the command, and if the board gets no command for 500 ms it stops the motors. The HC-05 doesn't tell the Uno when the connection drops; thanks to this safety timeout, the car stops within half a second if the link is lost. To reconnect, pick your HC-05 in the app again.