Bluetooth chat on an Arduino Uno
The HC-05 tutorial sketch with SoftwareSerial at 9600. Type as the phone in the module’s serial box, and send 1 or 0 for the LED. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $35. On Mokxi they're free.
Step by step, with the wiring and the common mistakes: Arduino HC-05 Bluetooth: Wiring, Code, AT Commands
What you'll learn
- A software serial port on two ordinary pins
- Crossing TX and RX between two devices
- Collecting characters into a line before acting
- Baud rate has to match on both ends
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
An HC-05 Bluetooth module is wired to the Uno's pins 2 and 3 and powered from 5V. The Uno's one hardware serial port belongs to the serial monitor, so the sketch makes a second port on pins 2 and 3 with SoftwareSerial at 9600 baud, the speed an HC-05 ships at.
In the serial monitor the module has its own box next to the board's. That box plays the phone. Whatever the sketch receives from the module is copied to the board's monitor, and whatever you type in the board's box is sent on to the phone.
Send a line that says 1 and the built-in LED on pin 13 turns on. Send 0 and it turns off. There is no radio: Mokxi models only the serial side of the module.
Parts list
3 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Bluetooth serial module, HC-05
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Bluetooth serial module, HC-05 pin RXD | Arduino Uno R3 pin 3 |
| Bluetooth serial module, HC-05 pin TXD | Arduino Uno R3 pin 2 |
| Bluetooth serial module, HC-05 pin VCC | Arduino Uno R3 pin 5V |
| Bluetooth serial module, HC-05 pin GND | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Bluetooth chat: the HC-05 sketch every tutorial starts with.
//
// HC-05 TXD -> pin 2 (the sketch's RX)
// HC-05 RXD <- pin 3 (the sketch's TX)
// HC-05 VCC -> 5V, GND -> GND
//
// The Uno's one hardware serial port belongs to the serial monitor, so the
// module gets a SoftwareSerial port on two ordinary pins, at 9600 baud, which
// is what an HC-05 ships at.
//
// In the serial monitor the module has its own box, beside the board's. That
// box is the phone: what you type there comes out of the module's TXD, and
// what the sketch sends the module shows up there. What you type in the
// board's box goes the other way, to the phone.
//
// Send a line that says 1 and the LED on pin 13 comes on; 0 turns it off.
//
// There is no radio here. Mokxi models the half of the HC-05 the Uno can see,
// its serial port, bit by bit. Pairing, range and the phone app are not
// modeled.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX, TX
char line[32];
byte length = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
BTSerial.begin(9600);
Serial.println("Bluetooth chat ready at 9600 baud.");
BTSerial.println("Hello from the Uno. Send 1 or 0 for the LED.");
}
// A whole line from the phone. The answer waits until the line has ended:
// SoftwareSerial cannot listen while it talks, so an answer sent halfway
// through a line would lose the rest of it.
void command() {
line[length] = '\0';
if (strcmp(line, "1") == 0) {
digitalWrite(LED_BUILTIN, HIGH);
BTSerial.println("LED on");
} else if (strcmp(line, "0") == 0) {
digitalWrite(LED_BUILTIN, LOW);
BTSerial.println("LED off");
}
length = 0;
}
void loop() {
// The phone to the serial monitor.
if (BTSerial.available()) {
char c = BTSerial.read();
Serial.write(c);
if (c == '\n') {
command();
} else if (c != '\r' && length < sizeof(line) - 1) {
line[length++] = c;
}
}
// The serial monitor to the phone.
if (Serial.available()) {
BTSerial.write(Serial.read());
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets the built-in LED as an output and starts both serial ports at 9600 baud: Serial for the monitor and BTSerial for the module. It then prints a greeting to each, so you can see both sides are alive.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
BTSerial.begin(9600);
Serial.println("Bluetooth chat ready at 9600 baud.");
BTSerial.println("Hello from the Uno. Send 1 or 0 for the LED.");
}loop sketch.ino
Passes bytes in both directions. A byte from the module is echoed to the monitor and added to a line buffer. When a newline arrives it calls command. A byte typed in the monitor is simply forwarded to the module.
void loop() {
// The phone to the serial monitor.
if (BTSerial.available()) {
char c = BTSerial.read();
Serial.write(c);
if (c == '\n') {
command();
} else if (c != '\r' && length < sizeof(line) - 1) {
line[length++] = c;
}
}
// The serial monitor to the phone.
if (Serial.available()) {
BTSerial.write(Serial.read());
}
}command sketch.ino
Runs once a whole line has arrived. It ends the text with a zero, then compares it to "1" or "0", switches the LED and answers "LED on" or "LED off". It waits until the line ends to answer, because SoftwareSerial cannot listen while it talks.
// A whole line from the phone. The answer waits until the line has ended:
// SoftwareSerial cannot listen while it talks, so an answer sent halfway
// through a line would lose the rest of it.
void command() {
line[length] = '\0';
if (strcmp(line, "1") == 0) {
digitalWrite(LED_BUILTIN, HIGH);
BTSerial.println("LED on");
} else if (strcmp(line, "0") == 0) {
digitalWrite(LED_BUILTIN, LOW);
BTSerial.println("LED off");
}
length = 0;
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing arrives from the module This project
TX and RX must cross: module TXD to pin 2 and module RXD to pin 3. - Garbled characters This project
BothBTSerial.beginand the monitor speed must match the module, 9600 baud. - Typing 1 does not turn the LED on This project
Send the 1 as a full line, ending with a newline. The sketch only acts when the line ends. - Garbled text or nothing arrives. Bluetooth serial module
Cross the wires: module TX to Arduino RX, module RX to Arduino TX. Match the baud rate. The default data rate is 9600 and the pairing code is usually 1234. - The module's RX pin is wired straight to a 5 V pin. Bluetooth serial module
HC-05 logic is 3.3 V. Use a voltage divider (for example 1 k and 2 k ohms) on the Arduino TX line going into module RX. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.