Receiving commands over Bluetooth lets your Arduino talk wirelessly to other devices. This helps control your project without wires.
Receiving commands over Bluetooth in Arduino
void setup() {
Serial.begin(9600); // Start serial communication
bluetoothSerial.begin(9600); // Start Bluetooth serial
}
void loop() {
if (bluetoothSerial.available()) {
char command = bluetoothSerial.read();
// Use the command
}
}Use bluetoothSerial.available() to check if data is ready to read.
Use bluetoothSerial.read() to get one byte (character) from Bluetooth.
if (bluetoothSerial.available()) { char c = bluetoothSerial.read(); Serial.print("Received: "); Serial.println(c); }
String command = ""; while (bluetoothSerial.available()) { command += (char)bluetoothSerial.read(); } Serial.println(command);
This program listens for commands over Bluetooth. When it gets '1', it turns the built-in LED on. When it gets '0', it turns the LED off. It also prints the commands and actions to the Serial Monitor.
#include <SoftwareSerial.h> SoftwareSerial bluetoothSerial(10, 11); // RX, TX pins void setup() { Serial.begin(9600); bluetoothSerial.begin(9600); Serial.println("Bluetooth Command Receiver Ready"); pinMode(LED_BUILTIN, OUTPUT); } void loop() { if (bluetoothSerial.available()) { char command = bluetoothSerial.read(); Serial.print("Command received: "); Serial.println(command); // Example: turn on LED if command is '1' if (command == '1') { digitalWrite(LED_BUILTIN, HIGH); Serial.println("LED ON"); } else if (command == '0') { digitalWrite(LED_BUILTIN, LOW); Serial.println("LED OFF"); } } }
Make sure your Bluetooth module is connected to the correct Arduino pins (here pins 10 and 11).
Set the same baud rate on both Arduino and Bluetooth device (usually 9600).
Use SoftwareSerial library if your Arduino has only one hardware serial port.
Bluetooth commands let you control Arduino wirelessly.
Use available() and read() to get data from Bluetooth.
Test commands by printing them to Serial Monitor and acting on them.