top of page

LED Light Switch

This project demonstrates how to control an LED using a momentary switch and an Arduino Uno. A momentary switch, also known as a push button, allows the LED to turn on only when the button is pressed. This simple setup is a great way to understand the basics of digital input and output with the Arduino.

When the button is pressed, it completes the circuit and sends a HIGH signal to a specified pin on the Arduino. The Arduino reads this signal and turns the LED on. When the button is released, the signal returns to LOW, and the LED turns off.

Materials Anchor

Materials

LED Light Switch

20241209_014638.jpg

To build a momentary button activated LED project with an Arduino Uno, you'll need the following materials:

Components:

​

1) Arduino Uno (or any compatible Arduino board)

  • The microcontroller that will process the input from the switch and control the LED.

​​

2) Momentary Push Button (Normally Open)

  • A button that closes the circuit only when pressed.

​​

3) LED

  • The visual output that will light up when the button is pressed.

​​

4) 220Ω Resistor

  • A resistor to limit the current to the LED and prevent it from burning out.

​​

5) 10kΩ Resistor

  • A pull-down resistor to ensure the Arduino reads a LOW signal when the button is not pressed.

​​

6) Breadboard

  • A prototyping board to connect the components without soldering.

​​

7) Jumper Wires

  • Used to connect the components on the breadboard and to the Arduino.

​​

8) USB Cable

  • To connect the Arduino to your computer for programming and power.

Setup Anchor

Basic Setup

Fritzing.png

1) Connect the LED:

  • Connect the longer leg (anode) of the LED to a digital pin on the Arduino (e.g., pin 13) through a 220Ω resistor.

  • Connect the shorter leg (cathode) of the LED to the ground (GND) on the Arduino.

​

2) Connect the Push Button:

  • Connect one terminal of the button to 5V (power).

  • Connect the other terminal of the button to a digital pin (e.g., pin 2) on the Arduino.

  • Add a 10kΩ pull-down resistor between the button terminal connected to pin 2 and GND.

​​​

4) Power the Arduino:
 

  • Connect the Arduino to your computer using the USB cable. This will provide power to the Arduino and allow you to upload the code.

schematic.png
Code
code.webp

CODE BREAK-DOWN

Code Break Down
  • Define Pins for LED and Switch

    Two constants are defined: ledPin, which specifies the pin number (13) where the LED is connected, and switchPin, which specifies the pin number (2) where the switch is connected.

​

  • Define the switchState Variable

    switchState is a variable initialized to 0 and will be used to store the state of the switch (either HIGH or LOW) as the program reads it.

​

  • The setup Function

    This function runs once when the Arduino is powered on or reset.

    • pinMode(ledPin, OUTPUT); sets the LED pin (13) as an output, allowing the Arduino to control the LED.

    • pinMode(switchPin, INPUT); sets the switch pin (2) as an input, enabling the Arduino to read the switch state.

​

  • The loop Function

    The loop function runs continuously after setup completes. It checks the state of the switch and controls the LED accordingly:

    • switchState = digitalRead(switchPin); reads the current state of the switch (HIGH if pressed, LOW if not) and assigns it to switchState.

    • The if statement checks the value of switchState. If the switch is pressed (HIGH), the LED is turned on by setting the ledPin to HIGH. If the switch is not pressed (LOW), the LED is turned off by setting the ledPin to LOW.

bottom of page