Close

Step 3: Bluetooth modules setup.

A project log for Bluetooth Doorlock

A wireless door lock key pad and receiver using AVR and HC-05 Bluetooth modules.

david-droletDavid Drolet 08/16/2014 at 18:060 Comments

Once the modifications to the boards are done, they now have to be setup properly. My code is divided in 3 parts under main(), selectable by a definition. Define either SETUP , SLAVE or MASTER in order to compile the board. The Bluetooth modules are setup using the keypad box , using the SETUP compilation.

You need to modify the code to fit your boards. The steps needed are :

I used my logic analyzer to view the module's response to my commands , but it is not absolutely necessary. Since the keypad box doesn't have a display, there is no output to tell you if the command was received properly. The HC-05 firmware has 2 LEDs to show its status, and they are on a different pin than the HC-06 firmware, so the breakout board is not wired properly. I did not modify the board to have the LED working because I do not need it in my project. I did use my logic probe to check the status of pin 31 to confirm the module was in AT mode. AT mode is required to setup the modules. While in this mode , the module will not output the data it receives , but instead it uses it to setup some parameters for its normal working mode.

There are 2 modules needed , the master module and the slave module. You cannot connect 2 slave modules together, hence the reason why the HC-06 firmware was not good for my project. In this project , the modules are bound to each other. No other devices can connect to them and send data. To bind the modules, you need the other module's address. There are a few ways you can acquire the addresses. The easiest is simply by powering your module and trying to connect to it to see its address. I used my android phone with blueterm+ and was able to quickly see the addresses. Using your PC to connect to it will also work. The default password is 1234 of course. If you have a logic analyzer you can use it to read the response of the module when you press key 7 on the keypad flashed with the SETUP program. The baud rate for the AT mode is always 38400 8N1. You can also get the addresses when you are flashing the HC-05 firmware,

One of my module address is : 2014 04 159158 but when you send the command AT+ADDR? The modules responds 2014 4 159158 , dropping the lead 0 of the 2nd part of the string.

You need to change the code for keys 2 and 3 to reflect the module's addresses with the address corresponding to your module. According to the datasheet the format is : AT+BIND=1234,56,abcdef\r\n

Here is the code for key 2, which is used to bind the slave module to the master module's address :

if (buffer == 2) // Bind to master
{
tot_overflow = 0;
USARTWriteChar('A');
USARTWriteChar('T');
USARTWriteChar('+');
USARTWriteChar('B');
USARTWriteChar('I');
USARTWriteChar('N');
USARTWriteChar('D');
USARTWriteChar('=');
USARTWriteChar('2');
USARTWriteChar('0');
USARTWriteChar('1');
USARTWriteChar('4');
USARTWriteChar(',');
USARTWriteChar('0');
USARTWriteChar('4');
USARTWriteChar(',');
USARTWriteChar('1');
USARTWriteChar('5');
USARTWriteChar('3');
USARTWriteChar('9');
USARTWriteChar('2');
USARTWriteChar('9');
USARTWriteChar(13);
USARTWriteChar(10);

}

You can now compile the SETUP program, flash it to the keypad box and be ready to setup both of your modules. Connect the module you want to use as master to the keypad box. Connect the KEY pin to 3.3v and power on the module. Now press (wait a second between each presses) :

The master module is ready! Now power off and disconnect it. Now connect the module that is to be used as slave, power on and press :

The slave module is ready! From now on , when both modules have power , and have established a connection (takes a few seconds) whatever appears on the RX line of one module will then appear on the TX line of the other module! It couldn't be easier to send data wirelessly!

Discussions