Example Code for Arduino-Bluetooth Wireless Control LED
Last revision 2025/12/26
This article provides a detailed guide on setting up Arduino-based wireless control of an LED using Bluetooth, including hardware and software preparation, Bluetooth device configuration, and example code for implementation.
Hardware Preparation
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
Other Preparation Work
Set two bluetooth devices as a master and a slave (bluetooth communication should be established between a master and a slave machine). Here we set BLE-Link as the master, while RoMeo BLE as the slave. Specifically, we first connect the BLE-Link to the PC via a USB cable and the BLE-Link would be identified as a serial port equipment. Then open the serial debugging assistant (take the V1.8 version of BLE-Link firmware to for example), send the "+++" to BLE-Link, proving that you have entered the AT command Mode when receiving "Enter into the AT command Mode". Send the BLE-Link "AT + SETTING = DEFCENTRAL" (remember to select a new line to send), meaning that the BLE-Link has been set as the master when receiving "OK". Accordingly we set the RoMeo BLE as the slave by connecting it to the PC, open the serial debugging assistant and send "+++" and "AT + SETTING = DEFPERIPHERAL".
Sample Code
int led = 13;
char rcv_buf[10]; // Receive command arrays.
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(115200);
}
void loop()
{
int data_len=0;
while(1)
{
while(Serial.available())
{
rcv_buf[(data_len++)%10] =Serial.read();
}
if(rcv_buf[data_len-2]== '\r' && rcv_buf[data_len-1]=='\n') // Stop reading the serial once getting the ending command.
break;
}
if ((data_len==4)&&(!strncmp(rcv_buf,"ON",2))) // Open the light L when the command is "ON".
{
digitalWrite(led, HIGH); // Set D13 pin as high and openg the light L
Serial.println("LIHGT ON");
}else if((data_len==5)&&(!strncmp(rcv_buf,"OFF",3))) // Close the light L when the command is "OFF".
{
digitalWrite(led, LOW); // Set D13 pin as low and the light L
Serial.println("LIHGT OFF");
}
}
Result
Connect the BLE-Link to the PC, and meanwhile open the RoMeo BLE. Wait a few seconds the two bluetooth devices will automatically connect. The LINK light will be on showing connection, indicating that the two devices are ready for wireless communication. Then open the serial debugging assistant and send "ON" under the mode of New line. This command is sent from the serial port, passing through the BLE-Link to RoMeo BLE. RoMeo BLE after receiving the command will parse the Arduino program, and then open the light L. When sending OFF under the mode of New line, RoMeo BLE turns OFF the light L.
Was this article helpful?
