◈ CircuitBench ▶ Open simulator

Input Components

Pushbuttons and switches: how to read them with pull-up resistors and debounce them in code. Each entry explains what the part does, what it looks like in CircuitBench, how to wire it and, where relevant, an Arduino code example you can paste into the code panel. New to electronics? Start with the electronics basics and the Arduino tutorial.

PushbuttonSlide switch

Pushbutton

Input

What it does. A momentary switch: connected while pressed, open when released.

What it looks like. A square button with a coloured cap you can change.

How to use it. It has four legs. The two legs on the left (1a, 1b) are joined inside, and so are the two on the right (2a, 2b); pressing the button connects left to right. Wire one left leg to a board pin and one right leg to GND, then turn on the internal pull-up so the pin reads HIGH when idle and LOW when pressed.

Code example.

pinMode(2, INPUT_PULLUP);
void loop(){
  if (digitalRead(2) == LOW) {
    Serial.println("pressed");
    delay(200);   // crude debounce
  }
}
Load “Button toggle” and click the button while the simulation is running.

Slide switch

Input

What it does. A latching switch that stays in the position you leave it, useful for modes or power.

What it looks like. A small housing with a sliding lever and three legs.

How to use it. It has three legs: 1, C (the middle, common leg) and 2. The switch joins C to either 1 or 2. Wire C to a board pin and one outer leg to GND, then use INPUT_PULLUP. The state stays until you slide it again.

Code example.

bool on = digitalRead(3) == LOW;
Use it to turn an LED circuit on and off by hand.

← All components · Try it in the simulator