«

»

Jan 06 2013

Getting Bluetooth modules talking to each other

Before we start, please note that most Apple devices don’t support the virtual serial link profile that these modules use.

A really easy method of making your projects wireless is to use serial Bluetooth modules, these modules take standard serial data and transparently transmit it via Bluetooth to another connected Bluetooth module, no extra code is needed.

Adding Bluetooth to your project so it can be controlled by your phone is easy, just plugging in a slave module and connecting to it from your phone is all it takes, but getting 2 modules to communicate with each other is a bit trickier.

What you need to know
Bluetooth works by having a slave and a master. The slave modules can not initiate a connection to another Bluetooth device, but can accept connections, the master can be be set to either master or slave mode, when in master mode the module can initiate a connection to other devices. Be sure not to make the mistake of buying 2 slaves like I did! 2 masters or 1 master and 1 slave is what you need.
Sometimes eBay sellers don’t put much information about their module, to find out if its a slave or master check the following:

  Slave Master
Number of pins 4 6
Firmware Linvor 1.5 or HC06 HC05

The master modules (~£8) are usually a little bit more expensive than the slave modules (~£6).

bt_modules_labels

The modules contain 2 parts, the back plane and the main Bluetooth board. The main Bluetooth part of the module is designed for 3.3V, the back plane board has a 3.3V regulator built in so supplying 5V for power is fine, but there is no level conversion for the serial data, you probably don’t need to level convert the output (TXD) data since most 5V controllers see 3.3V as high, but the serial input (RXD) does need conversion, a voltage divider or resistor and zener diode will do the job.

Divider Zener
bt_res_div bt_zener

Connecting
To configure the modules you will need a USB to serial converter, search for CP2102 on eBay or DealExtreme and you should be able to find them for less than £2, I’d recommend you get 2 so you can have both slave and master connected to your computer at the same time, make sure at least one of the converters has a 3.3V out as well as 5V out.

Connect the serial converter and Bluetooth modules like so:

Module Converter
VCC 5V
GND GND
TXD RXD
RXD TXD


The serial converters usually use 3.3V signaling for serial, so no level conversion needed.
You will need a serial console program, I use RealTerm.

Configuring
Slave
The slave module can be configured whenever no other BT devices are connected to it, the default baud rate is 9600, no parity, 8 data bits, 1 stop bit. Once you’ve opened the COM port in your serial console, type AT, the module should reply back with OK.
The timing of sending characters is important, if you type too slow the module won’t recognise the command, it will see it as 2 separate commands A and T, its probably best to copy & paste.
To check the firmware copy & paste AT+VERSION and it should respond with OKLinvor1.5.

bt_slave1

Set PIN:
AT+PIN1234

There are only a few available commands for slave modules –

Command Info
AT+BAUD<setting> Set baud rate
1 – 1200
2 – 2400
3 – 4800
4 – 9600
5 – 19200
6 – 38400
7 – 57600
8 – 115200
9 – 230400
A – 460800
B – 921600
C – 1382400
AT+NAME<name> Name – Max 20 characters
AT+PIN<number> PIN – 4 digit number

Master (and master as a slave)
Configuring the master is a little different from the slave, there are 2 ways to put the module into AT mode:

Method 1

  • Turn off module
  • Connect the key pin on the module to 3.3V on the serial converter
  • Turn on module

The baud rate will always be 38400 with this method, the module must also be power cycled to get back into normal mode.

Method 2

  • Turn on module
  • Connect the key pin on the module to 3.3V on the serial converter

The baud rate will be whatever it’s configured to, default 9600, the module can be put in and out of AT mode without being power cycled.

Quick Check
The timing of commands doesn’t matter with the master modules, instead you need to end each command by pressing the enter key (\r\n), sometimes the response will be constantly repeated, pressing enter again will stop it.
Entering AT should return OK, entering AT+VERSION? should return something similar to +VERSION:2.0-20100601

bt_master2

Master as a slave
If you’ve got 2 masters you will need to have one operating as a slave which isn’t too hard to do:

Reset to defaults:
AT+ORGL

Clear any paired devices:
AT+RMAAD

Set PIN:
AT+PSWD=1234

Slave mode:
AT+ROLE=0

Get the address of the device:
AT+ADDR?
Now that you have the address you can skip the AT+INQ and AT+RNAME? parts for configuring the master.

Turn off the module, disconnect key from 3.3V and turn back on.

bt_masterasslave1

Master as a master
Now, to get the master to connect to the slave module you will need to find the address of the slave. Make sure the slave module is powered up and enter these commands into the master module:

Reset to defaults:
AT+ORGL

Clear any paired devices:
AT+RMAAD

Master mode:
AT+ROLE=1

Need to restart after changing role:
AT+RESET

Allow connecting to any address:
AT+CMODE=1

Inquire mode – Standard, stop after 5 devices found, (or) stop searching after 5 seconds:
AT+INQM=0,5,5

Set PIN, should be the same as the slave:
AT+PSWD=1234

Start SPP profile lib (needed to do any Bluetooth transmitting/receiving):
AT+INIT
Note: If it says Error:(17) then that means it’s already loaded, you can carry on.

Start searching:
AT+INQ

A short list of devices found should appear, one of them will be the slave module.

bt_master1

The format of the output is as follows:
+INQ:address,type,signal

Type can be ignored. The signal will be 7FFF since inquire is in standard mode, in RSSI mode you’ll get a load of the same device but with different signal levels which you don’t want at the moment.

Copy the address part of the devices found, for example 12:3:289431 and change the colons to commas – 12,3,289431. To find out which device is the slave module you will need to query each address for its name:
AT+RNAME? <address>

Replace <address> with address of device like 12,3,289431

Response should be +RNAME:linvor if you’re using a normal slave module or +RNAME:HC05 if you’re using a master module as a slave.

Now you’ve got the address of the slave module you need to pair with it:
AT+PAIR=<address>,<timeout>
Note: If you are going to make the master connect to your phone then you need to set the timeout long enough for you to type in the PIN.

Bind address:
AT+BIND=<address>

Only allow connecting to the bound address:
AT+CMODE=0

And now to actually connect:
AT+LINK=<address>

bt_master3

If successful the slave LED should be on solid and the master LED should have a heartbeat flash pattern.

The modules have now been configured! They should automatically connect to each other whenever possible.

A full list of commands for master modules can be found in the download below.

Sending data
Now, if you entered AT mode on the master module by connecting key to 3.3V before turning on the module, you will need to turn off the module, disconnect key and turn it back on.
If you connected key while the module was on then all you need to do is disconnect key.

If you open up 2 serial consoles and have one open the COM port to the slave and the other open the COM port to the master you should be able to type in one and it will appear in the other console, if that worked you just need to plug them in to your projects!

bt_master_slave

Extra stuff
The Bluetooth board of these modules also have connections for SPI, USB and PCM audio, no idea if they work or not.
It also seems to be possible to upgrade the firmware on the modules too, making it possible to put master firmware onto a slave module – http://byron76.blogspot.co.uk/2011/09/i-hope-you-have-good-time-soldering.html

Downloads

LATEST
Download
btinfo_20140510.zip (564.28 kB)
Bunch of pdfs and stuff with Bluetooth commands and info
Downloaded 180 times
MD5: BA64E38C473F7E2E21EC91A92996CFB5

OLD
Download
btinfo_20130106.zip (243.41 kB)
Bunch of pdfs and stuff with Bluetooth commands and info
Downloaded 410 times
MD5: 9E79AD4A022DA1D4A0D694A2F6E35541

18 comments

1 ping

