Connecting Bluetooth Module HC-05/HC-06 to Arduino

How to properly connect the HC-05 module to the Arduino board, make control from a smartphone and receive data from sensors.

Connecting Bluetooth Module HC-05/HC-06 to Arduino
Connecting Bluetooth Module HC-05/HC-06 to Arduino

Very often in your projects there is a need for remote control or data transfer from your phone gadgets. One of the most popular and common methods of data exchange via Bluetooth.Today we will look at simple examples of how you can connect a Bluetooth module to Arduino and set up remote control from your phone.

Bluetooth connection diagram for Arduino:

Arduino Bluetooth
Pin 1 (TX) RXD
Pin 0 (RX) TXD
GND GND
5V VCC

Be careful, you need to connect TX -> RXD, RX -> TXD

Now you need to write a test program code:

When uploading the sketch, it is necessary that the Bluetooth module is disconnected from the arduino microcontroller. Otherwise, the sketch will not be written, because communication with the Bluetooth module occurs on the same RX and TX port as USB.

hc-05 bluetooth module arduino

int val;
int LED = 13;

void setup()

{
 Serial.begin(9600);
 pinMode(LED, OUTPUT);
 digitalWrite(LED, HIGH);
}
void loop()
{
 if (Serial.available())
 {
 val = Serial.read();

 // При символе "1" включаем светодиод
 if (val == '1')
 {
 digitalWrite(LED, HIGH);
 }

 // При символе "0" выключаем светодиод
 if ( val == '0')
 {
 digitalWrite(LED, LOW);
 }

 }

Once the sketch is written and the Bluetooth module is connected to the Arduino, you can move on to the next step.

Bluetooth connection to a smartphone

  • Turn on Bluetooth on your phone and look for new devices
  • We find in the list of disorders "HC-06" and connect to it.
  • The phone will ask for a pin code. you must enter "1234" or "0000"
  • Hooray. The device is connected.

Now you need to download the bluetooth terminal on your phone. We will look at the example of the Android platform. There are plenty of such terminals in the play store.

You can install different bluetooth terminals, as a rule they differ only in different designs, the functionality does not change from this. You can also find a terminal for ios products.

After we have installed the terminal, we launch it, select our bluetooth module HC-06 and connect to it.

It's time to try the project in action. We write the number "0" in the terminal and send. The L LED next to pin 13 on the arduino board should turn off. Now we will send the number “1” through the terminal and the LED L should light up.

An example of using data from a DHT11 temperature sensor

dht11 and bluetooth module arduino

Let's upload a sketch to the Arduino board (see below) - receiving humidity and temperature data from the DHT11 sensor and outputting data to the serial port (hardware) through the HC05 module on the Android device.

// подключение библиотеки DHT
#include "DHT.h"
// константы
#define DHTPIN 8           // пин подключения контакта DATA
#define DHTTYPE DHT11      // датчик DHT 11
#define INTERVAL_GET_DATA 2000  // интервала измерений, мс
// создание экземпляра объекта DHT
DHT dht(DHTPIN,DHTTYPE);
// переменная для интервала измерений
unsigned long millis_int1=0;
int pos=0;
//  подключение библиотеки SoftwareSerial.h
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // указываем пины rx и tx соответственно

void setup() {
   Serial.begin(9600);  // запуск последовательного порта
   mySerial.begin(9600);
   dht.begin();         // запуск DHT
   Serial.println("start prg");
}

void loop() {
   if(millis()-millis_int1 >= INTERVAL_GET_DATA) {
      pos=1-pos;
      if(pos==0)  {
        // получение данных влажности c DHT11
        int humidity = dht.readHumidity();
        // вывод в монитор последовательного порта
        Serial.print("humidity=");Serial.println(humidity);
        mySerial.print("H=");mySerial.println(humidity);
      }
      else  {
        // получение данных влажности c DHT11
        int temp = dht.readTemperature();
        // вывод в монитор последовательного порта
        Serial.print("temperature=");Serial.println(temp);
        mySerial.print("T=");mySerial.println(temp);
      }
      // старт интервала отсчета
      millis_int1=millis();
   }
}

Let's check on the Android device to receive data sent by the Arduino via the HC05 bluetooth module. On the Android device, install the Bluetooth Terminal program. Let's establish a connection with the HC05 module in the program:



on the site are taken from open sources. The site does not contain files for download. All links to files from open sources. Owners of copyrights to the material may request removal of the post from the site.


Buy me a coffee