4.15) Smart phone controlled devices using Bluetooth module HC05.

posted by Hamid Sayyed • November 15, 2025 0 Comments

Wireless control of devices using a smartphone and a Bluetooth module is an excellent first wireless project for beginners. Using the HC-05 Bluetooth module together with an ATmega16 microcontroller you can control lights, motors, relays and other loads without any wiring between the user and the appliance. The mobile phone runs a simple terminal or controller app and sends small command bytes such as '1' or '0' which the HC-05 forwards to ATmega16 over UART. ATmega16 reads the incoming bytes, decodes the commands in software and toggles output pins to operate a relay or LED driver. This method requires basic UART setup on ATmega16, safe voltage-level handling between 5V MCU and 3.3V module RX pin, and a simple command protocol for reliable control. The project is low-cost, reliable within Bluetooth range, and useful for home automation demos, lab assignments, and quick prototypes. In this article we will cover HC-05 features and pins, wiring best-practice, voltage level advice, full C source code (escaped for Blogger), smartphone setup, troubleshooting and an animated diagram to visualise data flow. Follow the steps carefully and test first with an LED or small relay module before connecting high-power appliances.

Overview of HC-05 and ATmega16

HC-05 is a Bluetooth-to-UART module commonly used in hobby and educational projects. It supports Bluetooth v2.0 + EDR, is easy to pair with Android phones, and can be configured with AT commands. The module’s TX pin transmits data to the microcontroller RX pin; its RX pin receives data from the MCU TX pin. ATmega16 provides a USART (serial port) that is perfect for communicating at standard baud rates such as 9600 bps. Typical workflow: phone → HC-05 → ATmega16 (UART) → driver (relay/LED/motor).

HC-05 Pin Summary

PinFunction
VCC5V supply (module typically has regulator; check board)
GNDGround
TXDTransmit (Bluetooth → MCU RX)
RXDReceive (MCU TX → Bluetooth) — 3.3V tolerant
STATEConnection status output (optional)
EN / KEYEnter AT mode when pulled high (optional)
Safety tip: HC-05 RX expects 3.3V logic. When connecting MCU TX (5V) to HC-05 RX, always use a simple voltage divider or level shifter to avoid damage. HC-05 TX is usually 3.3V and is safe for MCU RX.

Hardware Connections (recommended)

Connect HC-05 TXD → ATmega16 RXD (PD0). Connect HC-05 RXD ← ATmega16 TXD (PD1) via a voltage divider (e.g., 1.8k & 3.3k). Power HC-05 with 5V (check your breakout board) and common ground with microcontroller. Connect the device driver (relay module or transistor + diode) to a port pin (for example PB0). Use a separate 5V supply for motors or inductive loads and optoisolate or use proper relays and flyback diodes.

Command Protocol and Mobile App

Keep commands tiny and simple — a single ASCII byte is enough. For example: '1' = turn ON, '0' = turn OFF, '2' = toggle, 'S' = status request. On the phone, use any Bluetooth terminal app or a custom app made with MIT App Inventor. Pair with HC-05 (default pin usually 1234 or 0000). After connecting, send characters and the device will respond. If you need feedback, implement simple ACK replies from MCU.

Full C Code (ATmega16) — HTML-escaped for Blogger

This code example configures the USART, reads incoming bytes, processes multiple commands, and drives two outputs (PB0, PB1). It includes a simple acknowledge reply and a basic debounce for safety.

/* ATmega16 — Bluetooth HC-05 control (example)
   - HC-05 TX -> PD0 (RXD)
   - HC-05 RX <- PD1 (TXD) via voltage divider
   - Output devices -> PB0 (Device1), PB1 (Device2)
   - F_CPU assumed (adjust UBRR for your clock)
*/

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define F_CPU 8000000UL

void uart_init(unsigned int ubrr) {
    UBRRH = (unsigned char)(ubrr >> 8);
    UBRRL = (unsigned char)ubrr;
    UCSRB = (1<<RXEN) | (1<<TXEN);   // Enable RX and TX
    UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0); // 8-bit data
}

unsigned char uart_receive(void) {
    while (!(UCSRA & (1<<RXC)));    // Wait for data
    return UDR;
}

void uart_transmit(unsigned char data) {
    while (!(UCSRA & (1<<UDRE)));
    UDR = data;
}

int main(void) {
    unsigned int ubrr_value = 51; // 9600 @ 8MHz
    unsigned char cmd;

    // Configure outputs
    DDRB |= (1<<PB0) | (1<<PB1); // PB0, PB1 as output
    PORTB &= ~((1<<PB0) | (1<<PB1))); // Start OFF

    uart_init(ubrr_value);

    while (1) {
        cmd = uart_receive();

        // Simple command set
        if (cmd == '1') {
            PORTB |= (1<<PB0); // Device1 ON
            uart_transmit('A');    // ACK
        }
        else if (cmd == '0') {
            PORTB &= ~(1<<PB0);// Device1 OFF
            uart_transmit('A');
        }
        else if (cmd == '2') {
            PORTB |= (1<<PB1); // Device2 ON
            uart_transmit('B');
        }
        else if (cmd == '3') {
            PORTB &= ~(1<<PB1);// Device2 OFF
            uart_transmit('B');
        }
        else if (cmd == 'S') {    // Status request
            unsigned char status = (PINB & 0x03); // PB0..PB1
            uart_transmit(status + '0'); // send '0'..'3'
        }
        // small debounce / safety
        _delay_ms(50);
    }
}
  

Animated Diagram (data flow)

The diagram below visually shows the smartphone sending a command to HC-05, HC-05 forwarding that to ATmega16 over UART, and ATmega16 switching outputs. The animation also shows small moving dots to indicate serial data flow.

Troubleshooting & Tips

  • If HC-05 does not pair, reset module and try default PIN 1234 or 0000.
  • Use a level-shifter or a simple resistor divider on MCU→HC-05 RX to avoid 5V damage.
  • Test first with an LED before connecting real loads; use relay modules with opto-isolation where possible.
  • Use hardware flow control only if needed; most simple projects work fine at 9600 baud.
  • Keep grounds common and use decoupling capacitors near power pins.

Applications

  • Home lighting and appliance control
  • Remote robot control
  • Smart garden watering systems
  • Wireless test benches and lab automation

Conclusion

Wireless control with HC-05 and ATmega16 is a low-cost, easy-to-learn method to add remote control to your projects. Use simple one-byte commands for reliability, add acknowledgements for two-way safety, and always protect hardware lines with proper drivers and level shifting. Start with an LED or relay module, and expand to multi-device systems or a custom mobile app once the basic flow is stable.

Comments

Post a Comment

Subscribe to Post Comments [Atom]