Bluetooth modules like HC-05 and HC-06 let your Arduino talk wirelessly to other devices. This helps you send and receive data without wires.
Bluetooth with HC-05/HC-06 module in Arduino
SoftwareSerial bluetooth(10, 11); // RX, TX pins void setup() { Serial.begin(9600); // Start serial monitor bluetooth.begin(9600); // Start Bluetooth serial } void loop() { if (bluetooth.available()) { char data = bluetooth.read(); Serial.print(data); // Show data on serial monitor } if (Serial.available()) { char data = Serial.read(); bluetooth.print(data); // Send data to Bluetooth } }
Use SoftwareSerial to create a serial connection on other pins because Arduino Uno has only one hardware serial port.
Make sure baud rates match between Arduino and Bluetooth module (usually 9600).
SoftwareSerial bluetooth(2, 3); // RX=2, TX=3 void setup() { Serial.begin(9600); bluetooth.begin(9600); }
if (bluetooth.available()) { char c = bluetooth.read(); Serial.print(c); }
if (Serial.available()) { char c = Serial.read(); bluetooth.print(c); }
This program reads data from the Bluetooth module and prints it on the serial monitor. It also sends anything typed in the serial monitor to the Bluetooth device.
/* Simple Bluetooth communication with HC-05/HC-06 Connect HC-05 RX to Arduino pin 10 (RX), TX to pin 11 (TX) Use SoftwareSerial to communicate */ #include <SoftwareSerial.h> SoftwareSerial bluetooth(10, 11); // RX, TX void setup() { Serial.begin(9600); // Start serial monitor bluetooth.begin(9600); // Start Bluetooth serial Serial.println("Bluetooth ready"); } void loop() { if (bluetooth.available()) { char data = bluetooth.read(); Serial.print("Received: "); Serial.println(data); } if (Serial.available()) { char data = Serial.read(); bluetooth.print(data); } }
HC-05 can be master or slave; HC-06 is slave only.
Make sure to connect HC-05 TX to Arduino RX pin and RX to Arduino TX pin (cross connection).
Use a voltage divider on HC-05 RX pin if Arduino uses 5V to avoid damage (HC-05 RX is 3.3V tolerant).
HC-05/HC-06 modules let Arduino communicate wirelessly using Bluetooth serial.
Use SoftwareSerial to connect Bluetooth module on different pins.
Match baud rates and connect pins correctly for communication.