Send message to Arduino using Bluetooth Module HC-05 | applied electronics engineering

Latest

Send message to Arduino using Bluetooth Module HC-05

By Applied Electronics - Sunday, April 9, 2017 No Comments
In this tutorial you will learn how to send message to Arduino using Bluetooth Module HC-05. This tutorial is taken from Arduino Bluetooth Module HC-05 Tutorial.

The HC-05 is a bluetooth module which supports master and slave functionality. It has six pins: State, Rx, Tx, Gnd, Vcc and En. The picture below shows this module.

Send message to Arduino using Bluetooth Module HC-05





Using this module we can transmit message from PC to Arduino. The message can be like to turn on/off a LED that is connected to Arduino. To do this, the HC 05 bluetooth module is connected to Arduino. We don't need to connect all the pins. Only the Rx, Tx, Gnd and Vcc pins are required. Below shows the diagram on how to connect them with the Arduino Uno.

Send message to Arduino using Bluetooth Module HC-05

The +5V supply from Arduino Uno is connected to the Vcc of the bluetooth module. The Tx of Arduino Uno is connected to Rx pin of the module via the 1KOhm and 2.2KOhm voltage divider. This voltage divider limits the voltage at the Rx pin of the module because it cannot  handle more the 3.3V. Then Rx of the Arduino is connected to the Tx of the module. Finally a LED is connected to the pin 7 of the Arduino and grounded using 220Ohm resistor.

Once the circuit is build we need a program to upload to the Arduino. This program instructs the arduino microcontroller to monitor the serial port(Rx of Arduino). If there is any data then the microcontroller should turn on the LED at pin 7. Also it should send message like "LED ON and "LED OFF" via its Tx pin to the Bluetooth module HC-05. This message is then sent by Bluetooth module to PC via bluetooth wireless connection.

Below is the program code that does this.

 char Val; //value sent by PC to HC-05 over bluetooth
char lastVal; //stores last state of device (on/off)
int led = 7;

void setup() {
Serial.begin(9600); // begin communication on baud 9600
pinMode(led, OUTPUT); // set the led pin to output
}

void loop() {
  if(Serial.available()) // monitors for available data
    {                     //if data is there
      Val=Serial.read(); // read it
    }
  if (Val=='1')
    {//if value from bluetooth serial is '1'
      digitalWrite(led,HIGH); // turn on LED
      if (lastVal!='1')
        Serial.println(F("LED ON")); //print LED is on
        lastVal = Val;
    }
  else if (Val=='0')
      {//if value from bluetooth serial is '0'
      digitalWrite(led,LOW); //turn off LED
        if (lastVal!='0')
          Serial.println(F("LED OFF")); //print LED is off
          lastVal = Val;
      }
delay(1000);
}

You should compile/upload this sketch into Arduino. Remember to disconnect the Tx and Rx wires between the Arduino and Bluetooth module when you are uploading the code. Other you will get error. After the program has been successfully uploaded to Arduino reconnect the Tx and Rx wires.

The final step is send message(1 or 0) to the bluetooth. When you send 1 then the LED should turn on and there should also be message displayed LED ON on the com port terminal. Likewise when you send 0, the LED should turn off and message LED OFF should be displayed on the port terminal.

The port terminal used in this tutorial is the Tera Term. This is open source port terminal window for windows OS. So it is free for download. Download this Tera Term and install it. After you have installed it open it. Then choose Serial in the Tera Term window. Choose the port that bluetooth module is using. See below.


If you don't  know the com port the bluetooth module 05 is using then go to device manager(right click on computer and select properties then device manager on the left side pane). In the device manager you should see com port listed under Ports(COM & LPT).

Once you have selected the serial option and the port click OK button.

Now you are ready to send 1 or 0 to the bluetooth module. Type 1 in your keyboard. At this point you will see that the LED turns ON and also a message is displayed in the Tera Term window. Note that you won't see 1 in the Tera Term window. The 1 that you typed will be sent from the bluetooth com port on your computer to the Bluetooth module. Similarly, type 0 and you should see that the LED turns off and there is message LED OFF in the tera term window.

So in this way you can send message to Arduino using Bluetooth module HC-05.

See other Arduino tutorials:
For books see:

No Comment to " Send message to Arduino using Bluetooth Module HC-05 "