Exp. No - 2
Exp. No - 2
Experiment No - 2
Experiment No 2 Student ID
Date Student Name
Description:
In this experiment the variable pinButton the value 7, indicating that we've connected a button to
pin 7 on the Arduino board. Additionally, we've connected an LED to pin 8 in series with a
resistor.
In the setup() function, we've configured pin 7 as an INPUT and pin 8 as an OUTPUT.
Within the loop() function, we're reading the state of pin 7 and storing it in the variable
stateButton. We're making decisions using an if() statement:
• If the button is pressed (when stateButton equals 1), then we provide voltage to pin 8
(setting it to HIGH).
• Otherwise, if stateButton is not equal to 1 (indicating that the button is not pressed), we
don't output voltage on pin 8.
Connection Diagram:
Program A:
Page | 11
Experiment No 2 Student ID
Date Student Name
Program B:
Circuit/Schematic Diagram:
Program A:
Page | 12
Experiment No 2 Student ID
Date Student Name
Program B:
Pre-Requisites:
• Electronics fundamentals
• Basic knowledge about Arduino Pins
Pre-Lab:
1. What is the function of push button in Arduino?
Pushbuttons or switches connect two points in a circuit. When the button is pressed, LED
is ON and when the button is released LED is off.
2. What is the use of LED in Arduino?
LED (Light Emitting Diode) is an electronic device, which emits light when the current
passes through its terminals. It is used as an ON/OFF indicator in Arduino.
3. What is the value of the push button in Arduino?
When a push button is plugged to a digital pin, the value the Arduino reads is between 0V
and 5V.
4. What are the types of push button?
• Single pole
• Single throw (SPST)
• Single pole
• Double throw (SPDT)
• Double pole, Single throw (DPST)
• Double pole, double throw (DPDT)
5. How many pins are there for push button?
Push buttons have four different pins, with two on each side. The pins that are across from
each other are internally connected. This means that if one wire is connected to the pin on
the top left and another to the top right pin the two wires would be connected together.
Page | 13
Experiment No 2 Student ID
Date Student Name
In-Lab:
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 copy the previously provided Arduino code 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.
PROGRAM:
A. Read the Push button State Using Serial Monitor
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
Page | 14
Experiment No 2 Student ID
Date Student Name
void setup() {
// 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);
Results:
Page | 15
Experiment No 2 Student ID
Date Student Name
Page | 16
Experiment No 2 Student ID
Date Student Name
Post-Lab:
Exercise/Case study Program:
1. Write a program to blink an LED using push button with a delay of one second.
Blinking LED using Push Button with 1 sec delay.
Program:
// Define the pin to which the button is connected
int buttonPin = 7;
// Define the pin to which the LED is connected
int ledPin = 8;
void setup() {
// 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
Page | 17
Experiment No 2 Student ID
Date Student Name
2. Arduino program that uses a push-button switch and an LED to create a basic
counter. Each time you press the button, it increments the count displayed on the
LED.
Page | 18
Experiment No 2 Student ID
Date Student Name
int buttonPin = 7;
int ledPin = 8;
int count = 0;
boolean buttonPressed = false;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && !buttonPressed) {
buttonPressed = true;
count++;
digitalWrite(ledPin, HIGH);
delay(100); // Display LED for a brief moment to indicate button press
digitalWrite(ledPin, LOW);
Serial.println("Count: " + String(count));
} else if (buttonState == LOW) {
buttonPressed = false;
}
}
(Verify the output on serial monitor)
Results:
Page | 19