ATmega16 Bluetooth Home Automation with HC-05, 4 Relays & LCD

Build an ATmega16 Bluetooth home automation system with HC-05, a 4-channel relay module, 16×2 LCD and manual buttons. Wiring table, diagram and full AVR C code.

  • DifficultyAdvanced
  • Build time5–8 hours
  • Board / MCUATmega16
  • Parts12 items
  • CodeAVR C

Need it ready-made? Buy Ready Project

This ATmega16 Bluetooth home automation project lets you switch four home appliances ON and OFF from your Android phone using an HC-05 Bluetooth module, a 4-channel relay module and a 16×2 LCD that shows the live status of every relay. Four push buttons on the board also work as manual switches, so the lights still work even when the phone is not connected.

It is a big combination project: it uses the ATmega16 hardware UART with an interrupt, a 4-bit LCD driver written from scratch, debounced buttons and relay control, all in plain AVR C with no Arduino libraries. It is a great step up for students who already know basic AVR programming.

How the Bluetooth Home Automation Works

  • Phone to HC-05: a free Bluetooth terminal app on the phone sends single characters. The HC-05 receives them over Bluetooth and passes them to the ATmega16 over serial at 9600 baud.
  • UART interrupt: every received byte triggers the USART_RXC interrupt, which stores it. The main loop reads it and decides what to do.
  • Commands: 1–4 turn relay 1–4 ON, a–d turn them OFF, A turns all ON, Z turns all OFF and S sends the status back to the phone.
  • Manual buttons: SW1–SW4 on PA0–PA3 toggle the matching relay. The code uses internal pull-ups and a 30 ms software debounce.
  • LCD feedback: the 16×2 LCD shows R1 to R4 on the first line and ON or OFF under each one. Every change is also sent back to the phone.
  • Relay driver: the relay module has its own transistor and opto-coupler, so the ATmega16 pins drive the IN pins directly. Most modules are active LOW, which is set with RELAY_ACTIVE_LOW.

Components Required

  • ATmega16 / ATmega16A microcontroller (DIP-40) – 1
  • HC-05 Bluetooth module – 1
  • 4-channel 5 V relay module (opto-isolated) – 1
  • 16×2 character LCD (HD44780) – 1
  • 10 kΩ potentiometer (LCD contrast) – 1
  • 16 MHz crystal and 2 × 22 pF capacitors
  • Tactile push buttons – 4
  • Resistors: 1 kΩ, 2 kΩ (HC-05 RX divider), 10 kΩ (reset pull-up), 330 Ω (LED)
  • Status LED – 1
  • 7805 voltage regulator with 10 µF and 100 µF capacitors, plus 100 nF decoupling capacitors
  • 12 V DC adapter (1 A) and DC jack
  • USBasp programmer, breadboard or perfboard, jumper wires

Wiring Table (Pin-to-Pin)

Part / PinATmega16 pinNotes
HC-05 TXDPD0 / RXD (pin 14)Data from phone to MCU
HC-05 RXDPD1 / TXD (pin 15)Through 1 kΩ, with 2 kΩ from HC-05 RXD to GND (5 V to 3.3 V divider)
HC-05 VCC / GND+5 V / GNDThe HC-05 board has its own 3.3 V regulator
LCD RSPC0 (pin 22)Register select
LCD EPC1 (pin 23)Enable
LCD D4, D5, D6, D7PC2, PC3, PC4, PC5 (pins 24–27)4-bit data bus
LCD RWGNDWrite only
LCD V010 kΩ pot wiperPot ends to +5 V and GND
LCD VSS / VDD / A / KGND / +5 V / +5 V (via 220 Ω) / GNDPower and backlight
Relay IN1, IN2, IN3, IN4PB0, PB1, PB2, PB3 (pins 1–4)Relay VCC to +5 V, GND to GND
SW1, SW2, SW3, SW4PA0, PA1, PA2, PA3 (pins 40, 39, 38, 37)Other leg of each button to GND
Status LEDPD7 (pin 21)Through 330 Ω to GND
16 MHz crystalXTAL1 (12) and XTAL2 (13)22 pF from each pin to GND
VCC, AVCCPins 10 and 30+5 V from 7805, 100 nF to GND near each pin
GNDPins 11 and 31Common ground for all modules
RESETPin 910 kΩ pull-up to +5 V

Connection Diagram

The picture below gives an overview of the whole build. The pin-accurate drawing after it matches the wiring table and the code exactly, so follow the table and that drawing when you wire the board.

ATmega16 Bluetooth home automation connection diagram with HC-05, 16x2 LCD, 4-channel relay module, buttons and 7805 regulator
ATmega16 Bluetooth Home Automation – Pin-Accurate Wiring ATmega16 (DIP-40) PD0 (RXD, pin 14) HC-05 TXD PD1 (TXD, pin 15) 1 kΩ → HC-05 RXD (2 kΩ from RXD to GND) PC0 / PC1 (pins 22, 23) LCD RS / E PC2–PC5 (pins 24–27) LCD D4–D7 (RW → GND, 10 kΩ pot → V0) PB0–PB3 (pins 1–4) Relay module IN1–IN4 PA0–PA3 (pins 40–37) Buttons SW1–SW4 → GND (internal pull-ups) PD7 (pin 21) 330 Ω → status LED → GND XTAL1 / XTAL2 (12, 13) 16 MHz crystal + 22 pF to GND on each pin VCC, AVCC (10, 30) +5 V from 7805 (100 nF to GND) GND (11, 31) Common GND: HC-05, LCD, relays, 7805 RESET (pin 9) 10 kΩ to +5 V Relay module VCC: +5 V (7805 rated 1 A, add a heatsink). Mains wiring only by a qualified person.
Pin-accurate connection diagram for the ATmega16 Bluetooth home automation.

ATmega16 Code (AVR C)

Create a new GCC C project for the ATmega16 in Microchip Studio (or use avr-gcc), paste the code and build it. Flash the HEX with a USBasp and set the fuses to lfuse 0xFF and hfuse 0xD9 for the 16 MHz external crystal with JTAG disabled. The code is fully commented and compiles without errors for the ATmega16 (about 1 KB of flash).

/*
  ATmega16 Bluetooth Home Automation
  HC-05 Bluetooth + 4-channel relay module + 16x2 LCD (4-bit) + 4 manual buttons
  Compiler : AVR-GCC (Microchip Studio, or avr-gcc + avrdude)
  Clock    : 16 MHz external crystal  (fuses: lfuse 0xFF, hfuse 0xD9 - JTAG disabled)

  Wiring
    HC-05 TXD -> PD0 (RXD)      HC-05 RXD <- PD1 (TXD) through 1k/2k divider
    LCD RS -> PC0   E -> PC1   D4..D7 -> PC2..PC5   RW -> GND
    Relay IN1..IN4 -> PB0..PB3
    Buttons SW1..SW4 -> PA0..PA3 to GND (internal pull-ups)
    Status LED -> PD7 (330 ohm)

  Bluetooth commands (send from any "Bluetooth Terminal" phone app, 9600 baud)
    '1'..'4'  turn relay 1..4 ON
    'a'..'d'  turn relay 1..4 OFF
    'A'       all ON        'Z'  all OFF
    'S'       send status back to the phone
*/

#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>

#define BAUD 9600
#define UBRR_VALUE ((F_CPU / 16UL / BAUD) - 1)

#define RELAY_ACTIVE_LOW 1      // most blue relay modules switch ON when IN is LOW

#define LCD_RS PC0
#define LCD_E  PC1
#define LED_PIN PD7

static volatile uint8_t rx_char = 0;    // last character received (0 = none)
static uint8_t relay_state = 0;         // bit0..bit3 = relay 1..4 (1 = ON)

