Intro: 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.
- 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
- 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.
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 = "000666412345";
gBtKnownMAC = "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.