- Due to the move to our new headquarters, shipments will not be processed on Friday October 3th. Please, for any urgent matters, process your order prior October 2th. -
libelium
  • rss
  • flickr
  • instagram
  • twitter
  • facebook
  • youtube

Bluetooth Module for Arduino Tutorial

Contents

Introduction

Ingredients:

Difficulty: Medium - medium

Preparation Time: 45 minutes

 

NOTE: If you are interested in Wireless Sensor Networks (WSN), M2M and the Internet of Things (IoT) projects check our new open source sensor platform: Waspmote which counts with more than 70 sensors available to use and a low consumption mode of just 0.7uA to ensure years of battery life. Know more at:

NOTE: The codes of the tutorial have developed to work on Arduino IDE v1.0.1

Step 1: The Bluetooth module:

The Bluetooth module can be connected in a XBee shield for Arduino.

Technical characteristics:

Bluetooth Chip: eUnistone 31308/2
Version: Bluetooth 2.0 + EDR (Configurable BT 1.2)
TX Power: 2.5dBm
RX Sensitivity: -86dBm
Antenna: 2dBi
Outdoor Range: 60 m LOS (Line of sight)
Indoor Range: 40 m NLOS (Non line of sight)

(Range measured between the Bluetooth module for Arduino and the BT Linksys dongle)

Special Options:

  • Minimum and Maximum RSSI thresholds control
  • Adaptive Frequency Hopping
  • Security: Authentication, Pairing and Encryption
  • Store 5 different trusted devices in its internal memory
  • Master or Slave operation supported

Step 2: Device discovery:

For the device discovery example we are going to connect the Bluetooth module in Gateway mode. We'll need an Arduino and a XBee shield. For the gateway we must take the microcontroller (Atmega328) out from the arduino and set the jumpers to USB position.

Using the gateway connection we can send / receive AT commands directly from our PC to the Bluetooth module.

One of the devices we'll find is a Linksys USB-Bluetooth adapter connected to a PC.

Once the gateway is ready we connect it to the USB port and open the serial port terminal (Cutecom) using 115200 bps, 8 data bits,1 stop bit, no parity and no flow control.
The AT command for discovering devices is JDDS, the command to send is AT+JDDS=0 .
The response to this command will be a list of the bluetooth devices close to my module.

For each device we get its Bluetooth address (12 hexadecimal numbers), Remote name and class.

NOTE: We recommend not to run the command ' AT+JPRO ' because it may leave the BT module in non-working mode .

Use this command only if you have an advanced level with this type of technologies and under your responsibility.

Production mode is used for configuration and test purposes, the production mode shall be entered in order to execute the following commands:
AT+JDOI: DOwnload EEPROM Image
AT+JEDT: Enable Device under Test
AT+JCAC: Crystal Auto Calibration
AT+JCBD: Change BD_Data

General procedure to use production mode related commands:
1. HW reset
2. Enter production mode (AT+JPRO=1)
3. Execute command (e.g. “AT+JDOI”)
4. Exit production mode (AT+JPRO=0)
5. HW reset

This process must be done correctly or the module could be broken.

Step 3: Service discovery:

Service discovering is a tool that we can use to discover services in a bluetooth device.
For example, we add Serial port service to our Bluetooth PC (Linksys Bluetooth Adapter) using the command sdptool add SP .

Now we can connect the bluetooth module in gateway mode to the PC and launch Cutecom.
We discover bluetooth devices (AT+JDDS=0) and then we discover the Serial port service in the Bluetooth address 0016B6EE445D.

The command used is AT+JSDS=0016B6EE445D,1101
0016B6EE445D is the bluetooth address
1101 is the serial port service code

The module answers with the Serial ports detected in the other device.

Step 4: Connect without security:

In this basic example we'll use an Arduino (with Atmega microcontroller) with bluetooth module (Connected using a XBee shield) and a PC with a bluetooth USB dongle (Linksys Bluetooth Adapter).

The Arduino will send a message (Hola caracola...) every 2 seconds and we receive it in the PC.

Arduino code:

/*
*  Sending data using Bluetooth module from Libelium for Arduino
*  Connect without security
*
*  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
*  http://www.libelium.com
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*  Version 0.1
*  Author: Marcos Yarza
*/


int led = 13;
int val = -1;

void setup(){
	delay(2000);
	Serial.begin(115200);
	delay(2000);
	Serial.print("AT+JSEC=1,1,1,04,1111\r\n"); // Enable security command
	delay(2000);
	Serial.print("AT+JDIS=3\r\n"); // Discorable command
	delay(2000);
	Serial.print("AT+JRLS=1101,11,Serial Port,01,000000\r\n"); // Register local sevice command
	delay(2000);
	Serial.print("AT+JAAC=1\r\n");// Auto accept connection requests command
	delay(2000);
	pinMode(led, OUTPUT);
	digitalWrite(led,HIGH);
	Serial.flush();
	val = Serial.read();
	while (val != 'R'){
	  val = Serial.read();
	}
	delay(1000);
	Serial.print("AT+JSCR\r\n"); // Stream Connection Request command
}

void loop(){
	Serial.println("Hola caracola...");
	delay(2000);
}

Once we have the code uploaded in the Arduino, we have to connect the XBee shield with the Bluetooth module and power ON the Arduino. We use an external power supply.

Setting up the PC

In this example we use a Debian Linux PC with an USB Linksys Bluetooth Adapter From a terminal we launch these commands:

Command  
hcitool scan Scan the Bluetooth devices. We'll use the MAC addres of the Bluetooth module in Arduino (In this case 00:03:19:05:2B:9A)
sdptool search SP Discover the channel (In this case channel 1)
rfcomm connect 0 00:03:19:05:2B:9A 1 Connect to the Bluetooth module MAC: 00:03:19:05:2B:9A channel:1

Now we have connected the PC bluetooth with the Arduino module, as we can see, the serial port will be /dev/rfcomm0. So we can connect this port using a serial port monitor (Cutecom - 115200 bps, 8 data bits no parity 1 stop bit) and receive data from Arduino with bluetooth module:

Step 5: Connect with security PIN:

This example is very similar to the previous one, with the difference that now we use the PIN security for the connection between Bluetooth module for Arduino and PC.

Arduino code:

/*
*  Sending data using Bluetooth module from Libelium for Arduino
*  Connect with security PIN
*
*  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
*  http://www.libelium.com
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*  Version 0.1
*  Author: Marcos Yarza
*/


// variables
int led = 13;
int val = -1;

void setup(){
	delay(2000);
	Serial.begin(115200);
	delay(2000);
	Serial.print("AT+JSEC=3,1,1,06,123456r\n"); // Register local sevice security PIN activated
	delay(2000);
	Serial.print("AT+JDIS=3\r\n"); // Discoverable Inquiry & Page Scan enabled
	delay(2000);
	Serial.print("AT+JRLS=1101,11,Serial Port,01,000000\r\n"); // Register local service
	delay(2000);
	Serial.print("AT+JAAC=1\r\n"); // Auto accept
	delay(2000);
	Serial.print("AT+JPCR=06,123456\r\n"); // PIN code reply
	delay(2000);
	pinMode(led, OUTPUT);
	digitalWrite(led,HIGH);
	Serial.flush();
	val = Serial.read(); // wait for connection
	while (val != 'R'){
	  val = Serial.read();
	}
	delay(1000);
	Serial.print("AT+JSCR\r\n"); // Stream Connection Request
}

void loop(){
	digitalWrite(led, LOW);
	Serial.println("Hola caracola (connected with PIN 123456)...");
	delay(1500);
	digitalWrite(led,HIGH);
	delay(500);
}

Setting up the PC

From a terminal we launch these commands:

Command  
hcitool scan Scan the Bluetooth devices. We'll use the MAC addres of the Bluetooth module in Arduino (In this case 00:03:19:05:2B:9A)
rfcomm connect 0 00:03:19:05:2B:9A 1 Connect to the Bluetooth module MAC: 00:03:19:05:2B:9A channel:1

When the PC try to connect to the module, we get a Pin request window.

We type the PIN code "123456" and the connection is established. We can open Cutecom and check the messages sent from the Bluetooth Module for Arduino.

Step 6: Connecting two Bluetooth modules:

Here we're going to connect a standalone Arduino with a PC (Arduino USB gateway).
Power ON Arduino with Bluetooth module, when the LED becomes ON you can send commands to the Bluetooth module in the gateway.

Commands for the USB gateway module
AT+JSEC=1,1,1,04,1111
AT+JCCR=0003190d1064,01 ==> 0003190d1064 is the MAC address of the Bluetooth in Arduino
AT+JSCR
After each command you must send <LF><CR>

Arduino code :

/*
*  Sending data using Bluetooth module from Libelium for Arduino
*
*  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
*  http://www.libelium.com
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>
*
*  Version 0.2
*  Author: Marcos Yarza
*/

int led = 13;
int val = -1;

void setup(){

	Serial.begin(115200);
	 pinMode(led, OUTPUT);
	digitalWrite(led,LOW);
	delay(4000);
	Serial.print("AT+JRES\r\n");
	val = Serial.read();
	while(val != 'R'){
	 val = Serial.read();
	}
	delay(500);
	Serial.print("AT+JSEC=1,1,1,04,1111\r\n"); // Register local sevice
	val = Serial.read();
	while(val != 'O'){
	 val = Serial.read();
	}
	delay(500);
	Serial.print("AT+JDIS=3\r\n");
	val = Serial.read();
	while(val != 'O'){
	 val = Serial.read();
	}
	delay(500);
	Serial.print("AT+JRLS=1101,11,Serial Port,01,000000\r\n");
	val = Serial.read();
	while(val != 'O'){
	 val = Serial.read();
	}
	delay(500);
	Serial.print("AT+JAAC=1\r\n");
	val = Serial.read();
	while(val != 'O'){
	 val = Serial.read();
	}
	delay(2000);
	pinMode(led, OUTPUT);
	digitalWrite(led,HIGH);

	val = Serial.read();
	while (val != '+'){
	 val = Serial.read();
	}

	digitalWrite(led,LOW);
	delay(1000);
	Serial.print("AT+JSCR\r\n"); // Stream Connection Request
}

int count = 0;

void loop(){

	Serial.print("Hola caracola...");
	Serial.print(" Counter: ");
	Serial.println(count);
	count++;
	digitalWrite(led,HIGH);
	delay(2000);
	digitalWrite(led,LOW);
	delay(500);
}

Step 7: Video-Tutorial:

Here's an explanatory video, which shows the whole process developed in this tutorial:

 

 

Fritzing Libraries

 

Bluetooth Module for Arduino

Download download

Bluetooth Module for Arduino is able to be plugged into the XBee Shield and get a serial communication between the computer and an Arduino board through Bluetooth protocol.

You can download our Fritzing libraries from this area .

 

Links and Documentation