LOLIN32 Lite button
A pushbutton on GPIO 4 lights the LOLIN32 Lite’s blue LED on GPIO 22, which comes on when its pin is LOW. The whole build, a Wemos LOLIN32 Lite and 1 more part, 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.10. On Mokxi they're free.
What you'll learn
- Reading a button with INPUT_PULLUP
- An LED that lights when its pin is LOW
- Copying an input straight to an output
- Why a pull-up button reads LOW when pressed
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Wemos LOLIN32 Lite
- Parts: 3
How it works
The button connects GPIO 4 to ground. The sketch turns on the pin's internal pull-up, so the pin sits at 3.3 V and reads HIGH until you press, when it reads LOW.
The board's blue LED on GPIO 22 is wired so it lights when its pin is LOW. That fits nicely: the sketch simply copies the button's reading to the LED pin. Pressed gives LOW on both, so the LED lights.
Parts list
3 parts, plus the jumper wires. They retail for about $10.10; on Mokxi they're free.
- 1 × Wemos LOLIN32 Lite
- 1 × Full-size breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights 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 |
|---|---|
| Pushbutton pin 2a | Wemos LOLIN32 Lite pin GND |
| Pushbutton pin 2b | Wemos LOLIN32 Lite pin GND |
| Pushbutton pin 1a | Wemos LOLIN32 Lite pin 4 |
| Pushbutton pin 1b | Wemos LOLIN32 Lite pin 4 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Button: a pushbutton on 4 lights the blue LED on 22.
//
// INPUT_PULLUP switches on the pad's 45 k pull-up, so GPIO 4 sits at 3.3 V
// until the button pulls it to GND and reads LOW while it is held. The LED on
// GPIO 22 lights when that pin is LOW, so the pin simply copies the button.
const int BUTTON = 4;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, digitalRead(BUTTON));
delay(5);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once. pinMode(BUTTON, INPUT_PULLUP) makes GPIO 4 an input with its pull-up on, so no resistor is needed. pinMode(LED_BUILTIN, OUTPUT) makes the LED pin an output.
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}loop sketch.ino
One line does the job: digitalWrite(LED_BUILTIN, digitalRead(BUTTON)) writes whatever the button reads straight to the LED. Because this LED lights on LOW, no flip is needed. Then delay(5) rests briefly.
void loop() {
digitalWrite(LED_BUILTIN, digitalRead(BUTTON));
delay(5);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED is dark until you press, then lights This project
That is correct. On this board the LED lights when its pin is LOW, which is when the button is pressed. - 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. - Some pins do not work or the board will not boot. Wemos LOLIN32 Lite
GPIO 6 to 11 are for flash, and GPIO 34 to 39 are input only. Avoid the strapping pins (0, 2, 12, 15) pulled to the wrong level at boot. - The board does not charge or run from the battery. Wemos LOLIN32 Lite
Plug the LiPo into the JST socket with the right polarity before connecting. Logic is 3.3 V, so do not feed 5 V signals into the pins.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.