How to Receive Data from Bluetooth Module in Arduino
To receive data from a Bluetooth module in Arduino, use
Serial.read() or Serial.available() functions to read incoming bytes from the module connected via serial pins. Initialize serial communication with Serial.begin(baud_rate) matching the Bluetooth module's baud rate.Syntax
To receive data from a Bluetooth module connected to Arduino, you typically use the Serial object or a SoftwareSerial object if using other pins.
Serial.begin(baud_rate);- starts serial communication at the specified baud rate.Serial.available();- checks if data is available to read.Serial.read();- reads one byte of incoming data.
arduino
Serial.begin(9600); if (Serial.available() > 0) { char incomingByte = Serial.read(); // process incomingByte }
Example
This example shows how to receive characters sent from a Bluetooth module connected to Arduino's serial pins and print them to the Serial Monitor.
arduino
void setup() { Serial.begin(9600); // Start serial communication with PC Serial1.begin(9600); // Start serial communication with Bluetooth module (e.g., on Serial1) } void loop() { if (Serial1.available() > 0) { char received = Serial1.read(); Serial.print("Received: "); Serial.println(received); } }
Output
Received: H
Received: e
Received: l
Received: l
Received: o
Common Pitfalls
- Wrong baud rate: The baud rate in
Serial.begin()must match the Bluetooth module's baud rate. - Using wrong serial pins: Some Arduino boards have only one hardware serial port; use
SoftwareSerialfor other pins. - Not checking
Serial.available(): Reading without checking can cause errors or block the program. - Not connecting module correctly: Ensure TX of Bluetooth goes to RX of Arduino and vice versa.
arduino
/* Wrong way: reading without checking availability */ void loop() { char data = Serial.read(); // May read nothing or garbage Serial.println(data); } /* Right way: check before reading */ void loop() { if (Serial.available() > 0) { char data = Serial.read(); Serial.println(data); } }
Quick Reference
| Function | Purpose |
|---|---|
| Serial.begin(baud_rate) | Start serial communication at given speed |
| Serial.available() | Check if data is ready to read |
| Serial.read() | Read one byte of incoming data |
| SoftwareSerial(rxPin, txPin) | Create serial on other pins if needed |
Key Takeaways
Always match the baud rate between Arduino and Bluetooth module.
Use Serial.available() before reading data to avoid errors.
Connect Bluetooth TX to Arduino RX and Bluetooth RX to Arduino TX correctly.
Use SoftwareSerial if your Arduino has only one hardware serial port.
Print received data to Serial Monitor to verify communication.