2022.03.05(pm): How to communicate interactively with a Bluetooth module

The two-way communication with the Bluetooth module, which has been delayed and delayed, has finally been completed today. I wanted the vibration motor to operate in SLAVE when motion was detected on the MASTER side using Arduino and HC-05 module.

If you look at the video, the two Arduinos and the two communication modules are separated from each other, and the Bluetooth module is paired with each other. When motion is detected by the PIR sensor connected to the master (right), the vibration motor operates in the slave (left) in real time.

Then, let’s look at the materials, the circuit diagram, and the code in turn.

Materials

NoNameQuantity
1Arduino UNO Board2
2HC-05 Bluetooth module2
3PIR Sensor module (Motion Detection)1
4Vibration Motor1
51 kΩ2
62 kΩ2
79V battery2
8Jumper Cable, Battery holder

One thing to note here is the selection of the Bluetooth module, and the types of Bluetooth modules we are familiar with are HC-05 and HC-06. You shouldn’t use the HC-06 because you think the HC-06 has a bigger number, so it might be better. (If you want to use two-way communication.) HC-06 is sold separately in slave and master mode, or you have to look closely at the version provided to change the mode. However, since HC-05 can change ROLE by itself, it is recommended for mental health to use HC-05 for two-way communication.

Circuit

In HC-05, the RX pin is at 3.3V level, so you should not just connect 5V, which is the basic voltage of the Arduino digital pin, and you need to lower the voltage.

How to connect HC05-ARDUINO-UNO
https://www.trustfm.net/software/mobile/GRBLDroidBT.php?page=Documentation

Bluetooth module initial setting (AT commands)

First, upload the code below and then supply power while pressing and holding the button on the HC-05 module before supplying power to enter the AT command mode. In basic mode, the red light blinks once every second, in AT command mode, it blinks once every 2 seconds.

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(9, 8);  

void setup() {
  Serial.begin(38400);
  BTSerial.begin(38400);
}

void loop() {
  if (BTSerial.available())
    Serial.write(BTSerial.read());
  if (Serial.available())
    BTSerial.write(Serial.read());
}

After that, be sure to select both NL & CR in the serial monitor and set the baud rate to 38400. When you type AT and then press Enter, if OK is displayed, you have entered the AT command state normally.

HC 05 Bluetooth does not show in serial monitor - Programming Questions -  Arduino Forum

Below are the types of AT commands. Here, each name is changed, and the password is changed if necessary, but pairing is possible only when the password is the same for Master and Slave. Next, if ROLE is 0, it is Slave, and if it is 1, it is Master.

HC-05 AT commands

After setting is complete, upload the code by dividing Master and Slave as shown below, and connect the circuit properly.

Arduino Code

Master

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(9, 8);

int pir_sig = 7;
int val = 0;

void setup() {
    pinMode(pir_sig, INPUT);
    Serial.begin(38400);
    BTSerial.begin(38400);
    //pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    val = digitalRead(pir_sig);
    BTSerial.write(val);
    Serial.println(val);
}

Slave

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(9, 8);
int BluetoothData =0;
int vibPin = 5;

void setup() {
    Serial.begin(38400);
    BTSerial.begin(38400);
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(vibPin, OUTPUT);
}

void loop() {
        BluetoothData = BTSerial.read();
        Serial.println(BluetoothData);
        if(BluetoothData > 0){
          digitalWrite(LED_BUILTIN, HIGH);
          digitalWrite(vibPin, HIGH);
        }
        else{
          digitalWrite(LED_BUILTIN, LOW);
          digitalWrite(vibPin, LOW);
        }
}

When two Bluetooth modules are connected to each other in a two-way manner, it changes to blinking twice rapidly. However, before sending and receiving data, it is recommended to check the format or value of the data exchanged using serial communication when sending and receiving data.

Thanks for reading.

One Reply to “2022.03.05(pm): How to communicate interactively with a Bluetooth module”

Leave a Reply

Your email address will not be published. Required fields are marked *