Introduction: BlueTooth Link With Auto-detect & Connect

Picture of BlueTooth Link With Auto-detect & Connect

This will sow a way to auto-detect and create a link Using BlueTooth, this is a way for a 'master' module to detect a [known] 'slave' module using Arduino and BlueSmirf BlueTooth.

II used the BlueSmirf BlueTooth breakout board in default (slave) mode by connecting with it using an Android phone.  The next logical step was to use the BlueSmirf interface by programming the Arduino. Using a second Bluetooth board, I decided to create a link between two autonomous Arduinos. To make things interesting, I've set a couple of rules for the project. I wanted to heave the same code on both Arduinos and have the whole connection process be automatic. I also wanted the Master device to scan for other devices, retrieve the MAC address, connect and send data.

- Auto-Detect: The Master module will scan, discover and connect to the first [known] Slave Module. 
- Info Light: The RGB LED will help visualize the states of the modules and the connection.
- Master-Slave Jumper: You can quickly set a module type (master or slave) by using a single jumper.
Optional feature:
- Data logging: You can add Micro-SD data logging to the Master and/or the Slave module

Step 1: Part List

Picture of Part List

There is no soldering involve in this project since all connections are done on the breadboards.

Here is the parts list (for two modules -  1 master and 1 slave):
- 2 Arduino UNO boards
- 2 proto shields
- 2 BlueSmirf BlueTooth breakout boards (I use the silver ones)
- 2 RGB LEDs
- 2 sets of jumper (you can also use a simple switch)
- 6 560 Ohm resistors (Green, Blue, Brown)
- 2 10k Ohm resistors (Brown, Black, Red)
Not on the Picture:
- Breadboard wires
- A way to power both modules

Optional parts:
- A Micro-SD breakout board
- A Micro-SD SD card adapter

Step 2: Assembling

Picture of Assembling

The following instructions are all making reference to the image model.

The BlueTooth board
Connect the the bluetooth rx to the Arduino tx and the bluetooth tx to the Arduino rx.  Finishi by connecting the bluetooth GND to the Arduino GND and the bluetooth VCC to the Arduino 5V.
Note: The simplest way to connect the BlueSmirf breakout board is to connect it directly to the RX (Pin0) and TX (Pin1) of the Arduino.  This way we can directly use the Arduino Serial commands.

Use 560 Ohm resistors to connect the LED to the Arduino pins.  Connect the Red, Green and Blue LED pins to the Arduino Pin 5, 6 and 7 (in that order). Connect the LED GND pin to the Arduino GND.

Note: Make sure you know the function of each pins of your LED.  The documentation is not always right (voice of experience).  To be on the safe side, the program will cycle the three colors (Blue, Red and then Green) at start-up. If they don't light up in that sequence, fix it by switching theme around. 

The Switch/Jumper
Connect one side (A) of the Jumper to GND.  Connect the other side (B) to 5v using a 10k Ohm resistor. Connect side B to Pin4 of the Arduino.

Step 3: (Optional) Data-loging

Picture of (Optional) Data-loging

The code that is included in this project is ready to log all the steps to a Micro-SD breakout board.  If you want you can add this to one or both modules.  Here's how:

Using pins 10-13, follow these instructions for the wiring. 

- Connect the 5v pin to the 5V pin on the Arduino
- Connect the GND pin to one of the three GND pin on the Arduino
- Connect CLK to pin 13
- Connect DO to pin 12
- Connect DI to pin 11
- Connect CS to pin 10

Step 4: Code

The included code ( is for both Master & Slave modules.  The code will execute as Master or Slave depending on the Jumper (or switch) state.  On/closed is the master while off/open is the slave.

IMPORTANT: You must edit the code to add you BlueTooth devices MAC addresses into the list of 'trusted' addresses.  To do that set the length of the gBtKnownMAC variable to the total number of devices you want to scan.  Then go in the BtInit() function and add your addresses into the gBtKnownMAC list.  Here is an example where 2 addresses are added:

  gBtKnownMAC[0] = "000666412345";
  gBtKnownMAC[1] = "000666454321";

Upload this code to both modules and move to the next step.

If you want to play further with the code, read the following tips first:
- Read the User Manual (I included a copy)
- Serial speed: By default, BlueSmirf is set to 115200 so you need to call Serial.begin(115200); before sending the first command.
- The “$$$” command is the only one that is not followed by a carriage return. Why? To complicate things of course. Use print() for the “$$$” command and println() for all other commands.
- Read and validate all the responses that are sent back.
- Wait 100ms delay after each command you send and before trying to read the response. This will give Bluesmirf the time to process the command.
- Don't forget to exit the command mode (using “---”). Some commands are not effective until you have exited the command mode (e.g. MS command).

Step 5: Using It

To create the link power-up both modules at the same time.

- The info lights will flash Blue, Red and Green, 
    - When the power is ON a red light will appear on the BlueTooth boards
- The lights will then turn Red.  Indicating that there's no connection
    - While the master is inquiring, the red light on the BlueTooth boards will be flashing rapidly
    - While the slave is waiting for a connection, the red light on the BlueTooth boards will be flashing
- After the Master is done Inquiring for devices, its light will turn Blue
- When the Master identifies a known Slave device, its light will turn Cyan
    - When the connection is made a Green light will appear on both BlueTooth boards

Note: When the connection is made, the Master's light will be Green and the Slave will be Blue.

After the connection is made, data is sent by the Master and received by the salve.  The lights are flashing to indicate these events.

Go here  to see video of this in action.


AshishS167 (author)2016-10-06

Hi guys, I am using HC05 modul as a slave in one of my
projects. This HC05 is connected to the PC via USB-FTDI interface. That is USB
to FTDI and FTDI to HC05. This is supposed to send data to th buetooth of the
mobile. However I am not able to do it. The mobile pairs with the HC05 module,
but fails to communicate further. HC05 fails to transmit data to mobile. How do
I configure this.

And what does PASSIVE MODE in HC-05 mean (HC05 is passive in
slave mode)

arduinofanboy (author)2016-09-26

First of all, thanks for sharing!

I have a quick question: how would you go about using what you did above but also determining RSSI once connected? I am trying to make the master device sense if there is a slave device within around 4 ft or so, then the slave sends a simple signal (like turn on an LED on the master module). The tricky part is that I want the slave to be able to pass by quickly and still send that data. Will it be able to connect fast enough and could I implement multiple slave modules?


SupyanI (author)2016-08-09

Help me, I have a problem for connection with 4 arduino and can to connect to others with distance measure ex, 2 mater or others.

febin13 (author)2016-07-07

hi,i am a beginner to bt modules.. i need to sent alphabets from one arduino to an other say 's' and blink an led......plz help.....

miragempro (author)2014-03-13

Hello, I wonder if you can help me, I need to configure the Bluetooth so that when he loses the signal (when I turn away more than a meter, bluetooth / arduino he lights a LED., Or would like to get away, realizing signal dropout and regulate it, for example, after I verify that a given signal, a yard, gets a value, I put in the program and when it reached this value, the LED would light, or erase.

thank you

Eduardo castellani

pcantin (author)miragempro2014-03-13

You could check the connection status at regular intervals (lets say, every seconds) using the "GK" command. 1=connected, 0=not connected.

miragempro (author)pcantin2016-05-13

Thank you for your attention, I understand, when the signal goes off, the LED lights, open the proton etc?

jackzylkin (author)pcantin2015-06-02

I too have tried to use the GK command, with no luck. The problem is, it is impossible to get the RN42 to go into command mode when it is connected in HID mode. I tried it as a master and as a slave, and changed the config timeout to 255 (no timeout), but it still refuses to go into command mode after a connection is made. Instead, it just forwards $$$ as it would any other set of characters. Any advice?

bhuvaneshnick (author)2016-02-12

hi, if u having trouble in making into AT mode of HC-05 (ZS-040)(especiall if one having en/wakeup up pin instead of key pin). Follow they bellow procedure

Power off HC-05 module.

Press and hold small button above EN pin.

Power on and keep pressing small button.

Small LED should start to blink slowly about once every 2 seconds.

for more info visit below site:

JoshH17 (author)2015-12-29

Is there a sketch that will allow the Arduino/bluetooth to auto pair to iOS device when in range?

zindar (author)2015-10-26

Hi Phillipe, (using ProMicro and SMiRF Silver) I started with your post on simple Arduino-Bluetooth-link. That code is so simple yet it did not work for me. I tried setting master to SM,3 and that did nothing. Now I tried this post and I still cannot get my SMiRFs to communicate with each other. I must be missing something really simple. I can connect to either SMiRF using my phone, so I think it is authentication because the phone at least asked for it, and this code does not mention it. How might I add authentication to either code example from you? But you also mention a jumper in the video but not in the online text. What would you suggest I try? Your online posts are amazing and I ordered 2 HC-05s just to try your other posts. Thank you.

Karthikjs27 (author)2015-10-23

Dear Phillipe,

This is karthik, I need an urgent help from you. I need to know, how to make a knot via machine using two same side threads like our fingers. shall we use any stepper motors or anything else. Please give me some ideas. I need your contact mail, Please reply me back. I'm pleasure to waiting for you reply. This is my mail (

pcantin (author)Karthikjs272015-10-23

This sounds like a nice problem to solve but I have no spare time right now to add a new project. I would suggest to google on the subject of knot making robots. Don't forget that the mechanism for sewing machine (kind of) makes knot in a very simple way. Good luck with your project.

khan25 (author)2015-08-18

Dear sir i want your help in our project. me want to connect arduino uno r3 board with different shimmer sensors at a time and uno board auto detect and connect and save the data and when other device want that data they send it .. plz send me logic of programming of this code. i am w8ng for your positive and early response .

BambangS1 (author)2015-08-01

Can I use HC-05 as master with this metode?



pcantin (author)BambangS12015-08-02

Yes. I have a couple of post on my blog about the HC-05:

Cadasse (author)2015-06-25

Hi. I have a weird question. I have a bluetooth speaker (JBL) and I'm looking for a way to connect it with a bluetooth microphone, so I could use this system to speak in meetings and conferences. Trouble is : it's two slave items if I've right understood the bluetooth system. Is there any way to link both item ? Maybe with a computer?


khaleeesi17 (author)2015-03-22

Thanks for posting this! I have implemented this code but I would like to try a different method.

I am trying to use Autoconnect master mode on one smirf but it keeps disconnecting after a period of time. In this mode, shouldn't it automatically try to connect again?

pcantin (author)khaleeesi172015-03-23

I would have to revisit this project and build a more serious library that would also do that. For now this simple code doesn't handle error recovery like disconnection.

cnsv16 (author)2015-03-22

I am trying to pair two HC-05, using your code, but I was not able to pair. pair. I guess it is due to master is not sending the pairing code for slave.

2n2222 (author)2015-03-20


found your
BlueTooth Link with auto-detect & connect on Instructables. Thanks
for sharing.


I am running into a problem. When I try and
verify the code I get the following error:

C:\Program Files
(x86)\Arduino\libraries\SD\src\utility\Sd2Card.cpp:26:17: fatal error: SPI.h:
No such file or directory

#include <SPI.h>


I look at Arduino\libraries\SD\src\utility\Sd2Card.cpp:26

The code in line 26 shows, #include


If I add #include <SPI.h> in your
code it compiles, but states “Low memory
available, stability problems may occur”.

Sketch uses 14,148 bytes (43%) of
program storage space. Maximum is 32,256 bytes.

Global variables use 1,556 bytes
(75%) of dynamic memory, leaving 492 bytes for local variables. Maximum is
2,048 bytes.


I am using Arduino Ver. 1.6.1

is my first time working with BlueTooth.

Any help would be greatly appreciated


pcantin (author)2n22222015-03-20

It looks like you are having problems using the standard libraries. Maybe you should try starting a brand new project and only include (and use) some simple library like Servo.

marina.minkin (author)2015-03-18

Is it possible to connect one BlueSMiRF master to several HC-06 slaves at the same time?

pcantin (author)marina.minkin2015-03-18

Sadly, It's one to one unless you have a BT modules that supports multiple connections.

returner (author)2015-01-20

Hi, i have a question. i hope you dont mind answering it. Will this program still work if i were to use a HC-05 as master and HC-06 as slave instead?

pcantin (author)returner2015-01-20

Yes. As I understand it, HC-06 can only be a slave while HC-05 can be both. You would only skip trying to access the HC-06 in command mode since it is already a Slave.

returner (author)pcantin2015-01-21

Hi, i have a question. how does the arduino actually know actually know which data it is going to send or receive? in which part of the code does it do that? i want to make two arduinos one having a HC05 as a master sending data over to a HC06 slave on two different laptops. I hope you can answer me.

returner (author)pcantin2015-01-20

Hi thanks for your reply! I have another question if you dont mind. I have been wanting to send a 'char' or a 'string' from my HC06 which is connected to my arduino at one laptop to my other Bluetooth module HC05 which is connected to an arduino in another computer. I have been reading up on using the Bluetooth modules but I do not seem to understand on how the codes work. Could you explain to me how the code to send the data and receive goes?

pradipta03 (author)2015-01-14


I was willing to make a robot that communicates in both directions. I mean both will talk to each other and send data to each other to take dicissions. Will that be possible with one slave and one master? As far as I understand it means that both has to be in amaster mode, right? How can I use both master device?

Thanks in advance.

pcantin (author)pradipta032015-01-14

Yes it is possible to send data both ways while using a master and a slave device. In fact it the only way. The Master initiate the connection with a Slave. Once the connection is created both device can use this new serial communication port to send and receive data.

mohamedS15 (author)2014-12-02

I entered AT mode and i applied the commands





and it works but after disconnecting power and reconnect it again if i applied same code without pairing it linking but no data transmitted or received and if i applied the command AT+FSAD=0000,00,000000 it returns ok
So How to link without pairing every time ??
*note 0000,00,000000 ==> mac address of phone

pcantin (author)mohamedS152014-12-03

Out of those four AT commands, the only one you need to call is the AT+BIND command on the master. Then your code should not call any AT command because the connection will be initiated automatically. The step to configure the slave and master are here on my blog:

mohamedS15 (author)pcantin2014-12-03

And Slave (Phone) should just listening while applying AT+BIND=0000,00,000000 ??

pcantin (author)mohamedS152014-12-04

That's how I was able to do it between two HC-05. I have not try the auto connect with a phone yet. I'll try to get to this next weekend

mohamedS15 (author)2014-11-29

Hi Could you help me with something in my project ??
I want to connect auto connect between arduino(Master) and my smartphone(Slave). I entered AT mode made HC-05 as master initialize it and made it linked with address to phone while it is listening after linking how to go back to BT mode with that link to start send and recive data ??
Thanks for your help

pcantin (author)mohamedS152014-11-29

Yes this is possible, all you need is to know the MAC address of your phone's bluetooth. Also you may have to manually accept the connection request on the phone and you may have to enter the 4 digit pin of your HC-05(the default is 1234)

tanzimmubarrat (author)2014-10-24

Hi.... Could you help me with something? I want to pair a master RN-42 module with a slave HC-05 module? How can it be done?

pcantin (author)tanzimmubarrat2014-10-24

According to the user manual ( it is suported. I have not tried it though.

hemendraB (author)2014-04-07

can you guide me to have a communication between two microcontrollers each having HC-05 bluetooth modules.

pcantin (author)hemendraB2014-04-08

I have made a similar link between two HC-05. You can check check my post about it( and get the code here (

Littlegator (author)2013-05-24

Can this code be easily implemented using a simple Bluetooth device such as found here, or does it rely upon using BlueSmirf??

miragempro (author)Littlegator2014-03-14

Thank you if you can help me with a sample code, I am newbie and I am studying, I am very grateful.

pcantin (author)Littlegator2013-05-24

This should be compatible with All Roving Networks modules, super modules, serial adapters and USB dongle (RN-USB-X) since they all share the same command interface. On the other hand the HC-X module (if I'm not mistaken) works with the AT command set.

About This Instructable




Bio: I strive to be a polymathic person interested in a range of subjects which positions me as an Innovator, Problem Solver and Solution Designer.
More by pcantin:BlueTooth Link with auto-detect & connect
Add instructable to: