D1 mini blink
The module’s blue LED on D4 lights when the pin is low and an LED on D1 when it is high, so the two are always opposite. The whole build, a Wemos D1 mini 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 $9.15. On Mokxi they're free.
What you'll learn
- Active-low versus active-high LEDs
- D-pin names versus GPIO numbers on ESP8266 boards
- Using the board's built-in LED
- Wiring an external LED with a resistor
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Wemos D1 mini
- Parts: 4
How it works
The D1 mini has a blue LED of its own on D4, which is GPIO2. That LED is active low, so LOW lights it. You also wire a red LED from D1 through a 220 ohm resistor to ground, the ordinary way, so HIGH lights it.
The sketch writes opposite values to the two pins, waits half a second, then swaps them. Because one LED lights on LOW and the other on HIGH, the two are always opposite: when the blue one is on, the red one is off.
Parts list
4 parts, plus the jumper wires. They retail for about $9.15; on Mokxi they're free.
- 1 × Wemos D1 mini
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
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 pin | Connects to |
|---|---|
| Resistor, 220 Ω pin 1 | Wemos D1 mini pin D1 |
| LED, red pin C | Wemos D1 mini pin GND |
| LED, red pin A | Resistor, 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.
// 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, the module's own LED on LED_BUILTIN (GPIO2, D4), and kWired, the LED you built on D1 (GPIO5).
void setup()
{
pinMode(kLed, OUTPUT);
pinMode(kWired, OUTPUT);
}loop sketch.ino
Writes LOW to kLed (lit) and HIGH to kWired, waits 500 ms, then writes HIGH to kLed (dark) and LOW to kWired, and waits again. The two pins always hold opposite values.
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 red LED never lights This project
Check its direction: the cathode goes to GND, and the resistor connects D1 to the anode. - You expected both LEDs to light together This project
They are opposite on purpose, because the built-in LED lights on LOW. Swap the HIGH and LOW on one pin if you want them together. - 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 sketch fails to boot or the board will not upload. Wemos D1 mini
D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) are boot pins. Do not pull them to the wrong level at reset. Select the right board and speed. - Pins misread with 5 V signals. Wemos D1 mini
The ESP8266 runs on 3.3 V logic and is not 5 V tolerant. The only analog input A0 reads up to 3.2 V on the D1 mini, not 5 V.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.