Experiment 2 - Push Button LED
Experiment 2 - Push Button LED
Experiment No - 2
Experiment No 2 Student ID
Date Student Name
Components required:
Software Hardware
Arduino UNO Board
Tinkercad Arduino UNO Cable
Arduino IDE (To be installed in Laptop) LED (Red-1)
Push Button - 1
Resistors – 2 (1KΩ each)
Jumper wires
Pre-Requisites:
Electronics fundamentals
Basic knowledge about Arduino Pins
Pre-Lab:
Page | 2
In-Lab:
PROGRAM:
void setup()
{
Serial.begin(9600);
// Set pin 7 as an INPUT (for the button)
pinMode(buttonPin, INPUT);
// Set pin 8 as an OUTPUT (for the LED)
pinMode(ledPin, OUTPUT);
}
void loop()
{
// Read the state of the button and store it in the variable stateButton
int stateButton = digitalRead(buttonPin);
Serial.println(stateButton);
else
{
// Button is not pressed, do not output voltage on pin 8 (turn off the LED)
digitalWrite(ledPin, LOW);
}
}
Page | 3
PROCEDURE:
I. Connect the Components:
Place the Arduino board on a stable surface.
Connect one leg of the push-button switch to digital pin 7 on the Arduino.
Connect the other leg of the push-button switch to one end of a resistor.
Connect the other end of the resistor to the ground (GND) pin on the Arduino to create
a pull-down resistor configuration.
Connect one leg of the LED (the longer leg, the anode) to digital pin 8 on the Arduino.
Connect the shorter leg of the LED (the cathode) to a current-limiting resistor (220-
330 ohms).
Connect the other end of the resistor to the ground (GND) pin on the Arduino to
complete the LED circuit.
II. Upload the Arduino Code:
Open the Arduino IDE on your computer.
Create a new sketch and write the code provided into the sketch.
Verify the code for any errors by clicking on the checkmark icon.
If the code verifies successfully, upload it to your Arduino board by clicking on the
right arrow icon.
III. Test the Circuit:
After uploading the code, open the Serial Monitor in the Arduino IDE to observe the
button's state.
When you press the button, the LED connected to pin 8 should turn on. When you
release the button, the LED should turn off.
Connection Diagram:
Page | 4
Results:
Page | 5
Post-Lab:
1. Write a program to blink an LED with a delay of one second when the push button
released, LED off when button pressed.
Program:
// Constants
const int ledPin = 8; // LED connected to digital pin 8
const int buttonPin = 7; // Push button connected to digital pin 7
// Variables
int buttonState = 0; // Variable for reading the push button status
bool ledState = LOW; // Variable for storing the LED state
void setup() {
// Initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
void loop() {
// Read the state of the push button value:
buttonState = digitalRead(buttonPin);
Page | 6