Thursday, February 27, 2014

Communicating two Arduino Uno Boards Using Bluetooth RN-42

In the previous post i have shown how to configure your Bluetooth module and now in this post i will give you some basic ideas how to communicate with two Arduino devices wirelessly (using Bluetooth RN-42)


Steps 1: First take two Arduino Uno boards and configure these boards as shown below.

** Remember -- connect the Tx pin of your Arduino uno with Rx pin in Bluetooth and Rx (Arduino Uno) pin to Tx pin (Bluetooth RN-42)



                                                             
Step 2:- Upload the code to the master device.




1:  //Master  
2:  #include <TextFinder.h>           // Download the header file ** (link)  
3:  String Remote ="00066649----";    // Address of slave device  
4:  void setup()  
5:  {  
6:   Serial.begin(9600);                //Baudrate (Default Baudrate of RN-42 is 115200)  
7:   Serial.print("$$$");                // command mode  
8:    delay(100);  
9:   Serial.println("c,"+ Remote +"\r");  // connect to the device  
10:   delay(100);  
11:   Serial.println("SM,1");               // MASTER  
12:   delay(100);  
13:   Serial.println("---,"+ Remote +"\r");    // exit from command mode and enter data mode  
14:   delay(100);  
15:  }  
16:  void loop()  
17:  {  
18:   Serial.println("L");                         // Send ‘L’ in data mode  
19:   delay(100);  
20:   Serial.println("H");                         // Send ‘H’ in data mode  
21:   delay(100);  
22:  }  

** paste this file in Libraries of ArduinoIDE folder  https://www.dropbox.com/s/ywofibuw8li7m9p/TextFinder.rar

Step 3 :- Now Upload the given below code in Slave device




1:  //Slave  
2:  #include <TextFinder.h>  
3:  String Remote ="0006664953B0";   // Address of master bluetooth  
4:  void setup()  
5:  {  
6:   Serial.begin(9600);                // baudrate  
7:     Serial.print("$$$");               // command mode  
8:    delay(100);  
9:   Serial.println("c,"+ Remote +"\r");   // connecting to the slave  
10:   delay(100);  
11:   Serial.println("SM,0"); // slave mode  
12:   delay(100);  
13:   Serial.println("---,"+ Remote +"\r");   // exit from command mode  
14:   delay(100);  
15:  }  
16:  void loop()  
17:  {  
18:   if(Serial.available() > 0)               // Check if data is available  
19:   {  
20:    char ch = Serial.read();              // read the incoming data  
21:    if(ch == 'H')   
22:    {  
23:     digitalWrite(13,HIGH);  
24:     delay(100);  
25:    }  
26:    else if(ch == 'L')  
27:    {  
28:     digitalWrite(13,LOW);  
29:     delay(100);  
30:    }  
31:   }  
32:  }  


** Remember - never connect your Bluetooth while uploading the code.

Now when you turn on your devices you will see that the Master will initiate connection and when both the devices are connected the Green led will be turned on in your Bluetooth Device.

Here in this code the Master device will send "H" and  "L" and when the slave device receives "H" from the serial port it will turn on the led of Arduino and when it receives "L" from the serial port it will turn Off the led of Arduino which is connected to 13th pin.

This was a basic application. You can connect a keypad with the master Device and can build a robot where the master device will drive the slave wirelessly.
you can even display data send by the master device by connecting a LCD with the slave device. There are many more application that you can make


** In next post i will show how to connect Arduino Uno or any other controller with ANDROID DEVICE using Bluetooth.

No comments:

Post a Comment