Bluetooth
- Bluetooth is a standardized protocol for sending and receiving data via a 2.4GHz wireless link.
- It’s a secure protocol, and it’s perfect for short-range, low-power, low-cost, wireless transmissions between electronic devices.
- These days it feels like everything is wireless, and Bluetooth is a big part of that wireless revolution.
- You’ll find Bluetooth embedded into a great variety of consumer products, like headsets, video game controllers.
- In our world of embedded electronics hackery, Bluetooth serves as an excellent protocol for wirelessly transmitting relatively small amounts of data over a short range (<100m).
- It’s perfectly suited as a wireless replacement for serial communication interfaces.
HOW BLUETOOTH WORKS:-
The Bluetooth protocol operates at 2.4GHz in the same unlicensed ISM frequency band where RF protocols like ZigBee and WiFi also exist.
Masters, Slaves, and Piconets
- Bluetooth networks (commonly referred to as piconets) use a master/slave model to control when and where devices can send data.
- In this model, a single master device can be connected to up to seven different slave devices. Any slave device in the piconet can only be connected to a single master.
- The master coordinates communication throughout the piconet. It can send data to any of its slaves and request data from them as well.
- Slaves are only allowed to transmit to and receive from their master. They can’t talk to other slaves in the piconet.
- Bluetooth Addresses and Names
- Every single Bluetooth device has a unique 48-bit address, commonly abbreviated BD_ADDR.
- This will usually be presented in the form of a 12-digit hexadecimal value.
- The most-significant half (24 bits) of the address is an organization unique identifier (OUI), which identifies the manufacturer. The lower 24-bits are the more unique part of the address.
Pin Configuration – AT Command
HC-05 GND — Arduino GND Pin
HC-05 VCC (5V) — Arduino 5V
HC-05 TX — Arduino Pin 10 (soft RX)
HC-05 RX — Arduino Pin11 (soft TX)
HC-05 Key (PIN 34) — Arduino Pin 9
Bluetooth – AT Mode { Steps to change data mode to AT mode}
- Unplug power from HC-05
- Upload sketch
- Hold in HC-05 button
- Reconnect power to HC-05 (wait until LED blinks slowly)
- Press Arduino reset button
- Open Serial Monitor
- Make sure “Both NL & CR” is selected
- Type AT commands
AT Commands
- AT – Check the Connection
- AT+NAME – Default name
- AT+ADDR – Default Address
- AT+VERSION – Version
- AT+UART – Baudrate
- AT+ROLE – Role of the BT module (1=Master, 0=Slave)
- AT+PSWD –Default password
- AT+CMODE – Connection Mode { 0- connect to specific addr, 1- connect any addr}
- AT+ORGL – Restore factory settings
- AT+RESET - Reset and exit AT mode
Slave Configuration
Master Configuration
Program to control the LED in the Master device by client device through push button through Bluetooth communication:-
Client Code:-
#include <SoftwareSerial.h>
SoftwareSerial BT(2,3);
#define button 8
int state = 20;
int buttonState = 0;
void setup() {
pinMode(button, INPUT);
BT.begin(38400);
Serial.begin(9600);
Serial.println("Slave Bluetooth");
}
void loop() {
// Reading the button
buttonState = digitalRead(button);
if (buttonState == HIGH) {
Serial.println("Sending...");
BT.write('1');// Sends '1' to the master to turn on LED
delay(200);
}
else {
BT.write('0');
delay(200);
}
}
Server Code:-
#include<SoftwareSerial.h>
SoftwareSerial BTM(2,3);
#define ledPin 10
int state = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
BTM.begin(38400);
Serial.begin(9600);
Serial.println("Receiving Master");
// Default communication rate of the Bluetooth module
}
void loop() {
if(BTM.available() > 0){
state = BTM.read();
Serial.println("State of Slave");
Serial.println(state);
// Reads the data from the serial port
}
// Controlling the LED
if (state == '1') {
digitalWrite(ledPin, HIGH); // LED ON
delay(500);
}
else if (state == '0') {
digitalWrite(ledPin, LOW); // LED ON
}
delay(10);
}
0 Comments
If You Have Any Doubt, Please Let Me Know