Tutorials / Input
Push button
A momentary contact: HIGH while released, LOW while pressed — the first digital input.
- LED
- Button
- Potentiometer
What it is
A tactile switch closes a contact while you hold it. Wired between a GPIO and ground, with the chip's internal pull-up resistor on (pinMode(pin, INPUT_PULLUP)), the pin reads HIGH at rest and LOW while pressed. That inversion — “pressed is LOW” — trips up every beginner once.
In the simulator
The block is the button itself: press it with the mouse (or a finger, on a tablet) and hold. The Contact setting picks the kind: normally open (NA), the usual one, or normally closed (NF), which inverts the levels.
Pins
| Pin | What it is |
|---|---|
| S | signal — to a GPIO configured as an input |
| GND | the other side of the contact — to a GND symbol |
Wiring
The circuit below is the example Blink with button and potentiometer from the lab — open it with Project → Open example… and it comes ready to run.
| Button pin | Goes to |
|---|---|
| S | D4 on the board (GPIO 4) |
| GND | a GND symbol |
Settings
Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.
| Setting | Accepts | Default | Notes |
|---|---|---|---|
| Contact | Normally open (NA) · Normally closed (NF) | Normally open (NA) | NA: HIGH when released, LOW when pressed. NF inverts it. |
Code
#include <Arduino.h>
const int LED = 2;
const int BOTAO = 4;
const int POT = 34;
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode(BOTAO, INPUT_PULLUP);
Serial.println("ready");
}
void loop() {
// the potentiometer sets the blink speed
int leitura = analogRead(POT);
int intervalo = map(leitura, 0, 4095, 60, 1000);
// while the button is held down the LED stays on
if (digitalRead(BOTAO) == LOW) {
digitalWrite(LED, HIGH);
delay(100);
return;
}
digitalWrite(LED, HIGH);
delay(intervalo);
digitalWrite(LED, LOW);
delay(intervalo);
Serial.printf("pot=%d interval=%d\n", leitura, intervalo);
}
pinMode(BOTAO, INPUT_PULLUP), then digitalRead(BOTAO) == LOW means pressed. In the example, holding the button keeps the LED on; released, the LED blinks at the speed set by the potentiometer.
Run it
Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.
Press and hold the button on the canvas: the LED stays on and the blinking stops. Release it and the blink resumes.
How the simulation models it
- The block drives the level of its signal pin: HIGH at rest and LOW while pressed for a normally-open contact. The chip's pull-up resistor is not simulated — the block plays its part — so write
INPUT_PULLUPin the code anyway: that is what makes the same sketch work on the bench. - There is no contact bounce. A real button bounces for a few milliseconds; the debounce your code should have will not be exercised here.
- Reading the button before wiring S to a GPIO gives nothing: the message area tells you which pin is missing.
Try this
- Count presses and print the count on every release — you will need to remember the previous reading.
- Switch the contact to NF in Settings and see the readings invert without touching the code.
- Add a second button that halves the blink interval while held.
See also
- Slide switch — A contact that stays where you leave it — on/off state, read like a button.
- LED — The first output of every project: a GPIO set HIGH lights it, LOW turns it off.
- Your first circuit: blink an LED — From an empty canvas to a blinking LED — placing a part, drawing wires, writing the code, saving and running. Ten minutes, and you will have used every part of the lab once.