Lab 01
Controlling Light Emitting Diode (LED) with a
Push Button
Abstract:
This experiment investigates the control of a Light Emitting Diode (LED) using a push button.
The primary objective is to demonstrate the functionality of a simple digital input/output system
where the state of the LED can be toggled using a push button as an input. The experiment
involves setting up the circuit, programming the microcontroller, and observing the behavior of
the LED in response to the button press. The results demonstrate the successful control of the
LED, providing insights into basic digital input/output operations.
Introduction: Light Emitting Diodes (LEDs) are semiconductor devices that emit light when an
electric current passes through them. LEDs are widely used in various applications due to their
low power consumption, long lifespan, and fast response time. In this experiment, we aim to
control the illumination of an LED using a push button as the input device.
The basic principle involves setting up a circuit where the LED is connected to a microcontroller,
and a push button is used to control the state of the LED. When the button is pressed, it triggers a
change in the microcontroller's output, which in turn affects the LED's state.
Materials and Methods:
1. Arduino Uno board
2. Breadboard
3. Light Emitting Diode (LED)
4. 220 ohm resistor
5. Push button
6. Jumper wires
Diagram: The following circuit was set up on the breadboard:
Procedure:
1. I connected the LED's anode (longer leg) to digital pin 8 of the Arduino Uno using a 220 ohm
resistor.
2. I connected the cathode (shorter leg) of the LED to the ground (GND) pin of the Arduino.
3. One terminal of the push button was connected to digital pin 2 of the Arduino.
4. The other terminal of the push button was connected to the ground (GND) pin of the Arduino.
5. I wrote a program to control the LED based on the state of the push button.
6. I uploaded the program to the Arduino board.
7. I observed the behavior of the LED when the push button was pressed and released.
Code
const int buttonPin = 2; // the pin number for the pushbutton
const int ledPin = 13; // the pin number for the LED
int buttonState = 0; // variable for reading the pushbutton status
int prevButtonState = 0; // variable to store the previous button state
void setup() {
pinMode(ledPin, OUTPUT); // initialize LED pin as an output
pinMode(buttonPin, INPUT); // initialize pushbutton pin as an input
}
void loop() {
// read the state of the pushbutton
buttonState = digitalRead(buttonPin);
// check if the button is pressed
if (buttonState == HIGH && prevButtonState == LOW) {
// toggle the LED state
digitalWrite(ledPin, !digitalRead(ledPin));
delay(100); // debounce delay to avoid multiple toggles due to button press
}
// update the previous button state
prevButtonState = buttonState;
}
Results:
Upon executing the program and pressing the push button, the LED illuminates. Releasing the
button turns off the LED. This behavior persists as long as the Arduino is powered.
Discussion:
The successful control of the LED using a push button demonstrates the basic concept of digital
input/output operations. By programming the Arduino, we can define specific actions based on
the input received from the push button. In this case, pressing the button triggers the LED to turn
on, while releasing it turns the LED off.
This experiment serves as a foundation for more complex projects involving digital inputs and
outputs. It highlights the versatility of microcontrollers like the Arduino Uno in interfacing with
various electronic components and controlling their behavior based on external stimuli.