Bluetooth AT Commands (How to Configure and pair two Bluetooth as Master and Slave Mode)
Overview
In this tutorial, we will learn how to configure an HC-05 Bluetooth module as Master and Slave. We will also learn how to pair or communicate between two Bluetooth module.
You can watch the following video below:-
You can watch the following video below:-
Components Required
The required components list for this project given below:-- Arduino Uno
- Arduino Nano
- Two HC-05 Bluetooth Module
- 2.2K ohm Resistor
- 1K ohm Resistor
- 2 Push Button
- 2 LED
- Breadboard
- Some Jumper Wire
Pinout
Circuit Schematic
Source Code
The slave source code is given below:-
//*******************Slave*****************
#define buttonPin 8
#define ledPin 7
int state = 0;
int buttonPinState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
state = Serial.read(); // Reads the data from the serial port
}
// data receiving part
if (state == '1')
{
digitalWrite(ledPin, HIGH); // led turn on
}
else if (state == '0')
{
digitalWrite(ledPin, LOW); // led turn of
}
// Data sending Part
buttonPinState = digitalRead(buttonPin);
if (buttonPinState == HIGH) {
Serial.write('1'); // Sends '1' to the master to turn on LED
}
else {
Serial.write('0');
}
}
The master source code is given below:-
//***************Master*******************
#define buttonPin 4
#define ledPin 5
int state = 0;
int buttonPinState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
state = Serial.read(); // Reads the data from the serial port
}
if (state == '1')
{
digitalWrite(ledPin, HIGH); // led turn on
}
else if (state == '0')
{
digitalWrite(ledPin, LOW); // led turn of
}
// Data sending Part
buttonPinState = digitalRead(buttonPin);
if (buttonPinState == HIGH) {
Serial.write('1'); // Sends '1' to the slave to turn on LED
}
else {
Serial.write('0');
}
}
No comments