/* ---------------- LCD (4-bit, PORTC) ---------------- */
static void lcd_nibble(uint8_t n) {
  PORTC = (PORTC & 0xC3) | ((n & 0x0F) << 2);   // D4..D7 on PC2..PC5
  PORTC |= (1 << LCD_E);
  _delay_us(1);
  PORTC &= ~(1 << LCD_E);
  _delay_us(50);
}

static void lcd_send(uint8_t value, uint8_t is_data) {
  if (is_data) PORTC |= (1 << LCD_RS); else PORTC &= ~(1 << LCD_RS);
  lcd_nibble(value >> 4);
  lcd_nibble(value);
  if (!is_data && value < 4) _delay_ms(2);        // clear and home need more time
}

static void lcd_cmd(uint8_t c)  { lcd_send(c, 0); }
static void lcd_char(char c)    { lcd_send((uint8_t)c, 1); }
static void lcd_print(const char *s) { while (*s) lcd_char(*s++); }
static void lcd_goto(uint8_t row, uint8_t col) { lcd_cmd(0x80 | (row ? 0x40 : 0x00) | col); }

static void lcd_init(void) {
  DDRC |= 0x3F;                  // PC0..PC5 outputs
  _delay_ms(50);                 // LCD power-up time
  PORTC &= ~(1 << LCD_RS);
  lcd_nibble(0x03); _delay_ms(5);
  lcd_nibble(0x03); _delay_ms(1);
  lcd_nibble(0x03);
  lcd_nibble(0x02);              // 4-bit mode
  lcd_cmd(0x28);                 // 2 lines, 5x8 font
  lcd_cmd(0x0C);                 // display on, cursor off
  lcd_cmd(0x06);                 // auto-increment
  lcd_cmd(0x01);                 // clear
}

/* ---------------- UART (HC-05) ---------------- */
static void uart_init(void) {
  UBRRH = (uint8_t)(UBRR_VALUE >> 8);
  UBRRL = (uint8_t)UBRR_VALUE;
  UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);       // RX, TX, RX interrupt
  UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0);     // 8 data bits, 1 stop, no parity
}

static void uart_putc(char c) {
  while (!(UCSRA & (1 << UDRE)));  // wait until the transmit buffer is empty
  UDR = c;
}

static void uart_print(const char *s) { while (*s) uart_putc(*s++); }

ISR(USART_RXC_vect) {
  rx_char = UDR;                   // store the byte; main loop handles it
}

/* ---------------- Relays ---------------- */
static void relays_apply(void) {
  uint8_t out = relay_state & 0x0F;
  if (RELAY_ACTIVE_LOW) out = (~out) & 0x0F;
  PORTB = (PORTB & 0xF0) | out;    // PB0..PB3
}

static void show_status(void) {
  // Line 1: "R1 R2 R3 R4", line 2: "ON OF ON OF"
  lcd_goto(0, 0);
  lcd_print("R1  R2  R3  R4  ");
  lcd_goto(1, 0);
  for (uint8_t i = 0; i < 4; i++) {
    lcd_print((relay_state & (1 << i)) ? "ON  " : "OFF ");
  }
}

static void send_status(void) {
  uart_print("Status: ");
  for (uint8_t i = 0; i < 4; i++) {
    uart_putc('R'); uart_putc('1' + i); uart_putc('=');
    uart_print((relay_state & (1 << i)) ? "ON " : "OFF ");
  }
  uart_print("\r\n");
}

static void handle_command(char c) {
  if (c >= '1' && c <= '4')      relay_state |=  (1 << (c - '1'));
  else if (c >= 'a' && c <= 'd') relay_state &= ~(1 << (c - 'a'));
  else if (c == 'A')             relay_state = 0x0F;
  else if (c == 'Z')             relay_state = 0x00;
  else if (c == 'S')           { send_status(); return; }
  else return;                    // ignore newline and unknown characters
  relays_apply();
  show_status();
  send_status();
  PORTD ^= (1 << LED_PIN);        // blink LED on every valid command
}

