ESP8266 Control LED via Bluetooth

This tutorial instructs you how to program ESP8266 to control a LED through either Bluetooth or BLE.

This tutorial gives guidance on how to use both modules.

We will use the Bluetooth Serial Monitor App on a smartphone to send commands to ESP8266.

These commands include:

ESP8266 NodeMCU LED Bluetooth

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×HC-05 Bluetooth Module
1×(Alternative) HM-10 BLE Module
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of LED and Bluetooth Module

If you are unfamiliar with LED and Bluetooth Module (including pinout, functionality, and programming), the following tutorials can help:

Wiring Diagram

  • If you desire to manipulate an LED through Bluetooth, the HC-05 Bluetooth module should be used with the wiring diagram provided below.
The wiring diagram between ESP8266 NodeMCU and LED Bluetooth

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • If you desire to manipulate LED through BLE, the HM-10 BLE module should be used with the wiring diagram provided below.
The wiring diagram between ESP8266 NodeMCU and LED BLE

This image is created using Fritzing. Click to enlarge image

ESP8266 Code - controls LED via Bluetooth/BLE

The code functions for both the HC-10 Bluetooth module and the HM-10 BLE module. It is applicable to both.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-control-led-via-bluetooth */ #include <SoftwareSerial.h> #define SOFT_RX D7 // The ESP8266 pin connected to the TX of the bluetooth module #define SOFT_TX D6 // The ESP8266 pin connected to the RX of the bluetooth module #define LED_PIN 8 SoftwareSerial bluetooth(SOFT_RX, SOFT_TX); void setup() { Serial.begin(9600); bluetooth.begin(9600); pinMode(LED_PIN, OUTPUT); // Configure the ESP8266 pin as a digital output pin } void loop() { if (bluetooth.available()) { // if there is data comming String command = bluetooth.readStringUntil('\n'); // read string until meet newline character if (command == "LED OFF") { digitalWrite(LED_PIN, LOW); // turn off LED bluetooth.println("LED is turned OFF"); // reports action to smartphone app } else if (command == "LED ON") { digitalWrite(LED_PIN, HIGH); // turn on LED bluetooth.println("LED is turned ON"); // reports action to smartphone app } } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Download and install the Bluetooth Serial Monitor App on your smartphone.
  • Copy the code and open it with the Arduino IDE. Then, click the Upload button to upload the code to the ESP8266. If you experience any difficulty uploading the code, try disconnecting the TX and RX pins from the Bluetooth module, uploading the code, and then reconnecting the RX/TX pins again.
  • Once the code is uploaded, open the Bluetooth Serial Monitor App on your smartphone and select either Classic Bluetooth or BLE, depending on the module you used.
Bluetooth Serial Monitor App
  • Connect the Bluetooth App to the HC-05 Bluetooth module or HM-10 BLE module.
Bluetooth Serial Monitor pairing
  • Enter either “LED ON or “LED OFF and press the Send button
Bluetooth Serial Monitor App
  • Check the LED's state on the ESP8266 board. It will be either ON or OFF.
  • We can also view the LED's state on the Bluetooth App.
  • Check out the outcome on the Android App.
Bluetooth Serial Monitor App

You may ponder how ESP8266 can comprehend an entire command? For example, when we transmit the “OFF” directive, how can ESP8266 recognize whether the command is “O”, “OF” or “OFF”?

When sending a command, The bluetooth App adds a newline character ('\n') by choosing “newline” option on the App. ESP8266 reads data until it encounters the newline character. The newline character serves as a command separator.

If you find the Bluetooth Serial Monitor app helpful, please rate it 5 stars on Play Store. Your appreciation is much appreciated!

Video Tutorial

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!