Built-in project · ESP-01S

ESP-01 blink

An LED wired from 3V3 through a resistor to GPIO 2 lights together with the module’s own blue one, whenever the pin goes low. The whole build, an ESP-01S 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 $7.65. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Active-low outputs: LOW turns the LED on
  • Wiring an LED between 3V3 and a pin
  • Current-limiting resistor for an LED
  • Blinking with delay
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: ESP-01S
  • Parts: 4

How it works

The ESP-01S has a blue LED built in on GPIO2. It is active low, which means the LED lights when the pin goes LOW, not HIGH. You add a second LED that goes from 3V3 through the LED and a 220 Ω resistor to GPIO2.

Because both LEDs are connected to 3V3 at one end and to GPIO2 at the other, they behave the same way. When the pin goes low, current flows through both and both light. When it goes high, both go dark. The sketch toggles every half second.

Parts list

4 parts, plus the jumper wires. They retail for about $7.65; 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 2ESP-01S pin GPIO2
LED, blue pin AESP-01S pin 3V3
Resistor, 220 Ω pin 1LED, blue pin C

The code

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

sketch.ino · ESP-01S
// Blink: the module's own LED on GPIO2, and an LED you wire yourself.
//
// The LED on an ESP-12 module is GPIO2, which the NodeMCU silkscreen calls D4,
// and it is active low: LOW lights it. On a NodeMCU or a D1 mini the wired LED
// goes on D1 (GPIO5) with a resistor to ground, the ordinary way round, so the
// two are always opposite.
//
// An ESP-01S has no D1 pin. There the wired LED goes from 3V3 through a
// resistor to GPIO2, the same way round as the module's own blue one, so the
// two light together and the D1 lines below drive nothing you can see.

const int kLed = LED_BUILTIN; // GPIO2, D4, active low
const int kWired = D1;        // GPIO5, an LED and a resistor to ground (not on an ESP-01S)

void setup()
{
    pinMode(kLed, OUTPUT);
    pinMode(kWired, OUTPUT);
}

void loop()
{
    digitalWrite(kLed, LOW); // lit
    digitalWrite(kWired, HIGH);
    delay(500);
    digitalWrite(kLed, HIGH); // dark
    digitalWrite(kWired, LOW);
    delay(500);
}

Code walkthrough

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

setup sketch.ino

Sets both pins as outputs. kLed is the built-in LED on GPIO2. kWired is D1 (GPIO5), which the sketch also drives, but an ESP-01S has no D1 pin, so you will not see anything from it.

void setup()
{
    pinMode(kLed, OUTPUT);
    pinMode(kWired, OUTPUT);
}

loop sketch.ino

Writes LOW to kLed, which lights it, waits 500 ms, then writes HIGH, which turns it off, and waits 500 ms. Your wired LED is on the same pin, so it lights and goes dark together with the blue one.

void loop()
{
    digitalWrite(kLed, LOW); // lit
    digitalWrite(kWired, HIGH);
    delay(500);
    digitalWrite(kLed, HIGH); // dark
    digitalWrite(kWired, LOW);
    delay(500);
}

Troubleshooting

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

  • The LED is on when you expect it off This project
    The LED is wired from 3V3, so it is active low. LOW means lit and HIGH means dark.
  • The wired LED never lights This project
    Check the polarity. The anode (A) goes to 3V3 and the cathode (C) goes through the resistor to GPIO2.
  • 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 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 module resets, does not boot, or acts randomly. ESP-01S
    It needs a stable 3.3 V supply, never 5 V, and can pull up to 300 mA. Use a proper regulator with a capacitor. Do not rely on the Arduino's 3.3 V pin.
  • The module will not enter program mode. ESP-01S
    Tie GPIO0 low at boot to flash, and CH_PD (EN) high. Pull GPIO0 high for normal running. GPIO15 style pins are not on this module, and GPIO2 must be high at boot.

Change it and keep it

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