0% found this document useful (0 votes)
15 views9 pages

Experiment 4

Uploaded by

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

Experiment 4

Uploaded by

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

Exp-4

Interface Push button/Digital sensors


(IR/LDR) with Raspberry Pi, write a
program to turn ON LED when push
button is pressed or at sensor detection.
Components required
• Raspberry Pi 3 setup with monitor and USB
Mouse & Keyboard
– Checkout this guide if you need help
• One LED
• One 330 Ohm resistor
• Jumper wires
• Breadboard
• Push Button
Red LED

Jumper wire

Resistor

Breadboard

Push Button
Circuit Diagram
Steps for Circuit
• For hardware configuration of the above circuit diagram, we will
first connect the Raspberry Pi 4 and a LED on the breadboard:
• After this, we will connect the push-button(either of two legs or
four legs) on the breadboard:
• Then we will connect a cathode terminal of LED and any leg
terminal of push-button to the series short terminal of the
breadboard, and connect this short terminal of the breadboard
with the ground pin of Raspberry Pi 4:
• Connect the anode terminal of the LED with the GPIO pin 4 of
Raspberry Pi 4:
• In the last, connect the remaining second terminal of the push-
button with the GPIO 17 pin of the Raspberry Pi 4:
Python code
import RPi.GPIO as GPIO #import RPi.GPIO module
import time
LED = 40 #pin no. as per BOARD, GPIO18 as per BCM
button = 37
GPIO.setwarnings(False) #disable warnings
GPIO.setmode(GPIO.BOARD) #set pin numbering format
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED, GPIO.OUT)#set GPIO as output

while True:
button_state = GPIO.input(button)
if button_state == False:
GPIO.output(LED,GPIO.HIGH)
else:
time.sleep(1)
GPIO.output(LED,GPIO.LOW)
• from gpiozero import LED
#imports LED functions from gpiozero library
from gpiozero import Button
#imports Button functions from gpiozero library

led = LED(4)
#declare the GPIO pin 4 for LED output and store it in led variable
button = Button(17)
#declare the GPIO pin 17 for Button output and store it in button variable

while True:
#initiated an infinite while loop
button.wait_for_press()
#use the built-in function of the button to wait till press
led.on()
#turn on the led
button.wait_for_release()
#use the built-in function of button to wait till release
led.off()
#turn off the led
Import RPi.GPIO as GPIO
From time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13, GPIO.OUT) #Button
GPIO.setup(22, GPIO.OUT) #LED
State = 0
While True:
Input = GPIO.input(13)
If (input == False): #have to press button to work
If (state == 1): #this is on so led will start in off
GPIO.output(22, True)
State = 0
elif (state == 0): #led will start at this position which is off
GPIO.output(22, False)

State = 1
Sleep(1)
• Step 1 Insert LED into the Breadboard. ...
• Step 2 Insert a 220 ohm resistor. ...
• Step 3 Insert the button. ...
• Step 4 Connect pin 13 to the LED. ...
• Step 5 Connect the resistor to ground. ...
• Step 6 Connect the push button to pin 7. ...

You might also like