Connecting the HC-05 Bluetooth Module to a Microcontroller.

About a year ago I ordered an HC-05 bluetooth module on AliExpress. At the time I didn't yet know what I might need it for, but wireless technologies lured me with their mystery.
When I ordered it, there was a whole range of Bluetooth modules on the market, from HC-03 to HC-09, but some of them can work either in master mode (HC-04-M) or in slave mode (HC-06-S), while the HC-05 can work in both modes, so I chose it.
Connecting the HC-05 Bluetooth Module to a Microcontroller.

Let's briefly look at its specifications:

  • Bluetooth chip – BC417143 made by CSR;
  • communication protocol – Bluetooth Specification v2.0+EDR;
  • range – up to 10 meters; in my apartment it works through 2 concrete walls;
  • compatible with all Bluetooth adapters that support SPP;
  • flash memory size (for storing firmware and settings) – 8 Mbit;
  • radio frequency – 2.40 .. 2.48 GHz;
  • host interface – USB 1.1/2.0 or UART;
  • power consumption – current during pairing is 30–40 mA, with an average of about 25 mA. Once the connection is established, the current is 8 mA. There is no sleep mode.

Modules for DIY are usually sold as two soldered-together boards: the smaller one is the Bluetooth module itself, the larger one is an adapter with a voltage regulator and other support circuitry that makes the module usable for DIY.

DIY module pinout:

  • VCC – +5V supply
  • GND – ground
  • TXD, RXD – UART interface pins for communicating with the MCU.
  • KEY – pin for entering AT command mode
  • LED – you can connect an LED with a current-limiting resistor; when a connection between modules is established, the LED will light up.

As soon as the module arrived, I tried to get it working, but the attempt failed. So I decided to remove the heat shrink and reflow it. After reflowing, the module worked, but after the heat shrink was put back on, it stopped working again. I think readers have already guessed that the culprit was the heat shrink covering the antenna. The problem was solved by removing the heat shrink from the antenna.
Connecting the HC-05 Bluetooth Module to a Microcontroller.


To test the module, you need to:

  • power it up
  • connect the tx pin to the rx pin of a usb-uart adapter
  • download the Terminal v1.9b by Bray program
  • establish a connection with the usb-uart adapter at 9600 baud. This speed is set by default in the bluetooth module.


If the program doesn't see the adapter, it has most likely been assigned a COM port number higher than 10; you can change it in Device Manager. After these steps we can receive and send data over bluetooth from the computer.
Connecting the HC-05 Bluetooth Module to a Microcontroller.


Now we need to configure a second device for data exchange; it can be a phone, a laptop or a tablet. I chose an Android tablet; below I'll describe how to configure it for exchanging data over bluetooth.
For our task, download the bluetooth terminal app by Qwerty from the Play Market and install it. Turn on bluetooth and look for our module among the devices.
Connecting the HC-05 Bluetooth Module to a Microcontroller.

Connecting the HC-05 Bluetooth Module to a Microcontroller.

Connecting the HC-05 Bluetooth Module to a Microcontroller.

Try to connect to it; when asked for a password, enter 1234. Once we've connected to the module, the characters we send from the terminal on the tablet will appear in the terminal on the computer.

Having made sure the module works, I connected it to an Atmega16 and wrote a program that performs certain actions when it receives certain characters, for example turning on an LED.

#define F_CPU 8000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
	
ISR(USART_RXC_vect)
{
	switch(UDR)
	{
		case 'f':
			//here you can turn on the LED
		break;
		
		case 'r':
                        //and here turn it off 
		break;
	}	
}

int main(void)
{
	DDRD &= ~(1<<0);  //rx as input
	DDRD |= (1<<1);    //tx as output
        //enable receive, transmit and receive interrupt 
	UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(0<<UDRIE);
        //8 data bits, 1 stop bit, no parity
	UCSRC = (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
        //Baud Rate: 9600
	UBRRL = 0x33;
	sei();

         while(1)
        {
       
        }
}

After turning the LED on a few times, I realized it would be much more convenient not to tap away at the keyboard in the terminal but to create a primitive interface on the tablet for sending commands. I have almost no experience programming for Android, but I still managed to create an interface. You can see the result in the next article.