0
0
Arduinoprogramming~5 mins

Receiving commands over Bluetooth in Arduino

Choose your learning style9 modes available
Introduction

Receiving commands over Bluetooth lets your Arduino talk wirelessly to other devices. This helps control your project without wires.

You want to control a robot using your phone.
You need to change settings on a device without plugging it in.
You want to send simple commands from a computer to Arduino wirelessly.
You are building a remote control for lights or motors.
You want to receive sensor data from another Bluetooth device.
Syntax
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.

Examples
This reads one character from Bluetooth and prints it to the normal Serial Monitor.
Arduino
if (bluetoothSerial.available()) {
  char c = bluetoothSerial.read();
  Serial.print("Received: ");
  Serial.println(c);
}
This reads all available characters from Bluetooth and stores them in a string.
Arduino
String command = "";
while (bluetoothSerial.available()) {
  command += (char)bluetoothSerial.read();
}
Serial.println(command);
Sample Program

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.

Arduino
#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");
    }
  }
}
OutputSuccess
Important Notes

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.

Summary

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.