Push Button Switch with Arduino: 4-Leg Wiring, Pull-Up, Code and Debounce

Cartoon of a large red mushroom emergency stop button with a yellow collar beside a green-capped square tactile switch and a tiny black 6x6 tactile switch with silver legs

Quick answer: A 4-leg push button needs two wires and no resistor. Connect one leg to pin D2 and the diagonally opposite leg to GND, set the pin to INPUT_PULLUP, and a press reads LOW.

  1. Push the button across the centre gap of the breadboard.
  2. Wire one leg to D2 and the leg diagonally opposite to GND.
  3. In setup(): pinMode(2, INPUT_PULLUP);
  4. In loop(): digitalRead(2) == LOW means pressed.
  5. Always reads pressed? Both wires are on legs that are joined inside, so move one wire to the diagonal leg. Triggers by itself? The pin is on plain INPUT. One press counts as several? Add the 40 ms debounce below.

Which two legs of a 4-leg push button do you use?

A 4-leg push button (the common 6×6 tactile switch) has only two circuits. Each leg is joined inside to the leg facing it across the body, and pressing bridges one facing pair to the other.

Use two legs that sit diagonally opposite. That gives one leg from each circuit, whichever way round the button sits. Two wires on a facing pair short the pin to GND, so it always reads pressed.

Cartoon top view of a black 6x6 tactile switch with a cutaway showing two silver internal strips, each joining one facing pair of legs, a teal line across a diagonal pair and red crosses over both facing pairs
Each leg is already joined to the leg facing it across the body. Take any diagonal pair — never the two legs that face each other.

The joined pair is the wide one (6.5 mm across, 4.5 mm along on Hokuriku’s 6×6 drawing). With the button across a breadboard’s centre gap, the two legs in the same numbered row are the joined pair, so take your wires from different rows. The 12×12 capped button follows the same rule.

Parts you need

The first push button test needs only the Uno, a breadboard, two jumper wires and one 6×6 tactile button. The other parts are for later sections.

Worth adding

ItemPriceQty
Industrial Emergency Momentary Button LA38 Push Switch RED Push To Stop Button - MOMENTARY BUTTONIndustrial Emergency Momentary Button LA38 Push Switch RED Push To Stop Button - MOMENTARY BUTTONRMOMENBRM9.95
R13-507 Push Button Switch No-Locking Round Momentary Red Green Yellow Blue White Black - R13-507 BUTTON (RED)R13-507 Push Button Switch No-Locking Round Momentary Red Green Yellow Blue White Black - R13-507 BUTTON (RED)R507REDRM2.45
Data Cable Type-A Type-C MicroUSB Type-B 0.5m 1m 30cm 0.3m 100cm Data Transfer Upload Code - TYPE-A TO TYPE-B (1.0M)Data Cable Type-A Type-C MicroUSB Type-B 0.5m 1m 30cm 0.3m 100cm Data Transfer Upload Code - TYPE-A TO TYPE-B (1.0M)ATBC100RM4.90

The LA38 flat-head momentary button has the same 22 mm body and the same screw-terminal contact block as the emergency stop, so the pair shows momentary versus latching on one product family. The R13-507 is the classic 16 mm panel button. Its two terminals are solder lugs, so plan for an iron and some wire (our header-soldering guide covers it). The spare A-to-B cable is the one that programs the Uno.

Wiring a push button to an Arduino Uno

A push button on an Arduino Uno takes two wires: one leg to a digital pin, the diagonal leg to GND. The first three rows cover the first test.

From To Why
Uno GND Breadboard blue negative rail One ground feed the whole build shares
6×6 tactile, one leg Uno D2 Raw button, so you can see the bounce
6×6 tactile, the diagonally opposite leg Blue negative rail Pressing pulls D2 down to 0 V
12×12 capped button, one leg Uno D3 The debounced button
12×12 capped button, the diagonally opposite leg Blue negative rail Same two-wire pattern
LA38 e-stop, terminal 11 (NC) Uno D4 Closed while safe, so D4 sits LOW
LA38 e-stop, terminal 12 (NC) Blue negative rail Any break in this pair reads HIGH

