Momentary Switch LED Toggle

Z2M Part: EDM-00009-A

Toggle LED on/off with momentary push button.

Circuit Diagram
Circuit diagram for Momentary Switch LED Toggle
Wire Connections & Pin Configuration

Wire Color Connections:

Yellow → Pin 4 to switch, Pin 13 to LED
Black → GND to switch (other leg), LED cathode

Pin Configuration:

Arduino Digital Pin 4 → Momentary switch (one leg)
Arduino GND → Momentary switch (other leg)
Arduino Digital Pin 13 → LED anode (via 220Ω resistor)
Arduino GND → LED cathode
Arduino Code
Edit
int led = 13;
int button = 4;

/* Initialize LED and button states */
int ledState = LOW;
int buttonCurrent;
int buttonPrevious = HIGH;

void setup() {
  pinMode(button, INPUT_PULLUP); // Use internal pull-up resistor
  pinMode(led, OUTPUT);          // LED as output
  Serial.begin(9600);            // Start serial monitor for debugging
}

void loop() {
  // Read current button state
  buttonCurrent = digitalRead(button);

  // Detect button press (transition from LOW to HIGH)
  if (buttonCurrent == HIGH && buttonPrevious == LOW) {
    ledState = !ledState; // Toggle LED state
  }

  // Print button state to serial monitor
  Serial.println(buttonCurrent);

  // Update previous button state
  buttonPrevious = buttonCurrent;

  // Apply LED state
  digitalWrite(led, ledState);

  delay(200); // Simple debounce delay
}

Instruction Details

Wiring: Connect momentary switch one leg to Digital Pin 4, other leg to GND. Connect LED anode to Digital Pin 13 via 220Ω, cathode to GND.
Library: Not required (built-in).
Upload Code: Tools → Board → Arduino Uno. Tools → Port → select your COM port. Click Upload button.
View Output: Press button to toggle LED ON/OFF. Each press changes LED state.

How to Use

  1. Connect the required components as per the Pin Configuration above
  2. Open Arduino IDE and create a new sketch
  3. Copy and paste the code above
  4. Select your Arduino board and COM port from Tools menu
  5. Click the Upload button to upload the code to your Arduino
  6. Open Serial Monitor (if applicable) to see the output

Components Required

  • Arduino Uno
  • Momentary Push Button
  • LED
  • 220Ω Resistor
  • Breadboard
  • Jumper Wires

Category: LEDs & Display