Skip to comment form

  1. James

    Can you send me a hi-res image of the components on the backplane of your bluetooth modules? Or can you get the numbers off the regulator so I can make a backplane? The bluetooth modules I ordered did not come with a voltage regulating backplane and I plan to work in the voltage conversion circuit you diagrammed above so its all compact.

    1. Zak Kemble

      I couldn’t get a decent photo, but the number of the regulator is 662K. The backplane just contains a 3.3V regulator, some capacitors for the regulator, an LED, a resistor for the LED (2.2K), a resistor for pulling down the key pin (10K) and a diode to to protect against wiring power backwards. Any 3.3V regulator that can supply 50mA will work.

  2. bbb

    Do I have to set some setting on the module before use first time with microcontroller?

    1. Zak Kemble

      No, once configured they should just work with the microcontroller.

  3. raheela

    i have interfaced HC05 bluetooth module with arduino.. i also changed the name and password using AT commands but it doesn’t inquire device when i enter AT+INQ command.. plz help me regarding this issue

    1. Zak Kemble

      Does it just reply with OK and no list of devices after a few seconds? And what kind of device is it meant to be looking for (phone, another BT module etc)?

  4. yong

    why i enter command at+init , it does not reply?

  5. yong

    i solved the problem
    but i can’t c other bt module on my device list after enter command at+inq, it only reply ok

    1. Zak Kemble

      Hmm, maybe the CLASS has been changed, try entering this:
      AT+CLASS=0
      and then search again.

  6. NicoHood

    Hi!
    i’ve been searching through almost the whole internet about information how to use the HC-05 as a master. This is the only tutorial i found which explains what commands you actually need to type in. Getting into AT mode wasnt that problem. So thanks for sharing this information! :)

    What i try to do is to connect my HC-05 device with my Wii-Mote. Sadly noone ever did that before with “real hardware”, only with usb doogles and windows/linux software. But what i want to do is to connect with my HC-05 (or whatever else i should buy) with my Wiimote. The HC-05 is connected with my Arduino so i can controll the communication very easy.

    The problem is that i cannot pair the two devices. I read that you dont need that for the Wii-Remote and linking should be fine. But i cant get a connection, it always says FAIL. Getting the Address and the name just works fine. It also responds to the inq. Maybe you can help me or know any website where i can get information/help to get this working?

    The only website i found about how the Wii-Remote works is this:
    http://wiibrew.org/wiki/Wiimote#Bluetooth_Pairing

    It also sais that SPP is NOT supported. Does this mean that i can give this up?

    Here is another Datasheet what covers the Errorcodes, you might want to add this to your post:
    http://www.e-gizmo.com/KIT/images/EGBT-04/EGBT-045MS-046S%20Bluetooth%20Module%20Manual%20rev%201r0.pdf

    I got the HC-05 connected to the PC as slave (just connected after received the package and powered the module). Pairing as master might work, i am not sure. Windows sais error but my toshiba bluetooth stack wants the pin from me and the hc-05 saves the connection. At least my old sony ericsson k610i is connected properly but i do not know how the protocol works, will check this out now. (dont wanna destroy something).

    One thing i want to mention: I was confused: in your tutrial you switched the cmode possibly? the datasheet sais 0 and 1 the other way around. Not sure what is correct.

    It would be nice if you can help me or just tell me that my project just wont work :D
    Nico

    1. Zak Kemble

      If the Wii-remote doesn’t support SPP then these Bluetooth modules won’t be able work with it, don’t waste your time trying it get it so work :P
      CMODE 0 and 1 are the wrong way around, fixed. Though it can probably be left at its default 0 in this situation. Thanks for the datasheet, I’ll add it to the download.

  7. Lars Ole Belhage

    You saved my afternoon – after spending lots of time seaching for linux/mbed/bluez and other farout topics – I found your guide a blessing – 20min’s, and my 2 hc0x’s chatter like teenagers stapped to their breadboards ;)

    Thanks a lot
    Lars Ole

  8. ramin

    thanks for the phantastic tutorial!

    i was just wondering whether it’s possible to connect multiple slaves to one master?
    the idea is to have one “controller” and a couple of slaves that can be controlled by the
    one controller. i know that the standard arduino itself has just one serial port but the question
    is whether the bt master module can connect at all to a couple of slaves.
    i could imagine that if this is possible, we could find some wiring where the arduino
    would actively use the key-pin and direct communication to different slaves by connecting
    to the respective one via some AT commands.

    1. Zak Kemble

      Yeah it’s possible, but only 1 device at a time. The AT+DISC command will make the master disconnect and then you can run through the AT+PAIR and AT+LINK commands to connect to another BT device. Just set the key pin HIGH when you’re dealing with commands and LOW when dealing with data (once you’ve connected). You can do the AT+INQ and AT+PAIR commands while still connected to another device, but make sure you disconnect before the final AT+LINK otherwise the master will bug out.

  9. wesch

    Hi, I’m a beginner with BT, bought my HC-05 today (mounted on a BT Shield V2.2 for Netdiuno). First I’ve tried to see, if my phone can finde the device and it did. I also could connect.
    Then I started to discover it and wrote a small programm to handle AT commands. Since I’ve set Baudrate to 9600 first I couldn’t estabish a connection. Then I set the correct Baudrate, sent AT and got an ERROR back. My idea was, I have something misconfigured with the first Trials, the correct reason was some pending AT commands from previous trials.
    But since the ERROR I decided to send AT+ORGL. I think this was a mistake. The the device name changed, I found another on my phone. Then I changed the Name, AT-NAME? gives me the correct one.
    But now the HC-05 is no more shown on the phone or laptop.

    What do I have to do to get it back shown on other devices?
    I send the following commands:
    AT+ROLE=0
    AT+CMODE=0
    AT+CLASS=0
    AT-STATE? which respondes “INIZIALIZED”, I assume it should be “PAIRABLE”, right? What am I missing.

    1. Zak Kemble

      CMODE and CLASS are only for master mode (ROLE=1). STATE returning “INIZIALIZED” is correct because in slave mode (ROLE=0) the module is only pairable/discoverable after setting the KEY pin low (or disconnecting the KEY pin).

  10. Samuel J Garcia

    Hello! I followed this tutorial and everything worked, congrats and thank you!

    I have a question, maybe you could help me:

    I have a HC-05(Master) and other HC-05(Slave), they are paired and connected, there’s a way to send an AT command thru HC-05(Master) to work with some HC-05(Slave)’s PIO?

    Thank you

    1. Zak Kemble

      No problem :)
      As far as I know there’s no way of directly sending commands to other connected BT modules. You’ll need a microcontroller connected to the slave to receive and store the command, put the slave into command mode and then send the stored command to it.

  1. Telemetry: Bluetooth | Abboud's Corner

    […] Before connecting these modules to the scanner and stm32, you have to set the modes, one to master, and the other to slave. There is already a great blog that describes these steps, which I used to set my modules. Getting-Bluetooth-modules-talking-to-each-other, by Zak Kemble […]

Leave a Reply

Your email address will not be published. Required fields are marked *


1 − 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>