The Direct Answer: Wiring a Button to Arduino for Mains Control

To wire a button to an Arduino for controlling a 120V AC load, you route a momentary SPST (Single Pole Single Throw) switch to a digital input pin using 22 AWG stranded wire, leveraging the microcontroller's internal pull-up resistor. The Arduino's 5V output simultaneously drives an opto-isolated relay module, which acts as the galvanic barrier to switch the 14 AWG mains circuit. The button never touches mains voltage; it only pulls the logic pin to ground to trigger the relay's low-voltage coil.

This guide covers the exact physical terminations, wire color codes, and multimeter verification steps required to build a reliable, code-compliant smart switch interface. We are using a standard 15A, 120V AC branch circuit as our load example, switched via a 10A-rated mechanical relay module.

Tools, Materials, and Component Ratings

Before stripping any wire, verify your component ratings. Mixing low-voltage logic with mains voltage requires strict adherence to ampacity and insulation ratings.

Required Tools

  • Wire Strippers: Klein Tools 11055 (for 22-14 AWG range)
  • Digital Multimeter: Fluke 117 or equivalent with CAT III 600V rating for mains verification
  • Crimpers: Ferrule crimper for 22 AWG logic wire (prevents stranded wire fraying in screw terminals)
  • Screwdrivers: Precision flathead (2.4mm) for logic terminals; #2 Robertson/Phillips for mains terminals

Materials and Ratings

  • Microcontroller: Arduino Uno R3 or R4 (5V logic)
  • Relay Module: 5V Opto-isolated Relay Module with Songle SRD-05VDC-SL-C relay (Rated: 10A @ 250VAC / 15A @ 120VAC)
  • Switch: Panel-mount SPST Momentary Push Button (Normally Open)
  • Logic Wire: 22 AWG stranded hook-up wire (Red, Black, Yellow, Blue)
  • Mains Wire: 14 AWG THHN copper (Black, White, Green) — rated for 15A at 60°C/75°C per NEC Table 310.16
  • Load: 120V AC device (e.g., work lamp, max 1200W / 10A to maintain 80% continuous load safety margin on the 15A circuit)

Pinout and Termination Specification Table

The table below maps every termination point in the system. Keep this reference visible on your bench during assembly to prevent cross-wiring logic and mains domains.

Component Terminal / Screw Wire Color AWG Function / Routing
Push Button Terminal 1 Black 22 Routes to Arduino GND
Push Button Terminal 2 Yellow 22 Routes to Arduino Digital Pin 2
Relay Module VCC Pin Red 22 Routes to Arduino 5V Pin
Relay Module GND Pin Black 22 Routes to Arduino GND Pin
Relay Module IN1 Pin Blue 22 Routes to Arduino Digital Pin 3
Relay Module (Mains) COM (Common) Screw Black 14 Mains Line (Hot) IN from breaker panel
Relay Module (Mains) NO (Normally Open) Screw Red 14 Mains Line (Hot) OUT to load
Mains Load Neutral Terminal White 14 Direct bypass from panel Neutral (unswitched)
Mains Load Chassis Ground Green 14 Direct bypass from panel Ground (unswitched)

Step-by-Step Wiring Procedure

⚠️ CRITICAL MAINS SAFETY WARNING

This procedure involves terminating 120V AC mains wiring. Before touching any 14 AWG THHN wires, you must de-energize the circuit at the breaker panel. Apply a lockout/tagout (LOTO) device to the breaker to prevent accidental re-energization (OSHA Hazardous Energy Control guidelines). Verify the circuit is dead using a CAT III rated multimeter or non-contact voltage tester before proceeding. If you are not comfortable working inside electrical panels, hire a licensed electrician to run the dedicated 15A branch circuit to your project enclosure. Local AHJ (Authority Having Jurisdiction) codes always supersede general DIY guidance.

Phase 1: Low-Voltage Logic Wiring

  1. Prep the Logic Wires: Cut three 6-inch lengths of 22 AWG stranded wire (Red, Black, Blue) and two 12-inch lengths (Black, Yellow). Strip 5mm of insulation from each end. Crimp 5mm bootlace ferrules on all ends to prevent the "gumby" effect where stranded wire flattens and loses contact under screw terminals.
  2. Wire the Push Button: Solder or screw the 12-inch Black wire to Terminal 1 of the SPST push button. Screw the 12-inch Yellow wire to Terminal 2. Route the Black wire to the Arduino GND pin and the Yellow wire to Arduino Digital Pin 2. (We are using the microcontroller's internal pull-up resistor, so the button switches ground, not 5V. See the Arduino Digital Pins documentation for the electrical theory on internal pull-ups).
  3. Wire the Relay Logic Side: Insert the 6-inch Red wire into the Relay Module VCC pin and the Arduino 5V pin. Insert the 6-inch Black wire into the Relay Module GND pin and a second Arduino GND pin. Insert the 6-inch Blue wire into the Relay Module IN1 pin and Arduino Digital Pin 3.
  4. Verify Logic Connections: Give each logic screw terminal a gentle tug (approx. 1 lb of force) to ensure the ferrule is fully seated and clamped.

Phase 2: Mains Voltage Wiring (De-energized)

  1. Verify Dead Circuit: Set your multimeter to AC Voltage (V~). Probe the Black (Line) and White (Neutral) wires from the wall supply. The reading must be exactly 0.00V.
  2. Terminate Mains Line IN: Strip 12mm of insulation from the 14 AWG Black supply wire. Insert it into the Relay Module's COM (Common) screw terminal. Torque the screw firmly (approx. 0.5 Nm) until the wire cannot be pulled out by hand.
  3. Terminate Mains Line OUT: Strip 12mm from a 14 AWG Red jumper wire. Insert one end into the Relay Module's NO (Normally Open) screw terminal. Connect the other end to the Hot/Line terminal on your 120V AC load.
  4. Bypass Neutral and Ground: The relay only switches the hot leg. Wire-nut the 14 AWG White supply Neutral directly to the load's Neutral terminal. Wire-nut the 14 AWG Green supply Ground directly to the load's chassis ground screw. Never switch the neutral or ground through a standard relay.
  5. Enclosure Isolation: Ensure the Arduino and the low-voltage side of the relay module are physically separated from the 14 AWG mains wires inside your project box by at least 1/4 inch, or use an insulating acrylic barrier. Stranded 14 AWG wire can spring loose and short against 5V logic if left unconstrained.

Verification and Multimeter Testing

Never apply power and hope for the best. Follow this strict verification sequence to validate your terminations before energizing the mains.

  1. Button Continuity Test (Power Off): Set the multimeter to Continuity mode (the diode/sound wave symbol). Place the red probe on the Arduino D2 header and the black probe on the Arduino GND header. With the button released, the meter should read OL (Open Line). Press and hold the button; the meter should beep and read < 1.0 Ω. If it reads OL while pressed, your button wiring is broken.
  2. Relay Coil Resistance Test (Power Off): Probe the VCC and GND pins on the relay module. You should read between 70 Ω and 100 Ω (the exact coil resistance of a standard 5V Songle relay). A reading of 0 Ω indicates a short; OL indicates a blown coil or broken trace.
  3. Logic Power-Up Test: Plug the Arduino into your PC via USB. Upload a basic blink sketch that toggles D3 HIGH and LOW every 2 seconds. You should hear the relay module click mechanically, and the LED on the module should blink in sync. The button is not required for this step.
  4. Mains Voltage Verification (Power On): Re-energize the breaker panel. Set your multimeter to AC Voltage. Carefully probe the load's input terminals (Red wire from NO and White Neutral). With the relay off, you should read 0V. Trigger the relay via code (or by momentarily pressing the button if your code is loaded). The meter should immediately jump to 114V - 126V AC (standard US nominal 120V range).

The Most Common Botch: Floating Inputs and Relay Chatter

The single most frequent mistake makers encounter when learning how to wire a button to Arduino is the floating input pin.

The Symptom: You wire the button to 5V and D2, but forget to add a pull-down resistor to ground. When you release the button, the relay starts clicking randomly, or triggers when you simply wave your hand near the wires.

The Cause: Microcontroller digital pins have incredibly high input impedance (often >100 MΩ). When the button is open (released), the pin is not connected to 5V or GND; it is "floating." In this state, the pin acts like an antenna, picking up 60Hz electromagnetic interference (EMI) from the nearby mains wiring, static discharge from your body, and ambient RF noise. The Arduino interprets this noise as rapid HIGH/LOW transitions, causing the relay to chatter. This chatter will rapidly pit and destroy the relay's mechanical contacts due to arc welding.

The Fix: Never leave an input pin floating. You have two options:

  • Software Fix (Recommended for this build): Wire the button between D2 and GND (as detailed in Phase 1). In your Arduino sketch, use pinMode(2, INPUT_PULLUP);. This activates an internal 20kΩ-50kΩ resistor tying the pin to 5V. When the button is open, the pin reads a stable HIGH. When pressed, it shorts to GND and reads LOW. This requires zero extra components.
  • Hardware Fix (For high-noise industrial environments): If the Arduino is mounted near heavy motors or VFDs, the internal pull-up may be too weak. Wire the button to 5V and D2, and solder an external 10kΩ 1/4W carbon film resistor between D2 and GND. This provides a much stiffer, noise-immune pull-down path.

By strictly adhering to the wire color codes, respecting the boundary between 22 AWG logic and 14 AWG mains, and verifying your terminations with a multimeter, you ensure a robust interface that won't destroy your microcontroller or create a fire hazard.