D1 R32 button
A button on the Wemos D1 R32’s D2 hole, which is GPIO 26, lights an LED on D3, GPIO 25, and the blue LED on GPIO 2. The whole build, a Wemos D1 R32 and 3 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13.25. On Mokxi they're free.
What you'll learn
- Board pin labels versus GPIO numbers
- Internal pull-up resistors with INPUT_PULLUP
- Reading a button that pulls the pin to ground
- Driving two LEDs from one condition
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Wemos D1 R32
- Parts: 5
How it works
The button connects pin IO26 to ground. The sketch turns on the internal pull-up, so IO26 reads HIGH when the button is up and LOW while it is pressed.
Every 10 ms the sketch checks the button. If it is pressed, it drives IO25 HIGH, which lights the green LED through the 220 ohm resistor, and it lights the board's own LED too.
Parts list
5 parts, plus the jumper wires. They retail for about $13.25; on Mokxi they're free.
- 1 × Wemos D1 R32
- 1 × Full-size breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, greenIn 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 R32 pin IO25 |
| Pushbutton pin 1a | Wemos D1 R32 pin IO26 |
| Pushbutton pin 2b | Wemos D1 R32 pin GND |
| LED, green pin C | Wemos D1 R32 pin GND |
| LED, green 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.
// Button: the Uno's numbers are not the pins' numbers on a D1 R32.
//
// The hole printed D2 is GPIO 26 and the hole printed D3 is GPIO 25, so the
// sketch says 26 and 25. digitalWrite(2, ...) would be the blue LED on GPIO 2,
// which is the A0 hole, not D2.
const int kButton = 26; // D2, to GND
const int kLed = 25; // D3, an LED and a resistor to GND
void setup()
{
pinMode(kButton, INPUT_PULLUP);
pinMode(kLed, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
bool pressed = digitalRead(kButton) == LOW;
digitalWrite(kLed, pressed ? HIGH : LOW);
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets kButton (26) as INPUT_PULLUP, so no outside resistor is needed. Sets kLed (25) and LED_BUILTIN as outputs. The numbers are GPIO numbers, not the D2 and D3 labels printed on the board.
void setup()
{
pinMode(kButton, INPUT_PULLUP);
pinMode(kLed, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}loop sketch.ino
Stores whether the button reads LOW in pressed, then writes that result to both the external LED and LED_BUILTIN. A short delay of 10 ms keeps the loop calm.
void loop()
{
bool pressed = digitalRead(kButton) == LOW;
digitalWrite(kLed, pressed ? HIGH : LOW);
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(10);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing happens when you press the button. This project
Use the GPIO numbers 26 and 25 in the sketch, not 2 and 3. The D2 and D3 labels do not match the GPIO numbers. - The LED is on when the button is not pressed. This project
With a pull-up, pressed means LOW. Keep the test as== LOW. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.