sábado, 20 de diciembre de 2014

Configuration of a slave Bluetooth module

Configuring a slave Bluetooth module is similar to the configuration of a master/slave module with minor differences. This time a HC-06 module is used, which can be found at any electronics store on the web.
Besides, to configure this bluetooth module is necessary to use AT commands.

In this case, these are the AT commands used:
AT: Communication test.
AT + NAME: Rename the bluetooth device.
AT + BAUD: Change the serial communication of the bluetooth module.
AT + PIN: Change the password associated with the bluetooth module.

Needed hardware
  • Arduino Uno
  • 220Ω resistor
  • LED
  • Slave Bluetooth Module
  • Breadboard


Circuit



After implementing the circuit, copy the code below in the Arduino IDE. In the code is included the SoftwareSerial library, which allows the communication via Bluetooth. The digital pins 2 and 3 are used for the communication. The pin 2 for receiving data and pin 3 for sending.
The Arduino board sends AT commands to the bluetooth module to configure it. Additionally, the AT commands sent by the Arduino and the response received from the bluetooth module are displayed on the serial monitor.


Code

//library used for serial communication
#include<SoftwareSerial.h>
// RX is digital pin 2 (connect to TX of bluetooth module)
// TX is digital pin 3 (connect to RX of bluetooth module)
SoftwareSerial mySerial(2,3);
// Variable to control the configuration
boolean configurado = false;
void setup(){
  
pinMode(13,OUTPUT);    // Pin 13 to control the process
  
Serial.begin(9600);    // open serial communications
  mySerial.
begin(9600);  // set the data rate for the software serial port
  
digitalWrite(13, LOW); // turn the LED off
}
void loop(){
  if (configurado){
    
/* flashing to verify that is working */
    
digitalWrite(13, HIGH); // turn the LED on
    
delay(1000);            // wait a second
    
digitalWrite(13, LOW);  // turn the LED off
    
delay(1000);            // wait a second
  
}
  
else{
    
digitalWrite(13, HIGH); // start the waiting time
    
delay(1000);            // wait a second (can be modified)
    
digitalWrite(13, LOW);  // end the waiting time

    
    
Serial.println("AT");  
       
mySerial.write("AT");     // begin the communication
    delay(1000);             // with the Bluetooth module
    
while(mySerial.available()>0)     Serial.write(mySerial.read());      
       
Serial.println();

    
Serial.println("AT+NAMEHC-06");  
       
mySerial.write("AT+NAMEHC-06");  // name the device
    delay(1000);                    // wait a second    
    
while(mySerial.available()>0)     Serial.write(mySerial.read());    
       
Serial.println();        

       
Serial.println("AT+BAUD4"); 
    mySerial.
write("AT+BAUD4");   // set the data rate for the software serial port
    
delay(1000);                  // wait a second    
    
while(mySerial.available()>0)     Serial.write(mySerial.read());    
       
Serial.println();        

       
Serial.println("AT+PIN4321"); 
    mySerial.
write("AT+PIN4321"); // set the pairing PIN (password)
    
delay(1000);                  // wait a second    
    
while(mySerial.available()>0)     Serial.write(mySerial.read());    

    
/* At this point the bluetooth module should be configured */
    
digitalWrite(13, HIGH);
    configurado 
= true;     
  
}
}
///////////////////////////////////////////////////////////////////

Steps to follow:

     1. The bluetooth module must not be linked to any device.

     2. The visual way of knowing if the module is connected:
         - LED without flashing -> paired
         - Led flashing -> unpaired

     3. By using the SoftwareSerial library, define the TX and RX pins for Arduino, in this case          Rx: pin 2, Tx: pin 3. Then, connect the TX pin of the Bluetooth module to the RX pin of          Arduino and connect the RX pin of the Bluetooth module to the TX pin of the Arduino.
     
     4: Load the program.
     
     5: Start the Communication with the module using AT commands.
     
     6: Rename the module (AT+NAME). 
     
     7: Set the communication speed (AT+BAUD).
           1: 1200 baud
           2: 2400 baud
           3: 4800 baud
           4: 9600 baud (default)
           5: 19200 baud
           6: 38400 baud
           7: 57600 baud
           8: 115200 baud          
   
     8: Configure the pairing PIN (password) (AT+PIN).

That's all, if you have any questions or comments let them in the comment box below.

No hay comentarios:

Publicar un comentario