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: