Built-in project · Arduino Uno R3

Bluetooth home automation with relays on an Arduino Uno

An HC-05 and a four-relay board: send 1 to 4 from the module’s box, as a phone app does, and that lamp toggles; A and a switch them all, ? says which are on. The whole build, an Arduino Uno and 9 more parts, 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $46. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Serial data over a software serial port
  • Voltage divider to protect a 3.3 V input
  • Active low relay boards
  • Switching a separate 12 V supply with relays
  • Turning single characters into commands
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 11

How it works

The HC-05 Bluetooth module talks to the Uno over a serial link on pins 2 and 3. In the simulator, you type into the module's own box in the serial monitor, the way a phone app would send a character.

Send 1 to 4 and that relay toggles. Send A to turn all four on, a to turn all four off, or ? to get a status line like 0101. Each relay switches a 12 V lamp from its own supply. The module also replies with text such as Relay 1 ON.

Parts list

11 parts, plus the jumper wires. They retail for about $46; on Mokxi they're free.

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 pinConnects to
Relay board, four channels pin IN4Arduino Uno R3 pin 7
Relay board, four channels pin IN3Arduino Uno R3 pin 6
Relay board, four channels pin IN2Arduino Uno R3 pin 5
Relay board, four channels pin IN1Arduino Uno R3 pin 4
Resistor, 1k Ω pin 1Arduino Uno R3 pin 3
Bluetooth serial module, HC-05 pin TXDArduino Uno R3 pin 2
Bluetooth serial module, HC-05 pin VCCArduino Uno R3 pin 5V
Relay board, four channels pin VCCArduino Uno R3 pin 5V
Bluetooth serial module, HC-05 pin GNDArduino Uno R3 pin GND
Resistor, 2k Ω pin 2Arduino Uno R3 pin GND
Relay board, four channels pin GNDArduino Uno R3 pin GND
Resistor, 1k Ω pin 2Bluetooth serial module, HC-05 pin RXD
Resistor, 2k Ω pin 1Bluetooth serial module, HC-05 pin RXD
Filament lamp (1) pin 1Relay board, four channels pin NO1
Relay board, four channels pin COM2Relay board, four channels pin COM1
Relay board, four channels pin COM3Relay board, four channels pin COM1
Relay board, four channels pin COM4Relay board, four channels pin COM1
DC jack and adapter pin V+Relay board, four channels pin COM1
Filament lamp (2) pin 1Relay board, four channels pin NO2
Filament lamp (3) pin 1Relay board, four channels pin NO3
Filament lamp (4) pin 1Relay board, four channels pin NO4
DC jack and adapter pin V-Ground
Filament lamp (1) pin 2Ground
Filament lamp (2) pin 2Ground
Filament lamp (3) pin 2Ground
Filament lamp (4) pin 2Ground

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Bluetooth home automation: an HC-05 and a four-relay board, switched from a
// phone app by single characters, with each change answered back.
//
//   HC-05        TXD -> pin 2 (SoftwareSerial RX)
//                RXD <- pin 3 through 1k, with 2k from RXD to GND
//                VCC 5V, GND
//   pins 4 to 7  the relay board's IN1 to IN4 (VCC 5V, GND)
//   each relay   COM to the 12 V adapter's +, NO to its lamp, the lamp's
//                other lead to the adapter's -
//   adapter -    also to GND, so the 12 V side shares the Uno's ground
//
// The switch-panel apps ("Arduino Bluetooth Control" and the like) send one
// character per button. This sketch takes:
//
//   1 2 3 4   toggle that relay
//   A         all on        a   all off
//   ?         say which are on
//
// In Mokxi the far end of the HC-05 is the module's own box in the serial
// monitor: type 1 there and press Enter, and relay 1 clicks over and lights
// its lamp. There is no radio here, only the UART a paired phone would talk
// to, so anything about pairing or range is left to the real module.
//
// The relay board is active low: LOW pulls a relay in. Each pin is written
// HIGH before it becomes an output so nothing clicks at power-up. The HC-05's
// RXD is a 3.3 V input, which is what the 1k and 2k divider is for.

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(2, 3);  // RX, TX

const int RELAYS[4] = {4, 5, 6, 7};
bool on[4] = {false, false, false, false};

void report() {
  for (int i = 0; i < 4; i++) {
    bluetooth.print(on[i] ? '1' : '0');
    Serial.print(on[i] ? '1' : '0');
  }
  bluetooth.println();
  Serial.println();
}

void set(int i, bool state) {
  on[i] = state;
  digitalWrite(RELAYS[i], state ? LOW : HIGH);  // active low
  bluetooth.print("Relay ");
  bluetooth.print(i + 1);
  bluetooth.println(state ? " ON" : " OFF");
  Serial.print("relay ");
  Serial.print(i + 1);
  Serial.println(state ? " ON" : " OFF");
}

void setup() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(RELAYS[i], HIGH);  // off before it is an output
    pinMode(RELAYS[i], OUTPUT);
  }
  Serial.begin(9600);
  bluetooth.begin(9600);  // the HC-05's factory baud
  Serial.println("Bluetooth relays: 1-4 toggle, A all on, a all off, ? status");
}

void loop() {
  if (!bluetooth.available()) return;
  char c = bluetooth.read();
  if (c >= '1' && c <= '4') {
    int i = c - '1';
    set(i, !on[i]);
  } else if (c == 'A' || c == 'a') {
    for (int i = 0; i < 4; i++) set(i, c == 'A');
  } else if (c == '?') {
    report();
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

set sketch.ino

Updates the stored state for one relay, then writes the pin. The relay board is active low, so LOW pulls the relay in and HIGH lets it go. It also sends a short message like Relay 2 ON to both the Bluetooth module and the serial monitor.

void set(int i, bool state) {
  on[i] = state;
  digitalWrite(RELAYS[i], state ? LOW : HIGH);  // active low
  bluetooth.print("Relay ");
  bluetooth.print(i + 1);
  bluetooth.println(state ? " ON" : " OFF");
  Serial.print("relay ");
  Serial.print(i + 1);
  Serial.println(state ? " ON" : " OFF");
}

loop sketch.ino

Waits until the Bluetooth port has a character. Digits 1 to 4 become an index and toggle that relay. A and a set all four relays at once, and ? calls report. Any other character is ignored.

void loop() {
  if (!bluetooth.available()) return;
  char c = bluetooth.read();
  if (c >= '1' && c <= '4') {
    int i = c - '1';
    set(i, !on[i]);
  } else if (c == 'A' || c == 'a') {
    for (int i = 0; i < 4; i++) set(i, c == 'A');
  } else if (c == '?') {
    report();
  }
}

setup sketch.ino

Sets each relay pin HIGH before making it an output, so no relay clicks on for a moment at power up. Then it starts both serial ports at 9600 baud, the HC-05's factory speed.

void setup() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(RELAYS[i], HIGH);  // off before it is an output
    pinMode(RELAYS[i], OUTPUT);
  }
  Serial.begin(9600);
  bluetooth.begin(9600);  // the HC-05's factory baud
  Serial.println("Bluetooth relays: 1-4 toggle, A all on, a all off, ? status");
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • A relay clicks on at power up, or works backwards. This project
    The board is active low. Write HIGH for off and LOW for on, and set the level before pinMode, as setup does.
  • The lamps never light even though the relays click. This project
    Check that the 12 V adapter's minus is also joined to the Uno's GND, and that COM goes to the adapter's plus.
  • The module sends garbage or nothing. This project
    Check the 1k and 2k divider on pin 3 and that both sides use 9600 baud.
  • 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.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The relay clicks on at startup, or does the opposite of what the code says. Relay board
    These boards are active low: LOW turns the relay on and HIGH turns it off. Set the pin HIGH before pinMode OUTPUT so the relay does not pulse on at boot.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.