/* ---------------- Buttons (PA0..PA3, active LOW) ---------------- */
static void check_buttons(void) {
  static uint8_t last = 0x0F;     // all released (pull-ups)
  uint8_t now = PINA & 0x0F;
  uint8_t pressed = last & ~now;  // bits that went from 1 to 0
  if (pressed) {
    _delay_ms(30);                // debounce
    now = PINA & 0x0F;
    pressed &= ~now;              // still pressed after debounce
    if (pressed) {
      relay_state ^= pressed;     // toggle the matching relay(s)
      relays_apply();
      show_status();
      send_status();
    }
  }
  last = now;
}

int main(void) {
  DDRB |= 0x0F;                   // relay outputs
  relays_apply();                 // start with all relays OFF
  DDRA &= ~0x0F;                  // buttons as inputs
  PORTA |= 0x0F;                  // enable pull-ups
  DDRD |= (1 << LED_PIN);

  lcd_init();
  lcd_goto(0, 0); lcd_print("BT Home Control");
  lcd_goto(1, 0); lcd_print("ATmega16 Ready ");
  _delay_ms(1500);

  uart_init();
  sei();                          // enable global interrupts
  uart_print("ATmega16 Home Automation ready\r\n");
  show_status();

  while (1) {
    if (rx_char) {
      char c;
      cli(); c = rx_char; rx_char = 0; sei();
      handle_command(c);
    }
    check_buttons();
    _delay_ms(5);
  }
}

Example command line build and flash:

avr-gcc -mmcu=atmega16 -Os -o bt.elf atmega16_bt_home_automation.c
avr-objcopy -O ihex bt.elf bt.hex
avrdude -c usbasp -p m16 -U flash:w:bt.hex -U lfuse:w:0xFF:m -U hfuse:w:0xD9:m

How to Use It From Your Phone

  • Power the board and pair your phone with the HC-05 (default PIN is 1234 or 0000).
  • Open any free Bluetooth serial terminal app and connect to the HC-05.
  • Send 1 to switch on relay 1, a to switch it off, A for all on, Z for all off and S to get the status.
  • Many apps let you make buttons that send these characters, which gives you a simple remote control screen.

Troubleshooting

ProblemFix
LCD shows black boxes onlyAdjust the 10 kΩ contrast pot and check the RS, E and D4–D7 wires.
Phone sends commands but nothing happensCheck that HC-05 TXD goes to PD0 and that the baud rate is 9600. Make sure the fuses select the 16 MHz crystal, or the baud rate will be wrong.
Relays work the opposite waySet RELAY_ACTIVE_LOW to 0 for an active-HIGH relay module.
Board resets when a relay clicksUse a 1 A adapter, add a 470 µF capacitor on the 5 V rail and power the relay coils from a separate JD-VCC supply.
PORTC pins do not workJTAG is enabled. Program hfuse 0xD9 to disable JTAG so PC2–PC5 work as normal I/O.

Safety and Ideas to Extend

  • Switching 230 V AC is dangerous. Test with low-voltage lamps first, keep the mains side in a closed box and let a qualified person do the mains wiring.
  • Use relays rated for your load and add a fuse on the mains side.
  • Add a DS1307 RTC for timed switching, or an LDR for automatic night lights.
  • Replace the HC-05 with an ESP8266 module to control the same board over Wi-Fi.

FAQ

Can I use the ATmega32 instead? Yes. The ATmega32 has the same pinout and registers, so change the MCU to atmega32 and rebuild.

Why a voltage divider on HC-05 RXD? The HC-05 logic runs at 3.3 V. The 1 kΩ and 2 kΩ divider lowers the 5 V TX signal from the ATmega16 to about 3.3 V so the module is not stressed.

Does it need internet? No. Bluetooth works directly between the phone and the board within about 10 metres.

Leave a Comment