CNAP

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 IDLabelDescription
1.l1 LeftTerminal 1 — left side
2.l2 LeftTerminal 2 — left side (internally connected to 1.l)
1.r1 RightTerminal 1 — right side
2.r2 RightTerminal 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

NameDescriptionDefault
colorButton cap colour: "green", "red", "blue", "yellow", "white", "black""green"

Wiring

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

DescriptionAttrs
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Ω)