Tuesday, August 4, 2015

Bluetooth Module to be Used with Microcontroller

HC-05 Master/Slave Bluetooth Module is a cheap (~SGD 16) and easy to use Bluetooth module with UART interface. Interfacing and using HC-05 with Arduino Uno microcontroller to control an LED light as commanded by a hand phone via Bluetooth communication is discussed in this post. In fact, any microcontroller with UART interface can be used with it.


Figure. HC-05 Bluetooth module with Arduino Uno.


HC-05 uses 3.3V for its digital logic pins while the voltage level for Arduino Uno is 5V. A bi-directional logic level converter can be used to interface them. But, here, we just use simple voltage dividers using readily available components in our labs to interface them as shown in the following diagram.

Figure. Interfacing HC-05 Bluetooth module with Arduino Uno.


To communicate this Bluetooth module from an Android phone, we can search and use any 'Bluetooth SPP' app in the Play store. We have used 'Bluetooh spp tools pro' by Jerry.Li. After opening the app and scanning for Bluetooth devices, you can connect HC-05 using '1234' as the pairing pin. Thereafter, characters sent to RX pin of HC-05 UART will be received by the phone and the characters sent by phone will be output from TX pin of UART of HC-05. If you connect the Bluetooth module using computer instead of the phone, a serial port will appear in your computer which can be used as a normal comm port sending and receiving data to and from the Bluetooth module.

#include <SoftwareSerial.h>
#define RxD 2
#define TxD 3
char recvChar;
SoftwareSerial blueToothSerial(RxD,TxD);
void setup()
{
    Serial.begin(9600);
    
    pinMode(RxD, INPUT);
    pinMode(TxD, OUTPUT);
    blueToothSerial.begin(9600);

    pinMode(13, OUTPUT);
}
void loop()
{  
  if(blueToothSerial.available())
  {
      recvChar = blueToothSerial.read();
      Serial.print(recvChar);
      if(recvChar == '1') digitalWrite(13, HIGH);
      else if(recvChar == '0') digitalWrite(13, LOW);
  }
  if(Serial.available())
  {
      recvChar  = Serial.read();
      blueToothSerial.print(recvChar);
  }
}


This Arduino program forwards the characters sent by phone to the serial monitor and vice versa. It turns on or off the LED which is connected to the pin 13 of Arduino Uno microcontroller board when 1 or 0 is sent from the phone. The push button on HC-05 Bluetooth module needs to be pressed to send AT commands and to configure it.

Figure. Sending and receiving characters to and from the Bluetooth app using Serial monitor.


Another Bluetooth module called Bluetooth Shield is also tested. That module is designed for Arduino Uno and can be directly fixed on the Arduino Uno board. Therefore, no additional interfacing or connection is needed to make it work. But you need to make sure that the jumper settings are correct. As shown in the following figure, the jumper for HBT_TX is connected to D2 pin of the Arduino Uno board and HBT_RX is connected to D3. Therefore, D2 needs to be configured as RX pin and D3 as TX pin respectively in the demo program which is available at Bluetooth Shield Wiki page. You can define Bluetooth device name and pairing pin in that example program.

Figure. Bluetooth Shield.


No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.