loading
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.

Features:
- 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

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

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.

The RGB LED
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

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 (bt2bt.zip) 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.

<p>Hi guys, I am using HC05 modul as a slave in one of my<br>projects. This HC05 is connected to the PC via USB-FTDI interface. That is USB<br>to FTDI and FTDI to HC05. This is supposed to send data to th buetooth of the<br>mobile. However I am not able to do it. The mobile pairs with the HC05 module,<br>but fails to communicate further. HC05 fails to transmit data to mobile. How do<br>I configure this.</p><p>And what does PASSIVE MODE in HC-05 mean (HC05 is passive in<br>slave mode)</p>
<p>First of all, thanks for sharing!</p><p>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?</p><p>Thanks!</p>
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.
<p>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.....</p>
<p>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.</p><p>thank you </p><p>Eduardo castellani</p>
<p>You could check the connection status at regular intervals (lets say, every seconds) using the &quot;GK&quot; command. 1=connected, 0=not connected. </p>
<p>Thank you for your attention, I understand, when the signal goes off, the LED lights, open the proton etc?</p>
<p>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?</p>
<p>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</p><p>Power off HC-05 module.</p><p>Press and hold small button above EN pin.</p><p>Power on and keep pressing small button.</p><p>Small LED should start to blink slowly about once every 2 seconds.</p><p>for more info visit below site:</p><p>http://abratukhin.blogspot.in/2015/04/connect-atmega328-with-hc-05-zs-040.html</p>
Is there a sketch that will allow the Arduino/bluetooth to auto pair to iOS device when in range?
<p>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.</p>
<p>Dear Phillipe,</p><p> 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 (karthikjs27@gmail.com)</p>
<p>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.</p>
<p>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 .</p>
<p>Can I use HC-05 as master with this metode? </p><p>Regard</p><p>Bambangsud</p>
<p>Yes. I have a couple of post on my blog about the HC-05: http://phillipecantin.blogspot.ca/search/label/HC-05</p>
<p>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?</p><p>David</p>
<p>Thanks for posting this! I have implemented this code but I would like to try a different method.</p><p>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?</p>
<p>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.</p>
<p> 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.</p>
<p><strong style="">pcantin</strong></p><p><strong>I <br>found your </strong><strong>BlueTooth Link with auto-detect &amp; </strong><strong>connect on Instructables. Thanks <br>for sharing.</strong></p><p><strong>-------------------------------------------------------------</strong></p><p><strong> I am running into a problem. When I try and <br>verify the code I get the following error:</strong></p><p><strong>C:\Program Files <br>(x86)\Arduino\libraries\SD\src\utility\Sd2Card.cpp:26:17: fatal error: SPI.h: <br>No such file or directory</strong></p><p><strong> #include &lt;SPI.h&gt;</strong></p><p><strong>-------------------------------------------------------</strong></p><p></p><p><strong>I look at Arduino\libraries\SD\src\utility\Sd2Card.cpp:26</strong></p><p><strong>The code in line 26 shows, #include <br>&lt;SPI.h&gt;</strong></p><p><strong>--------------------------------------------------------------</strong></p><p></p><p><strong> If I add #include &lt;SPI.h&gt; in your <br>code it compiles, but states &ldquo;Low memory <br>available, stability problems may occur&rdquo;.</strong></p><p><strong>Sketch uses 14,148 bytes (43%) of <br>program storage space. Maximum is 32,256 bytes.</strong></p><p><strong>Global variables use 1,556 bytes <br>(75%) of dynamic memory, leaving 492 bytes for local variables. Maximum is <br>2,048 bytes.</strong></p><p><strong>--------------------------------------------------------------</strong></p><p></p><p><strong>I am using Arduino Ver. 1.6.1</strong></p><p><strong>This <br>is my first time working with BlueTooth.</strong></p><p><strong>Any help would be greatly appreciated</strong></p><p></p><p><strong>Ron</strong></p>
<p>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.</p>
<p>Is it possible to connect one BlueSMiRF master to several HC-06 slaves at the same time?</p>
<p>Sadly, It's one to one unless you have a BT modules that supports multiple connections.</p>
<p>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?</p>
<p>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.</p>
<p>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.</p>
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?
<p>Hello,</p><p>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?</p><p>Thanks in advance.</p>
<p>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. </p>
<p>I entered AT mode and i applied the commands</p><p>AT+BIND=0000,00,000000</p><p>AT+INT</p><p>AT+PAIR=0000,00,000000,30</p><p>AT+LINK=0000,00,000000</p><p>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<br>So How to link without pairing every time ?? <br>*note 0000,00,000000 ==&gt; mac address of phone</p>
<p>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: http://phillipecantin.blogspot.ca/2014/08/hc-05-bluetooth-link-with-zero-code.html </p>
<p>And Slave (Phone) should just listening while applying AT+BIND=0000,00,000000 ??</p>
<p>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</p>
<p>Hi Could you help me with something in my project ??<br>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 ?? <br>Thanks for your help</p>
<p>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)</p>
<p>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?</p>
<p>According to the user manual (https://www.sparkfun.com/datasheets/Wireless/Bluetooth/rn-bluetooth-um.pdf) it is suported. I have not tried it though.</p>
<p>can you guide me to have a communication between two microcontrollers each having HC-05 bluetooth modules.</p>
<p>I have made a similar link between two HC-05. You can check check my post about it(http://phillipecantin.blogspot.ca/2014/01/hc-05-bluetooth-link-of-2-arduino.html) and get the code here (https://github.com/pcantin/bt2bt_HCa)</p>
Can this code be easily implemented using a simple Bluetooth device such as found here, or does it rely upon using BlueSmirf??<br> <br> &nbsp;<a href="http://www.aliexpress.com/store/product/wholesale-hc-05-HC-05-RF-Wireless-Bluetooth-Transceiver-Module-RS232-TTL-to-UART-converter-and/721071_819803955.html" rel="nofollow">http://www.aliexpress.com/store/product/wholesale-hc-05-HC-05-RF-Wireless-Bluetooth-Transceiver-Module-RS232-TTL-to-UART-converter-and/721071_819803955.html</a><br>
<p>Thank you if you can help me with a sample code, I am newbie and I am studying, I am very grateful.</p>
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

97,854views

77favorites

License:

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: