sábado, 20 de diciembre de 2014

Configuration of a master/slave Bluetooth module as slave

First of all, is important to know a bit about Bluetooth technology. Bluetooth is a wireless technology standard that allows the transmission of voice and data between devices via a secure radio frequency of 2.4 GHz. This technology allows creating wireless home networks to synchronize and share information that is stored on different computers.

On the other hand, AT commands are encoded instructions used to control a modem, in this case the Bluetooth module. AT is the abbreviation of ATtention. Every command line starts with "AT" or "at". That's why modem commands are called AT commands.

In this tutorial a HC-05 module is used, this Bluetooth module can be configured either as a slave or master device and has a little difference with the other modules when sending AT commands. The module is in slave mode by default; also both the name and password are already defined.
In this case the following commands are used:

AT + ROLE = 0\r\n
This command configures the way in which the bluetooth module will work, the 1 indicates a master mode configuration and the 0 indicates slave mode. For setting the module in slave mode you have to use: "AT + ROLE = 0\r\n"

AT + PSWD = 4444\r\n
This command defines a password. The command set the password associated with the module.
Now let's implement the circuit to configure the module. For this, it is necessary the following components.


Needed hardware
  • Arduino Uno
  • 220Ω resistor
  • LED
  • Bluetooth Module (master/slave)
  • Breadboard

Circuit


Finally, in the code is included the SoftwareSerial library, which allows serial communication via any digital pin of the Arduino. On the Arduino web site you can find more information about the SoftwareSerial library. This library will serve for bluetooth communication . The digital pins 2 and 3 are used for the communication. The digital pin 2 for receiving data and the 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
   
digitalWrite(8, LOW);
   mySerial.
begin(9600);   // set the data rate for the software serial port
   digitalWrite(13, LOW);  // turn the LED off
}
void loop(){

  
if (configurado){
    
digitalWrite(8, LOW);
    
/* flashing to verify that is working */
    
digitalWrite(13, HIGH);      // turn the LED on
    
delay(1000);                 // wait a second
  }
  
else{
    
digitalWrite(13, HIGH); // start the waiting time
    
digitalWrite(8, HIGH);  // set the KEY pin to HIGH
    
delay(1000);            // (to enter in AT mode)
    digitalWrite(13, LOW);  // end the waiting time
    
    
Serial.println("AT");  
       mySerial.
write("AT\r\n");      // begin the communication 
    delay(1000);                  // with the Bluetooth module
    
while(mySerial.available()>0) {
      
Serial.write(mySerial.read()); 
    
}     
       
Serial.println();

    
Serial.println("AT+ROLE=0\r\n"); 
    mySerial.
write("AT+ROLE=0\r\n"); 
    
delay(1000);                        // wait a second  
    
while(mySerial.available()>0)     Serial.write(mySerial.read());    

       
Serial.println("AT+PSWD=4444\r\n"); 
    mySerial.
write("AT+PSWD=4444\r\n");  // set the pairing PIN (password)   
    
delay(1000);                          
    while(mySerial.available()>0)     Serial.write(mySerial.read());
    
    
digitalWrite(8,HIGH);
    
/* 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 the status of the module:
         LED without flashing or slow flash -> paired
         LED flashing quickly -> No paired

     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: Make the connections as shown in the circuit
         NOTE: When making connections ensure that the Arduino board is not connected.

     5: Load the program, then check that the bluetooth module LED flashes quickly. This                indicates that you are in AT mode at 9600 baud, if otherwise the LED flashes slowly,            indicating that the module is at 38400 baud. In the code, the module has been set at              9600 baud. Repeat the steps 5 and 6 until the LED flashes quickly.

     6: Test the communication with the module using the command: AT\r\n. You should then          see the response in the serial monitor. The response expected is "OK". If this answer            is not showed, check the connections.

      7: If the answer is confirmed, enter the AT commands according to your interest.

     8: Verify that the AT commands are received correctly by the serial monitor (must wait an          "OK" response after sending AT commands).

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

No hay comentarios:

Publicar un comentario