What?
Bluetooth is a wireless technology standard for exchanging data over short distances(using short-wavelength UHF radio waves in the ISM band from 2.4 to 2.485 GHz from fixed and mobile devices and building personal area networks(PANs Invented by telecom vendor Ericsson in 1994[5 it was originally conceived as a wireless alternative to RS-232RS-232 data cables It can connect up to seven devices overcoming problems that older technologies had when attempting to connect to each other
How?
A Bluetooth device uses radio waves instead of wires or cables to connect to a phone or computer. A Bluetooth product, like a headset or watch, contains a tiny computer chip with a Bluetooth radio and software that makes it easy to connect. When two Bluetooth devices want to talk to each other, they need to pair. Communication between Bluetooth devices happens over short-range, ad hoc networks known as piconets. A piconet is a network of devices connected using Bluetooth technology. When a network is established, one device takes the role of the master while all the other devices act as slaves. Piconets are established dynamically and automatically as Bluetooth devices enter and leave radio proximity.
Communication and connection
A master Bluetooth device can communicate with a maximum of seven devices in a piconet (an ad-hoc computer network using Bluetooth technology), though not all devices reach this maximum. The devices can switch roles, by agreement, and the slave can become the master (for example, a headset initiating a connection to a phone necessarily begins as master—as initiator of the connection—but may subsequently operate as slave).
At any given time, data can be transferred between the master and one other device. The master chooses which slave device to address. Since it is the master that chooses which slave to address, whereas a slave is (in theory) supposed to listen in each receive slot, being a master is a lighter burden than being a slave. Being a master of seven slaves is possible; being a slave of more than one master is possible.
We need to configure both the modules. In order to do that we need to switch to AT Command Mode and here’s how we will do that.
First we need connect the Bluetooth module to the Arduino as the circuit schematics. Note that the “EN” pin of the Bluetooth module is connected to 5 volts and TX/RX pins goes to TX/RX of Arduino board. i.e. Tx -> Tx and Rx -> Rx
If the Bluetooth module led is flashing every 2 seconds that means that we have successfully entered in the AT command mode.
After this we need to upload an empty sketch to the Arduino but don’t forget to disconnect the RX and TX lines while uploading. Then we need to run the Serial Monitor and there select “Both NL and CR”, as well as, “38400 baud” rate which is the default baud rate of the Bluetooth module. Now we are ready to send commands and their format.
Slave Configuration
Type just “AT” which is a test command we should get back the message “OK”. Then type “AT+UART?” we should get back the massage that shows the default baud rate which is 38400. Then type “AT+ROLE?” we will get back a massage “+ROLE=0” which means that the Bluetooth device is in slave mode. If we type “AT+ADDR?” we will get back the address of the Bluetooth module and it should looks something like this: 98d3:31:3069b0
Now we need to write down this address as we will need it when configuring the master device. Actually that’s all we need when configuring the slave device, to get its address, although we can change many different parameters like its name, baud rate, pairing password and so on, but we won’t do it right now.
Master Configuration
First we will check the baud rate to make sure it’s the same 38400 as the slave device. Then by typing “AT+ROLE=1” we will set the Bluetooth module as a master device. After this using the “AT+CMODE=0” we will set the connect mode to “fixed address” and using the “AT+BIND=” command we will set the address of the slave device that we previously wrote down.
Note here that when writing the address we need to use commas instead of colons. Also note that we could have skipped the previous step if we entered “1” instead of “0” at the “AT+CMODE” command, which makes the master to connect to any device in its transmission range but that’s less secure configuration.
Verifying the configuration
That’s all we need for a basic configuration of the Bluetooth modules to work as a master and slave devices and now if we reconnect them in normal, data mode (i.e. disconnecting “EN” pin),
and re-power the modules, in a matter of seconds the master will connect to the slave. Both modules will start flashing every 2 seconds indicating a successful connection.
Let’s begin with transferring data from one Arduino to another using HC05 Bluetooth module.
In this example, we have attached one push button to master device. On pressing the button, it sends the single BYTE HIGH data to the slave device. When button is not pressed, it simply sends the single BYTE LOW data.
On the other side i.e. slave, upon receiving the HIGH BYTE, slave turns on the LED. If LOW BYTE is received, LED remains off.
Master Code
#include <SoftwareSerial.h>
#define PUSH_BUTTON_PIN 2
SoftwareSerial sigmaSS(9,10);
void setup() {
pinMode(PUSH_BUTTON_PIN, INPUT_PULLUP);
sigmaSS.begin(9600);
}
void loop() {
if(digitalRead(PUSH_BUTTON_PIN) == HIGH){
sigmaSS.write('1');
}
else{
sigmaSS.write('0');
}
delay(10);
}
Slave Code
#include <SoftwareSerial.h>
#define LED_PIN 3
SoftwareSerial sigmaSS(9,10);
char pushValue = 0;
int counter = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
sigmaSS.begin(9600);
}
void loop() {
if(sigmaSS.available() > 0){
pushValue = (char)sigmaSS.read();
}
if(pushValue == '0'){
digitalWrite(LED_PIN, HIGH);
}
else{
digitalWrite(LED_PIN, LOW);
}
delay(10);
}
File and Image transfer between two Arduinos can be performed using two Bluetooth modules.
In this example, we will use HC-05 and HC-06 to transfer two files between two Arduinos. The master device sends test.txt and mypicture.jpg files to slave device.
The storage method is SD card in both sending and receiving ends. We can have two options to connect SD card to Arduino:
1)Using SD Card module
2)Using Ethernet Shield
We choose the second option
Algorithm:
Master side: Open the SD card, read test.txt file and send the data stream to slave device via HC-05. Repeat the same for mypicture.jpg file. Report any error during opening and reading SD card.
Slave side: Keep Listening to any incoming messages from master via HC-06. As soon as data stream is received, open the SD card, write first file and close. Repeat the same for second file. Report any error during opening and writing SD card.
PC to Arduino text file transfer using Bluetooth HC-05:
1) Insert SD card into Adapter and "Open folder to view files"
2) Look for the file "PC_FILE.txt". You won't find it as it is not present yet in SD card :-)
3) Take out the card and plug it into the Ethernet shield
4) Upload the below sketch in Arduino and note down the COM port of Arduino
5)Open hyperterminal program. i.e. Tera Term (Good for serial communication). You can get it here: http://download.cnet.com/Tera-Term/3000-20432_4-75766675.html
6) Select the COM port as shown in the picture:
7) Go to Setup > Terminal
8) Set Receive and Transmit values as CR+LF and press Ok as shown below:
9) Go to File > Send File option
10) Choose the text file to send. And press Open
Note:
1) File EOF (End Of File) marker MUST be ‘$’
2) Don’t choose large files
3) File type should be .txt
Sample- t.txt might look like below:
Hi this is text file. I will go to Arduino from PC
$
Note the ‘$’ at the End of file. This is called EOF marker
11) Now the process will start and the file will be written to the SD card.
12) Take out the SD card from Ethernet shield and insert into your computer and locate PC_FILE.txt file. Now you should be able to locate it :-). This is the file that came from PC via serial transfer. i.e. the copy of t.txt
Now open both the file i.e. t.txt and PC_FILE.txt side by side in notepad. And compare the contents. Both are same !!!
Conclusion: File t.txt has been successfully transferred from PC to Arduino using Bluetooth Serial communication
Sketch:
#include <SoftwareSerial.h>
SoftwareSerial sigmaSS(8,9); // Rx,Tx
#include <SPI.h>
#include <SD.h>
char EOF_MARKER = '$'; //End of file Marker. File must contain this character at the END
File myFile;
char ch;
boolean foundEOF = false;
void setup(){
Serial.begin(9600);
sigmaSS.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("Initialization Failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("PC_FILE.txt", O_CREAT | O_TRUNC | O_WRITE);
}
void createStr(){
foundEOF = false;
int idx = 0;
while(1){
if(Serial.available()){
ch = (char)Serial.read();
if(ch == '$'){
Serial.flush();
if(myFile) myFile.close();
foundEOF = true;
break;
}
if(myFile) myFile.print((char)ch);
}
}
if(foundEOF){
Serial.println("Found EOF and Written to SD card");
}
else{
Serial.println("Could not find EOF character in file !!");
}
Serial.flush();
//for(;;);
}
void loop(){
if(Serial.available()){
createStr();
}
}
Sample t.txt file:
int soundDetectedPin = 10; // Use Pin 10 as our Input
int soundDetectedVal = HIGH; // record Measurement
boolean bAlarm = false;
unsigned long lastSoundDetectTime;
int soundAlarmTime = 500;
void setup ()
{
Serial.begin(9600);
pinMode (soundDetectedPin, INPUT) ;
}
$
Top Comments