Pushbutton Reference
Momentary tactile pushbutton (SPST-NO). The circuit is open when released and closed when pressed.
Pin names
The button has four pins arranged in two pairs. Pins on the same side (1.l/2.l or 1.r/2.r) are always connected to each other internally. Pressing the button bridges the left pair to the right pair.
| Pin ID | Label | Description |
|---|---|---|
1.l | 1 Left | Terminal 1 — left side |
2.l | 2 Left | Terminal 2 — left side (internally connected to 1.l) |
1.r | 1 Right | Terminal 1 — right side |
2.r | 2 Right | Terminal 2 — right side (internally connected to 1.r) |
For most circuits you only need one pin from each side — use 1.l and 2.l (as in all CNAP templates), or 1.r and 2.r.
Attributes
| Name | Description | Default |
|---|---|---|
| color | Button cap colour: "green", "red", "blue", "yellow", "white", "black" | "green" |
Wiring
With INPUT_PULLUP (recommended — no external resistor needed)
Connect one pin to an Arduino GPIO, the other to GND. Enable INPUT_PULLUP in setup().
The pin reads HIGH when released, LOW when pressed.
Arduino Pin 2 ── 1.l ┐
│ (button)
GND ── 2.l ┘
const int BTN = 2;
void setup() {
pinMode(BTN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
if (digitalRead(BTN) == LOW) { // LOW = pressed
Serial.println("Pressed!");
delay(200); // simple debounce
}
}
With external pull-down resistor
Connect one pin to 5V, the other to both an Arduino GPIO and a 10 kΩ resistor to GND. The pin reads LOW when released, HIGH when pressed.
5V ── 1.l ┐
│ (button)
Pin 2 ── 2.l ── 10kΩ ── GND
Toggle pattern
Press to toggle an LED on/off — detects the falling edge so one press = one toggle:
const int BTN = 2;
const int LED = 13;
bool ledState = false;
bool lastBtn = HIGH;
void setup() {
pinMode(BTN, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
void loop() {
bool btn = digitalRead(BTN);
if (btn == LOW && lastBtn == HIGH) { // falling edge = press
ledState = !ledState;
digitalWrite(LED, ledState);
delay(20); // debounce
}
lastBtn = btn;
}
Examples
| Description | Attrs |
|---|---|
| Default green cap | {} |
| Red cap | { "color": "red" } |
| Blue cap | { "color": "blue" } |
See also
- wokwi-pushbutton-6mm — smaller footprint, same pin layout and logic
- Arduino Uno — connect to any digital pin; D2 and D3 also support hardware interrupts
- Resistor — needed for external pull-down wiring (10 kΩ)