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.
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_RXCinterrupt, which stores it. The main loop reads it and decides what to do. - Commands:
1–4turn relay 1–4 ON,a–dturn them OFF,Aturns all ON,Zturns all OFF andSsends 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 / Pin | ATmega16 pin | Notes |
|---|---|---|
| HC-05 TXD | PD0 / RXD (pin 14) | Data from phone to MCU |
| HC-05 RXD | PD1 / 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 / GND | The HC-05 board has its own 3.3 V regulator |
| LCD RS | PC0 (pin 22) | Register select |
| LCD E | PC1 (pin 23) | Enable |
| LCD D4, D5, D6, D7 | PC2, PC3, PC4, PC5 (pins 24–27) | 4-bit data bus |
| LCD RW | GND | Write only |
| LCD V0 | 10 kΩ pot wiper | Pot ends to +5 V and GND |
| LCD VSS / VDD / A / K | GND / +5 V / +5 V (via 220 Ω) / GND | Power and backlight |
| Relay IN1, IN2, IN3, IN4 | PB0, PB1, PB2, PB3 (pins 1–4) | Relay VCC to +5 V, GND to GND |
| SW1, SW2, SW3, SW4 | PA0, PA1, PA2, PA3 (pins 40, 39, 38, 37) | Other leg of each button to GND |
| Status LED | PD7 (pin 21) | Through 330 Ω to GND |
| 16 MHz crystal | XTAL1 (12) and XTAL2 (13) | 22 pF from each pin to GND |
| VCC, AVCC | Pins 10 and 30 | +5 V from 7805, 100 nF to GND near each pin |
| GND | Pins 11 and 31 | Common ground for all modules |
| RESET | Pin 9 | 10 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 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:mHow to Use It From Your Phone
- Power the board and pair your phone with the HC-05 (default PIN is
1234or0000). - Open any free Bluetooth serial terminal app and connect to the HC-05.
- Send
1to switch on relay 1,ato switch it off,Afor all on,Zfor all off andSto get the status. - Many apps let you make buttons that send these characters, which gives you a simple remote control screen.
Troubleshooting
| Problem | Fix |
|---|---|
| LCD shows black boxes only | Adjust the 10 kΩ contrast pot and check the RS, E and D4–D7 wires. |
| Phone sends commands but nothing happens | Check 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 way | Set RELAY_ACTIVE_LOW to 0 for an active-HIGH relay module. |
| Board resets when a relay clicks | Use 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 work | JTAG 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.