ESP8266: Blinking a LED

The objective of this post is to explain how to blink a LED using the ESP8266 and the ESP libraries for the Arduino IDE.


Introduction

Driving a LED with a microcontroller is typically one of the first things people do to test it. Although the electric scheme for driving a LED is very simple, there are some considerations we need to take before doing it.

This post will explain how to blink a LED using a ESP8266 and the ESP8266 libraries for the Arduino IDE. You can check how to install them here.


The hardware

First of all, we need to define the circuit. To drive a LED, we basically need a power source and a current limiting resistor.

The resistor is needed because the relation between the current and the voltage in a LED is not linear. If we exceed a certain voltage, the current in the LED will quickly increase with the increase of voltage, and it will simply burn.

So, we need to use the resistor in series with the LED to guarantee that it works inside a safe interval of voltage, so the current flowing in the circuit is the recommended for the operation of the LED.

You can check a typical circuit with a series resistor to drive a LED in figure 1.

series-led-schematic

Figure 1 – Electric schematic to drive a LED.

The relation between the voltage of the LED and the current can be obtained from the LED’s datasheet. It will vary depending on the type of LED.

So, to get the needed value for the resistor, we will need to use Ohm’s law, as we will see.

As we stated, the voltage that will drop on the LED is known and also the voltage of our power source, VCC (it can be a battery, a pin of a microcontroller, etc…).

Using KVL law, we know that the voltage that drops across the resistor will be equal to VCC minus the voltage drop on the LED:

voltage-drop-in-resistor

Using Ohm’s law, we know that the voltage in the resistor is given by:

ohm-law-for-resistor

In this equation, I is the current flowing in the circuit and corresponds to the value of the current of the LED, that we get from its datasheet.

If we substitute Vr by the value calculated before and solve the equation in order to R, we get:

resistor-calculator-for-led

Although we can manually solve this equation (we know all the variables to get R), there are a lot of online calculators that do this for us, such as the one here.

When controlling a LED from a microcontroller, we will be providing the voltage from a GPIO pin, which in fact will be our VCC, as seen in figure 2. Keep in mind that the GND symbol represented in the schematic needs to be the GND of the ESP8266.

esp8266-driving-a-led

Figure 2 – Driving a LED with the ESP8266.

So, we need to know what is the voltage of the GPIO that will be driving the LED. Since the ESP8266 operates at 3.3 V [1], the GPIO voltage will be 3.3 V. In our equation, VCC is 3.3 V.

We also need to consider the maximum output current of each GPIO. In the case of the ESP8266, the maximum current per pin is 12 mA [1]. Nevertheless, we will not want to be driving the LED at the maximum current of the pin. So, we need to guarantee that the current of the LED, at the voltage we will be driving it (Vled), needs to be lower than 12 mA.

This is very important when choosing the LED we are using, so choose it carefully so it can operate at a current lower than 12 mA. Otherwise, the ESP8266 will get damaged.

To test if the calculations are correct, my advice is to power the LED from a 3.3 V power source, using a resistor with the value calculated, and measure the current draw with a multimeter, before testing the circuit with the ESP8266.

If you don’t have the specs of the LED and will try a reasonable resistor value, do this test with a external power source and not with the ESP8266. Also, always start from higher resistors to lower to avoid damaging the LED.


The Code

The code is very simple and is the same that we have in the example blink program that comes with the Arduino IDE.

First, we will declare the GPIO that we are going to use as output. This is done in the setup function, as shown bellow. We declare the pin direction with the pinMode function.

void setup() {

pinMode(13, OUTPUT); //Declare GPIO13 as output

}

If you are using a NodeMCU board, take into consideration that the GPIOs of the ESP don’t map to the numbers on the board. The mapping can be seen here.

Now, to turn the LED on, we simple put a HIGH value in the GPIO. This can be done with Arduino’s digitalWrite function.

digitalWrite(13, HIGH);

To turn it off, we write a LOW value, with the same function.

digitalWrite(13, LOW);

So, to make it blink, we simply turn it on, delay the execution for a certain amount of milliseconds, turn it off and delay the execution again. The delay is done with the delay function.

This pattern is specified in the loop function, as shown bellow.

void loop() {

digitalWrite(13, HIGH);      // turn the LED on 
delay(1000);                         // Wait 1 second 
digitalWrite(13, LOW);       // turn the LED off 
delay(1000);                        // wait 1 second

}

You can check the final result bellow.


References

[1] http://download.arduino.org/products/UNOWIFI/0A-ESP8266-Datasheet-EN-v4.3.pdf

 

Technical details

  • ESP8266 libraries: v2.3.0
Advertisements
This entry was posted in ESP8266 and tagged , , , , . Bookmark the permalink.

5 Responses to ESP8266: Blinking a LED

  1. Pingback: ESP8266 Webserver: Controlling a LED through WiFi | techtutorialsx

  2. Hi antepher! Great blog! Good luck with your ESP8266! Enjoy Python coding! Epic Chas Gamer 😎

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s