Posted on أترك تعليق

HC-05 Connection to Arduino

HC-05 Bluetooth Module Main

The HC-05 Bluetooth module is an excellent interface for communicating with your mobile. It has a great data transfer rate and very easy to implement.

In order to try this tutorial out you will need the following:

  • Any Arduino board you like and if you don’t already have one check these out
  • Grab a breadboard similar to any one of these
  • Get yourself one of these HC-05 Bluetooth Modules, if you don’t already have one then grab one from here
  • 2 Resistors, 2K Ohm and 4.6K Ohm
  • Finally grab some Dupont jumper wires or use normal wires if you are comfortable with that

It is worth noting that the HC-05 power in (Vcc) uses 5V, while the transmit and receive (TXD and RXD) logic signal uses 3.3V. Accordingly sending signals from the HC-05 module to Arduino is ok as the Arduino   I/O pins can safely receive up to 5V but the issue is when Arduino tries to send the data to the HC-05 with signal level 5V, in this case it is required to use a voltage divider as we will see during the tutorial.

HC-05 Bluetooth module connection

Now let’s get down to business. First and in order to avoid a great deal of frustration and time wast, if your breadboard has a power rail attached test it for connectivity.

1- Get 2 dupont jumper cables, connect 1 at the rail start and one at the rail end. Now grab your voltmeter and set it on Ohm or connectivity measuring mode then connect the tips to the end of the jumper cables. Make sure that the rail is consistent.

Breadboard power rail connectivity

2- If the power rail connectivity is not consistent then consider using jumper wires to fix that.

Breadboard power rail jumper

3- Connect the Arduino Nano and connect it 5V out and GND to power rails as depicted in the image below.

Arduino Nano connected

4- Now let’s connect the HC-05 and its power sockets. Vcc to the Arduino 5V and GND to Arduino GND.

HC-05 Connection step1

5- Next we will connect the HC-05 TXD (this is the pin where all data received from the mobile will be transmitted via it to Arduino) to Arduino pin D3.

Step2 - HC-05 TXD connection

6- Now we will have to connect the HC-05 RXD. First we need to establish a voltage divider using the 2K and the 4.6K ohm resistors. First connect one end of the 2K resistor to Arduino D2 pin, the connect the other end to any unused line in the breadboard, then into this line connect the 4.6K resistor and the other end to the GND. See the image below.

HC-05 voltage divider for RXD connection

7- Connect a dupont jumper originating from the centre point between the 2 resistor and connect it to the HC-05 RXD.

HC-05 RXD connection

8- You are all done now with the hardware work. Let’s move to programming. Grab you USB cable and connect the Arduino to your laptop and use the code below:

#include <SoftwareSerial.h>

// Define the data transmit/receive pins in Arduino
#define TxD 2
#define RxD 3

SoftwareSerial mySerial(RxD, TxD); // RX, TX for Bluetooth

void setup() {

  mySerial.begin(9600); // For Bluetooth
  Serial.begin(9600);   // For the IDE monitor Tools -> Serial Monitor

  // Any code that you want to run once....

}

void loop() {

  // put your main code here, to run repeatedly:

  boolean isValidInput;
  do
  {
    byte c; // get the next character from the bluetooth serial port
    while ( !mySerial.available() )
      ; // LOOP...
    c = mySerial.read(); // Execute the option based on the character recieved

    Serial.print(c); // Print the character received to the IDE serial monitor

    switch ( c )
    {
      case 'a': // You've entered a

        // Do the code you need when 'a' is received.....

        mySerial.println( "You've entered an 'a'" );
        isValidInput = true;
        break;

      case 'b': // You've entered b

        // Do the code you need when 'a' is received.....

        mySerial.println( "You've entered an 'b'" );
        isValidInput = true;
        break;

      default:

        // Do the code you need when any other character is received.....

        mySerial.println( "Please enter 'a' or 'b'" );
        isValidInput = false;
        break;
    }
  } while ( isValidInput == true ); // Repeat the loop
}

Oh, forgot to tell you, download and use BT-Term (S2 Terminal for Bluetooth) downloadable from the Google PlayStore to send characters to your module 🙂

Hope this tutorial helps! 🙂

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *