Arduino control LED via Bluetooth

In this tutorial, we are going to learn how program Arduino to control a LED via Bluetooth or BLE.

This tutorial provides the instruction for both modules

We will use the Bluetooth Serial Monitor App on smartphone to send commands to Arduino. The commands include:

Arduino LED Bluetooth

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×HC-05 Bluetooth Module
1×(Alternative) HM-10 BLE Module
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About LED and Bluetooth Module

If you do not know about LED and Bluetooth Module (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

  • If you want to control LED via Bluetooth, use HC-05 Bluetooth module with the below wiring diagram
Arduino LED Bluetooth Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • If you want to control LED via BLE, use HM-10 BLE module with the below wiring diagram
Arduino LED BLE Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code - controls LED via Bluetooth/BLE

The below code works for both HC-10 Bluetooth module and HM-10 BLE module

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-controls-led-via-bluetooth */ // NOTE: change the Serial to other Serial/Software Serial if you connects Bluetooth module to other pins #define LED_PIN 8 void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); // set the digital pin as output: } void loop() { if (Serial.available()) { // if there is data comming String command = Serial.readStringUntil('\n'); // read string until meet newline character if (command == "OFF") { digitalWrite(LED_PIN, LOW); // turn off LED Serial.println("LED is turned OFF"); // reports action to smartphone app } else if (command == "ON") { digitalWrite(LED_PIN, HIGH); // turn on LED Serial.println("LED is turned ON"); // reports action to smartphone app } } }

Quick Steps

  • Install Bluetooth Serial Monitor App on your smartphone
  • Copy the above code and open with Arduino IDE and upload the code to Arduino
  • Click Upload button on Arduino IDE to upload code to Arduino. If you are unable to upload code to your Arduino, try disconnecting the TX and RX pins from the Bluetooth module, uploading the code, and then reconnecting the RX/TX pins again.
  • Open Bluetooth Serial Monitor App on your smartphone
  • Select the Classic Bluetooth or BLE according to the module you used
Bluetooth Serial Monitor App
  • Pair the Bluetooth App with HC-05 Bluetooth module or HM-10 BLE module
Bluetooth Serial Monitor pairing
  • Type “ON” or “OFF” and click Send button
Bluetooth Serial Monitor App
  • See the LED's state on Arduino board. We will see LED's state is ON or OFF, respectively.
  • We also see LED's state on Bluetooth App
  • See the result on the Android App.
Bluetooth Serial Monitor App

You may wonder how Arduino can receive a complete command? For example, when we send “OFF” command, how Arduino can know the command is “O”, “OF” or “OFF”?

⇒ When sending a command, The bluetooth App appends a newline character ('\n') by selecting “newline” option on the App. Arduino will read data until it meets the newline character. The newline character acts as a command delimiter.

If the Bluetooth Serial Monitor app is useful for you, please give it a 5-star rating on Play Store. Thank you!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The Best Arduino Starter Kit

※ OUR MESSAGES