Want to switch on a light or drive a robot car from your phone? The simplest way with an Arduino is the HC-05 module: it acts as a wireless serial link. Whatever you send from the phone reaches the Arduino as if it came from the Serial Monitor.
Wiring
| HC-05 | Arduino Uno |
|---|---|
| VCC | 5V (the module has a regulator) |
| GND | GND |
| TXD | 10 (RX in SoftwareSerial) |
| RXD | 11 (TX) through a voltage divider |
Why the divider? The module's RXD pin is 3.3 V. The Arduino's TX outputs 5. Put 1k from the Arduino pin to the module, and 2k from the module pin to ground. See level shifting.
Why not pins 0 and 1? Those are the USB pins. If the module is on them, uploads will fail. That's why we use SoftwareSerial on two other pins.
And watch the crossover: the module's TX goes to the Arduino's RX, and vice versa. It's the most common mistake of all.
The code
#include <SoftwareSerial.h>
SoftwareSerial bt(10, 11); // RX, TX
void setup() {
pinMode(13, OUTPUT);
bt.begin(9600); // the HC-05's default speed
}
void loop() {
if (bt.available()) {
char c = bt.read();
if (c == '1') digitalWrite(13, HIGH);
if (c == '0') digitalWrite(13, LOW);
}
}
The limits of SoftwareSerial
- It can't send and receive at the same moment.
- Above 38400 it starts making errors, so stick to 9600.
- It can conflict with other libraries that use interrupts (such as Servo).
If you have an Arduino Mega, use Serial1 on pins 18 and 19 instead, which is much better.
From the phone (Android)
- Turn on Bluetooth, find HC-05, and pair. The default password is usually 1234 or 0000.
- Install the Serial Bluetooth Terminal app (or any similar app).
- Connect to the module, and send
1and0.
The module's LED blinks fast while waiting, and slowly (or two blinks every so often) once connected.
There are also apps that let you build your own buttons and sliders, each button sending a character or word. Or you can build your own app with MIT App Inventor, with almost no programming.
Longer commands and sending readings
If you want to send pwm 128 or temp?, read up to the end of the line:
if (bt.available()) {
String cmd = bt.readStringUntil('\n');
cmd.trim();
if (cmd == "temp?") { bt.print("T="); bt.println(readTemp()); }
else if (cmd.startsWith("pwm ")) analogWrite(9, cmd.substring(4).toInt());
}
And have the app send a line ending (newline) with each message. The reply you send with bt.println appears on the phone's screen, so you can show a sensor's readings on the phone.
Project: a robot car
| Letter | Movement |
|---|---|
| F | Forward |
| B | Back |
| L | Left |
| R | Right |
| S | Stop |
Each letter calls a function that drives the motors with an H-bridge. And add a safety feature: if no command arrives for half a second (Bluetooth disconnected, for example), stop the car by itself. Use millis for that.
The iPhone can't see it!
The HC-05 uses Bluetooth Classic (SPP), and the iPhone doesn't allow this for ordinary apps. For an iPhone you need a BLE module (such as the HM-10) or an ESP32.
AT commands (changing the name and password)
- Disconnect the power, and hold the button on the module (or connect the EN/KEY pin to 3.3 V) while powering it up.
- The LED will blink slowly: that's AT mode.
- The speed in this mode is usually 38400.
- Commands such as:
AT(replies OK),AT+NAME=MyRobot,AT+PSWD="4321"(the syntax varies between versions).
Choose "Both NL & CR" as the line ending in the Serial Monitor, or the module won't reply. The easiest way: a small sketch that passes what you type in the Serial Monitor to the module and back.
Pairing two modules
For one Arduino to talk to another: one stays an ordinary slave, and you make the other a master with commands such as AT+ROLE=1, AT+CMODE=0 and AT+BIND= with the slave's address (which you get with AT+ADDR? from the other module). After that, the two connect to each other by themselves whenever they start.
HC-05 or HC-06?
- HC-06: slave only (the phone connects to it). Simpler.
- HC-05: can also be a master, meaning it can connect to another module. Useful for linking Arduino to Arduino.
With an ESP32
The original ESP32 has classic Bluetooth built in, and the BluetoothSerial library does the same job without a module. Note: the ESP32-C3 and S3 support BLE only. See wireless modules.
Common faults
| Fault | Usual cause |
|---|---|
| Connected but no data | TX and RX not crossed, or the speed differs |
| Strange characters | The speed in the code doesn't match the module |
| Uploads fail | The module is on pins 0 and 1 |
| No reply to AT commands | Not in AT mode, wrong line ending, or speed not 38400 |
| It disconnects when the motor starts | The supply dips; add a capacitor and power the motor separately |
FAQ
What's the range? Usually about 10 m, and less through walls.
Can several phones connect at once? No, the HC-05 accepts one connection at a time.