ESP8266 - LED

This tutorial instructs you how to use ESP8266 to control an LED. In detail, we will learn:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
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

The LED Pinout

LED has two pins:

  • The Cathode(-) pin: should be connected to the negative of power supply
  • The Anode(+) pin: should be connected to the positive of power supply via a resistor
LED pinout

How It Works

The below table shows the LED state according to how the power connects to LED's pin

LED cathode(-) pin LED anode(+) pin Condition LED state
GND VCC via a resistor ON
GND PWM via a resistor ON, variable brightness
GND GND any OFF
VCC VCC any OFF
VCC GND any burned! cautious!
How LED works

As shown on the above table, by generating a PWM signal to the anode (+) of an LED, the brightness of the LED varies in accordance with the PWM value. This has been explained in detail in ESP8266 fade LED tutorial.

※ NOTE THAT:

  • For most of LED, a resistor is required to protect LED from the current. There are two options to place the resistor: between the anode(+) and VCC, or between the cathode(-) and GND. The value of the resistor depends on the specification of the LED.
  • Some kinds of LEDs have a built-in resistor. In this case, the resistor is not required.

ESP8266 - LED

When an ESP8266's pin is set up as a digital output, it can be programmed to have either the GND or VCC voltage.

Connect the ESP8266's pin to the anode(+) pin of the LED with a resistor. This will allow us to control the state of the LED through programming.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and LED

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.

How To Program

  • Set the pin of an ESP8266 to digital output mode by utilizing the pinMode() function. For instance, pin GPIO9:
pinMode(D5, OUTPUT);
  • Program the pin to GND using the digitalWrite() function to turn OFF the LED:
digitalWrite(D5, LOW);
  • Program the pin to VCC using the digitalWrite() function to turn ON the LED.
digitalWrite(D5, HIGH);

ESP8266 Code for controlling LED

#define LED_PIN D5 // The ESP8266 pin D5 connected to resistor void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }

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.
  • Click the Upload button on Arduino IDE to compile and upload the code to the ESP8266 board.
How to upload code to ESP8266 NodeMCU using Arduino IDE
  • Check out the outcome: The internal LED light switches between being ON and OFF in a regular pattern every second.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

※ NOTE THAT:

The code above makes use of delay(). This function prevents ESP8266 from performing other tasks during the delay period. If your project necessitates the completion of certain tasks, you should avoid blocking ESP8266 by utilizing the non-blocking method for Arduino.

Video Tutorial

Additional Knowledge

  • At one time, a pin can only does a single task. If it has already been assigned to another job (e.g., digital input, analog input, PWM, UART, etc.), then it should NOT be used as a digital output to control an LED. For instance, if we use the Serial.println() function, pins GPIO1 (TX) and GPIO3 (RX) should not be used for any other purpose because they are reserved for Serial.
  • This tutorial demonstrates how to use the output pin of an ESP8266 to control an LED. We can utilize this code to switch ON/OFF any apparatus, even large machines.
  • For devices/machines that require a power supply greater than 5 volts and/or high-current consumption, a relay must be used between the output pin and the device/machine. Further information can be found at ESP8266 - Relay.

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!