Built-in project · ESP8266 NodeMCU V1.0

NodeMCU fade

An LED breathing on an ESP8266, which has no PWM hardware at all: the core moves the pin itself, and the range is 0 to 1023. The whole build, a NodeMCU ESP8266 and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $10.15. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • PWM for fading an LED
  • Software PWM on the ESP8266
  • A 0 to 1023 duty range
  • Setting the PWM frequency
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: ESP8266 NodeMCU V1.0
  • Parts: 4

How it works

The yellow LED connects to pin D1 through a 220 ohm resistor, and its other leg goes to GND. The sketch fades the LED up and then down using PWM, which switches the pin on and off fast.

An ESP8266 has no PWM hardware, so the core does it with a timer in software. That is why the range is 0 to 1023 and not 0 to 255. The duty moves in steps of 8, with a 4 ms wait on each step.

Parts list

4 parts, plus the jumper wires. They retail for about $10.15; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω pin 1ESP8266 NodeMCU V1.0 pin D1
LED, yellow pin CESP8266 NodeMCU V1.0 pin GND
LED, yellow pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · ESP8266 NodeMCU V1.0
// Fade: an LED on D1 breathing, on the software PWM.
//
// There is no PWM hardware on an ESP8266: analogWrite moves the pin from a
// timer handler. Two things follow. The range is 0 to 1023, not 0 to 255, and
// the core wakes twice a period to do the work, so a fading LED costs
// something here where on an ESP32 it costs nothing.

const int kLed = D1;

void setup()
{
    pinMode(kLed, OUTPUT);
    analogWriteFreq(1000);
}

void loop()
{
    for (int duty = 0; duty <= 1023; duty += 8) {
        analogWrite(kLed, duty);
        delay(4);
    }
    for (int duty = 1023; duty >= 0; duty -= 8) {
        analogWrite(kLed, duty);
        delay(4);
    }
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Makes kLed (D1) an output and calls analogWriteFreq to set the PWM frequency to 1000 Hz.

void setup()
{
    pinMode(kLed, OUTPUT);
    analogWriteFreq(1000);
}

loop sketch.ino

The first for loop raises duty from 0 to 1023 in steps of 8, calling analogWrite and waiting 4 ms each time, so the LED fades up. The second loop counts back down, so it fades out. Then it repeats.

void loop()
{
    for (int duty = 0; duty <= 1023; duty += 8) {
        analogWrite(kLed, duty);
        delay(4);
    }
    for (int duty = 1023; duty >= 0; duty -= 8) {
        analogWrite(kLed, duty);
        delay(4);
    }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED is always at full brightness or off. This project
    The range is 0 to 1023 here. A value like 255 is only a quarter bright.
  • The LED never lights. This project
    Check its direction. The anode (A) goes to the resistor from D1, and the cathode (C) to GND.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • The D numbers on the silkscreen do not match GPIO numbers, so the wrong pin is driven. D0 is GPIO16, D1 is GPIO5, D2 is GPIO4, D3 is GPIO0, D4 is GPIO2. ESP8266 NodeMCU V1.0
    Use the D names from the board package (D1, D2) or look up the real GPIO. The built-in LEDs are active low, so LOW turns them on.
  • The board will not boot or flash. GPIO0, GPIO2 and GPIO15 set the boot mode, so pulling them the wrong way at reset stops it. ESP8266 NodeMCU V1.0
    Keep D3 (GPIO0) and D4 (GPIO2) high and D8 (GPIO15) low at reset. Avoid hanging switches or loads on those pins at startup.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.