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 Runs in your browser. Free, and no account needed.
Step by step, with the wiring and the common mistakes: Arduino HC-05 Bluetooth: Wiring, Code, AT Commands
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());
}
}
Parts list
3 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 3; Bluetooth serial module, HC-05 pin RXD
- Arduino Uno R3 pin 2; Bluetooth serial module, HC-05 pin TXD
- Arduino Uno R3 pin 5V; Bluetooth serial module, HC-05 pin VCC
- Ground: Arduino Uno R3 pin GND; Bluetooth serial module, HC-05 pin 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.