Arduino Uno + HC-05 Bluetooth LED and Relay
Switch an LED and a relay on and off over Bluetooth with two switches on your phone; it is built with an Arduino Uno and an HC-05, 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 the LED and relay template, and upload the code from Get the code.

Video
Circuit Diagram

Source Code
1// ┌────────────────────────────────────┐
2// │ BlueGrays · bluegrays.com │
3// │ Bluetooth · Arduino Uno + HC-05/06 │
4// │ BG-Lamba │
5// └────────────────────────────────────┘
6// LED and relay
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-Lamba"; // 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 LED_PIN = 5;
16const int RELAY_PIN = 6;
17const bool RELAY_ACTIVE_LOW = true; // most relay modules switch on LOW
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 ──
87bool ledOn = false, relayOn = false;
88
89void setRelay(bool on) {
90 if (RELAY_ACTIVE_LOW) {
91 if (on) { pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, LOW); }
92 else { pinMode(RELAY_PIN, INPUT); } // off releases the pin: a 3.3 V HIGH is not enough to turn a 5 V module off
93 } else {
94 pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, on ? HIGH : LOW);
95 }
96}
97
98void apply() {
99 digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
100 setRelay(relayOn);
101}
102
103void onValue(const char *key, const char *value) {
104 if (is(key, "led")) ledOn = isOn(value);
105 else if (is(key, "role")) relayOn = isOn(value);
106 apply();
107}
108
109void sendAll() {
110 sendInt("led", ledOn);
111 sendInt("role", relayOn);
112}
113
114void onLinkLost() {
115}
116
117void setup() {
118 Serial.begin(115200);
119 pinMode(LED_PIN, OUTPUT);
120 apply();
121 btBegin();
122 Serial.print(F("BlueGrays Bluetooth: "));
123 Serial.println(BLE_NAME);
124}
125
126void loop() {
127 btPoll();
128}
129FAQ
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 LED and relay guide built with an ESP32 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 Bluetooth LED project?
In the older project #34, which lit LEDs from a phone over Bluetooth (HC-06, two LEDs), the LEDs were switched with letter commands sent from a generic Bluetooth terminal app. This project works with the LED and relay switches on the BlueGrays panel and reports the state back to the app: when the panel opens, the switches show what is really happening on the board. The app prepares the code for the board you pick.
Can I connect a real lamp to the relay?
The relay module can switch 220 V, but mains electricity can kill and is a job for adults. In this project nothing is wired to the relay's COM and NO terminals; the relay shows it works with its click and the module's LED. If you want to try a load, use a low-voltage circuit such as a battery or a 12 V adapter. If you move on to 220 V, do it with an adult who knows the work, and never leave bare wires or screws exposed.
What happens if the connection drops, and what is the range?
The LED and the relay stay as they were; the board doesn't switch them off by itself. The HC-05 doesn't tell the Uno when the connection drops, so the board only sees that no new command arrives. The range is about 10 metres in the open, less through walls. When you reconnect, the app sends ? and the board replies with the real state of the LED and the relay, so the switches start in the right place. No internet or server is used.
