0% found this document useful (0 votes)
8 views

Exp. No - 2

456445
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Exp. No - 2

456445
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

FUNDAMENTALS OF IOT

AND SENSOR – 23EC1101

Experiment No - 2
Experiment No 2 Student ID
Date Student Name

CONTROLLING LED INTERFACE WITH ARDUINO


USING PUSH BUTTON
Aim/Objective: The main objective of this experiment is,
A. To read the Push button State Using Serial Monitor
B. To turn ON an LED when a button is pressed, OFF when button is released.

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;

// the setup routine runs once when you press reset:


void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input pin:

Page | 14
Experiment No 2 Student ID
Date Student Name

int buttonState = digitalRead(pushButton);


// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}

B. Turn ON an LED when a button is pressed, OFF when button is released.


// 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
int stateButton = digitalRead(buttonPin);

// Make a decision based on the button state


if (stateButton == HIGH) {
// Button is pressed, provide voltage to pin 8 (turn on the LED)
digitalWrite(ledPin, HIGH);
} else {
// Button is not pressed, do not output voltage on pin 8 (turn off the LED)
digitalWrite(ledPin, LOW);
}
}

Results:

Analysis and Inferences:

Page | 15
Experiment No 2 Student ID
Date Student Name

VIVA-VOCE Questions (In-Lab):


1. What is meant by button debouncing?
Push button is physically “bouncing” when it is touched (going from LOW to HIGH
and HIGH to LOW many times in a short interval). This might be due to some error in
the code.
2. How does push button work in Arduino?
When the button is closed (pressed), it makes a connection between its two legs,
connecting the pin to 5 volts, so that we read a HIGH and when the button is released,
the pin is connected to ground to read a LOW.
3. What is the push button to reset Arduino?
The RESET button is a white or blue push button located on top of your Arduino board.
Pressing it has the same effect as disconnecting and reconnecting the power supply.
4. Does push button need resistor?
The resistor is mandatory for proper operation of a button.
5. What are the applications of push button?
Push button switches are used in a wide range of applications, including computers,
crosswalks, telephones, industrial machinery, security systems, ATMs, military
equipment, casino gambling slot machines, fitness equipment, and gadgets.

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

int stateButton = digitalRead(buttonPin);


// Make a decision based on the button state
if (stateButton == HIGH) {
// Button is pressed, provide voltage to pin 8 (turn on the LED)
digitalWrite(ledPin, HIGH);
} else {
// Button is not pressed, do not output voltage on pin 8 (turn off the LED)
digitalWrite(ledPin, LOW);
}
delay(1000);
}
Results:

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

You might also like