All seven connections are male-to-male jumpers (see our jumper wire guide). At the LA38’s screw terminals, push the pin right under the clamp so the screw bites metal.

Ground every button to the blue rail: the Uno R3 has only three GND sockets.

Push button code: a first test

This first push button sketch needs only the button on D2.

// First test: one push button on D2, no resistor.
// One leg to D2, the diagonally opposite leg to GND.
const uint8_t BUTTON_PIN = 2;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);   // the pin rests HIGH
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  bool pressed = (digitalRead(BUTTON_PIN) == LOW);   // pressed reads LOW
  digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);   // on-board LED on while pressed
}

Hold the button and the Uno’s on-board LED (marked L) lights. The full sketch below uses the same D2 wiring.

Pull-up or pull-down: why pressed reads LOW

An Arduino pin on plain INPUT is connected to nothing while the push button is open, so stray charge decides what it reads. That floating input is why a button seems to press itself.

pinMode(pin, INPUT_PULLUP) switches in a 20–50 kΩ resistor inside the ATmega328P, between the pin and 5 V. With the button open the pin reads HIGH. Pressing joins the pin straight to GND, so it reads LOW. This is called active LOW, and it needs no external resistor.

A pull-down works the other way round: the button between the pin and 5 V, a 10 kΩ resistor from the pin to GND, and pinMode(pin, INPUT). A press then reads HIGH. The Uno has no built-in pull-down, so pull-up is the default.

Never wire a button straight between 5 V and GND. A press then shorts the supply and the Uno resets.

Debounce with millis(), not delay()

A push button’s contacts are springy metal, and they bounce several times before they settle. Jack Ganssle put eighteen switches on a scope. Across sixteen ordinary ones he measured an average of 1,557 µs of bouncing and 6,200 µs at the worst, and one bad switch chattered for 157 ms. The Uno checks the pin thousands of times a second, so it counts every bounce as a fresh press.

The sketch’s 40 ms covers the 6.2 ms worst case six times over and stays under the roughly 100 ms at which a button feels laggy.

Do not use delay(50): for those 50 ms the Arduino cannot read a second button or check the e-stop. Record the millis() time when the level moves and accept the new level only once it has held still.

No emergency stop yet? Put a jumper from D4 to the blue negative rail first. With D4 open, the sketch prints E-STOP OPEN - load off and ignores both buttons, because the fail-safe reads an open circuit as a stop.

// Push buttons on an Arduino Uno: raw vs debounced, plus a fail-safe e-stop.
// Every switch simply shorts its pin to GND, so no external resistors.
//   D2  6x6 tactile, DIAGONAL legs: one to D2, one to the GND rail   (raw)
//   D3  12x12 tactile, DIAGONAL legs: one to D3, one to the GND rail (debounced)
//   D4  e-stop NC pair: one screw terminal to D4, one to the GND rail
//       GND rail = the breadboard's blue rail, fed once from an Uno GND pin
//   D13 the on-board LED stands in for the machine

const uint8_t PIN_RAW   = 2;
const uint8_t PIN_CLEAN = 3;
const uint8_t PIN_ESTOP = 4;

// Ganssle measured 6.2 ms of bounce worst case across 16 ordinary switches.
// 40 ms clears that with a wide margin and is still under the ~100 ms at
// which a person starts to feel a button as laggy.
const unsigned long DEBOUNCE_MS = 40;

unsigned long rawCount = 0, cleanCount = 0;
bool rawLast = HIGH;
bool cleanReading = HIGH;    // whatever the pin says right now
bool cleanAccepted = HIGH;   // the level we have agreed to believe
unsigned long cleanChangedAt = 0;
bool wasHalted = false;

void report() {
  Serial.print(F("raw "));
  Serial.print(rawCount);
  Serial.print(F("   debounced "));
  Serial.println(cleanCount);
}

void setup() {
  Serial.begin(9600);
  pinMode(PIN_RAW,   INPUT_PULLUP);
  pinMode(PIN_CLEAN, INPUT_PULLUP);
  pinMode(PIN_ESTOP, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println(F("Press each button once, then compare the two counts."));
}

void loop() {
  // --- The e-stop is read first, every pass. ---------------------------
  // The NC pair is closed while all is well, holding the pin at 0 V. Pushing
  // the mushroom head opens it - and so does a cut wire, a loose screw or an
  // unplugged lead. Every one of those reads HIGH, and every one of them
  // stops the load. That is what fail-safe means in practice.
  bool halted = (digitalRead(PIN_ESTOP) == HIGH);
  digitalWrite(LED_BUILTIN, halted ? LOW : HIGH);
  if (halted != wasHalted) {
    Serial.println(halted ? F("E-STOP OPEN - load off")
                          : F("E-stop closed - load on"));
    wasHalted = halted;
  }
  if (halted) return;   // nothing below runs while the chain is open

  // --- D2: believe the pin the instant it moves. -----------------------
  bool rawNow = digitalRead(PIN_RAW);
  if (rawLast == HIGH && rawNow == LOW) {   // pull-up beaten: contact closed
    rawCount++;
    report();
  }
  rawLast = rawNow;

  // --- D3: believe the pin only after it has held still. ---------------
  bool cleanNow = digitalRead(PIN_CLEAN);
  if (cleanNow != cleanReading) {    // the level moved: restart the clock
    cleanReading = cleanNow;
    cleanChangedAt = millis();
  }
  // millis() keeps counting on its own, so the rest of loop() is never
  // blocked while we wait - that is the whole difference from delay(40).
  if (millis() - cleanChangedAt >= DEBOUNCE_MS && cleanReading != cleanAccepted) {
    cleanAccepted = cleanReading;
    if (cleanAccepted == LOW) {
      cleanCount++;
      report();
    }
  }
}

It compiles to 2,722 bytes, 8% of the Uno’s flash. Open the Serial Monitor at 9600 baud and press each button ten times. The debounced counter says ten and the raw one usually says more.

Cartoon waveform comparison, a dark navy line breaking into six ragged spikes on the left and a single clean step on the right, each change ringed in teal
The same single press, seen twice. Raw, the contact chatters between HIGH and LOW several times; debounced, the sketch waits for the line to hold still and reports one press.

Contact bounce on a scope:

Button not working? Check these

Match your push button’s symptom to the fix.

  • Always reads pressed. Both wires are on a facing pair of legs, or the button is not across the centre gap. Move one wire to the diagonal leg, or seat the button across the gap.
  • Triggers by itself. The pin is on plain INPUT. Use INPUT_PULLUP.
  • Never reads pressed. A leg is not seated in the breadboard, or the wire is in a different row from its leg. Reseat the button and check the row.
  • Logic upside down (LED on until you press). With INPUT_PULLUP, pressed is LOW. Test for == LOW.
  • Serial Monitor shows E-STOP OPEN - load off and both buttons do nothing. D4 is not reaching GND. Without an emergency stop, put a jumper from D4 to the GND rail. With the LA38, use terminals 11 and 12 (the NC pair), and if the head is latched down, twist it to release.
  • A second button misses presses. A delay() debounce is blocking the sketch. Use millis().
  • Emergency stop on the NO pair. A broken wire goes unnoticed. Move it to NC.

Momentary, latching and maintained switches

Push switches differ in what the contact does when you let go. A momentary switch springs back. A latching one stays down: the LA38 emergency stop locks when struck and releases when the mushroom head is twisted (the white arrows on the head show which way to twist). A maintained switch stays where you flipped it.

Type When you let go Reads as Example
Momentary (miniature) Springs open An event to catch 6×6 tactile, 12×12 capped tactile
Momentary (panel) Springs open An event to catch R13-507, LA38 flat-head
Latching Stays pressed until twisted A condition to read LA38 emergency stop
Maintained Stays where you flipped it A condition to read Rocker switch, E-TEN 1121 toggle
Electronic latching Flips state on each touch A condition to read Bistable touch module
Cartoon comparing two 22mm panel buttons with identical black bodies and orange screw-terminal blocks, one with a flat red lens in a silver bezel and one with a red mushroom head on a yellow collar
One 22 mm body, two behaviours. The flat red head springs straight back; the mushroom head latches down and only lets go when it is twisted.

For an on/off power switch, see our rocker, toggle or push button guide.

Emergency stop on an Arduino: use the NC pair

Read this before you fit an emergency stop to anything that moves. An emergency stop on an Arduino input stops your code. It does not cut power to the motor or heater, and a crashed sketch keeps them running with the button pressed flat. A real e-stop breaks the load’s own supply, in the power path (see our LA38 emergency stop guide and relay safety guide). Wire the Arduino input as well, never instead.

A tactile button is NO (normally open): open until pressed. The LA38 also has an NC (normally closed) pair, closed until pressed. Take the stop signal from the NC pair. A press opens it, and so does a cut wire or a loose screw, so any break in the wiring reads as a stop. On the NO pair, a broken wire looks the same as a button nobody pressed.

The LA38’s contact block is numbered: NC is terminals 11 and 12, NO is 23 and 24. Terminals 11 and 12 sit at opposite ends of the block, and so do 23 and 24. Two screws side by side, such as 12 and 24, belong to different contacts. Wire 11 to D4 and 12 to GND, with D4 on INPUT_PULLUP.

To confirm, a multimeter on continuity beeps across 11 and 12 with the mushroom head out. With the full sketch loaded, the Uno’s on-board LED stays lit while the head is out.

The contact block carries a mains-level rating, but this page is 5 V logic only and wiring 230 V mains is not a DIY job. To switch a workshop machine’s own supply, use an enclosed KAO-5M start/stop box.

Will Donaldson on installing an emergency stop correctly:

Button terminals: legs, solder lugs, screws and modules

A button’s terminal decides which tools you need.

Terminal What it looks like How you join to it Example
Through-hole legs Bent silver legs, two per side Push into a breadboard, or solder 6×6 tactile
Solder lug Flat metal tab with a hole in it Hook the wire through and solder R13-507
Screw terminal Silver screws under a clear cover Clamp bare wire or a male pin end LA38 e-stop
Module header pins Three right-angle pins on a small PCB Male-to-female jumpers, no soldering Button sensor module

The R13-507 needs soldering (see our soldering guide). Its datasheet rates it at 6 A at 125 VAC and 3 A at 250 VAC.

Do not run a motor, pump or LED strip through a small button. The 12×12 capped button is rated for 0.1 A at 12 V DC, and overloaded contacts can weld shut and leave the load stuck ON. Use a relay or a MOSFET for the load and let the button only signal.

FAQ

Which pins of a 4-pin push button switch do I connect?

Any two that sit diagonally opposite. The legs facing each other across the body are joined inside, so those two always read pressed.

Do I need a resistor for an Arduino push button?

Not an external one. INPUT_PULLUP enables a 20–50 kΩ resistor inside the ATmega328P, so the button only connects the pin to GND and pressed reads LOW.

Should I use a pull-up or a pull-down resistor with a push button?

Use the pull-up on an Arduino Uno. It is built into the chip, so you need no extra resistor. Pressed reads LOW. A pull-down needs the button wired to 5 V and an external 10 kΩ resistor from the pin to GND. Pressed then reads HIGH.

Why is a red emergency stop button wired normally closed?

So that a fault stops the machine. A press opens the NC contact, and so does a cut wire or a loose screw.

How long should an Arduino button debounce be?

Around 40 ms. Bounce on ordinary switches peaks near 6 ms, and 40 ms is still too short to notice. Time it with millis(), never with delay().

Last updated December 2026. Stuck? Chat with us on WhatsApp.

One thought on “Push Button Switch with Arduino: 4-Leg Wiring, Pull-Up, Code and Debounce”

  1. Pingback: Vibration Motor vs Vibration Sensor for Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *