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.

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.
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
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: