HC-06 Bluetooth chat on an Arduino Uno
An HC-06 on SoftwareSerial at 9600. Pair from the Bluetooth tab with PIN 1234, connect, and send 1 or 0 for the LED. Simulated Bluetooth, no radio. 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Serial communication over Bluetooth
- SoftwareSerial on two spare pins
- Crossing TX and RX between devices
- Collecting characters into a line
- Comparing strings to pick a command
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
An HC-06 Bluetooth module talks to the Uno over SoftwareSerial on pins 2 and 3 at 9600 baud. The module's TXD goes to pin 2 and its RXD to pin 3. In Mokxi, a simulated phone stands in for the real radio.
Press Run, open the Bluetooth tab, pair with PIN 1234 and connect. Then type 1 or 0.
The sketch collects characters into a line. When it sees a newline, it checks the line. A 1 turns the on-board LED on and a 0 turns it off. It replies over Bluetooth either way.
Parts list
3 parts, plus the jumper wires. They retail for about $34; 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-06
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
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-06 pin RXD | Arduino Uno R3 pin 3 |
| Bluetooth serial module, HC-06 pin TXD | Arduino Uno R3 pin 2 |
| Bluetooth serial module, HC-06 pin VCC | Arduino Uno R3 pin 5V |
| Bluetooth serial module, HC-06 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.
// HC-06 Bluetooth chat: the four-pin module on a SoftwareSerial port.
//
// HC-06 TXD -> pin 2 (the sketch's RX)
// HC-06 RXD <- pin 3 (the sketch's TX)
// HC-06 VCC -> 5V, GND -> GND
//
// Simulated Bluetooth: a phone in Mokxi, not a real radio. Press Run, open
// the Bluetooth tab, pair with the HC-06 (PIN 1234) and connect, the way a
// phone's terminal app does. Then send 1 or 0 and the LED on pin 13 follows.
//
// An HC-06 comes out of the bag with nothing connected, and until something
// connects, whatever the sketch sends it is an AT command: the hello below
// is only sent once a phone is there to read it.
#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("HC-06 chat ready at 9600 baud. Connect from the Bluetooth tab.");
}
// A whole line from the phone. The answer waits until the line has ended:
// SoftwareSerial cannot listen while it talks.
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");
} else {
BTSerial.println("Send 1 or 0 for the LED.");
}
length = 0;
}
void loop() {
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;
}
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
It sets the on-board LED as an output and starts both serial ports at 9600: the USB serial for the monitor and BTSerial for the module. Then it prints a ready message that reminds you to connect from the Bluetooth tab.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
BTSerial.begin(9600);
Serial.println("HC-06 chat ready at 9600 baud. Connect from the Bluetooth tab.");
}command sketch.ino
Called when a full line has arrived. It ends the text with a zero byte, then uses strcmp to compare it with "1" and "0". It switches the LED and replies "LED on" or "LED off". Anything else gets a hint to send 1 or 0. Finally it resets length for the next line.
// A whole line from the phone. The answer waits until the line has ended:
// SoftwareSerial cannot listen while it talks.
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");
} else {
BTSerial.println("Send 1 or 0 for the LED.");
}
length = 0;
}loop sketch.ino
It checks BTSerial.available(), reads one character, and echoes it to the serial monitor. A newline calls command(). Other characters, except carriage returns, are added to line as long as there is room.
void loop() {
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;
}
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing arrives when you send 1 or 0 This project
TXD must go to pin 2 and RXD to pin 3. Also connect from the Bluetooth tab first; the module only listens once paired. - The reply says to send 1 or 0 This project
The line must be exactly 1 or 0 and end with a newline. Extra characters make it unrecognized. - AT commands get no reply. Bluetooth serial module
The HC-06 only takes AT commands when nothing is connected. Use capital letters, 9600 baud and no line ending, as it does not want CR or LF. - Text is garbled or the module is stressed. Bluetooth serial module
Cross TX and RX between the module and Arduino. The module RX is 3.3 V logic, so use a voltage divider on a 5 V TX. The default pairing code is 1234. - 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.