# Project objective: Light up an external LED when a push button is pressed
#
# Hardware and connections used:
# Push button to GPIO Pin 16
# 10000 ohm pull-down resistor for the push button
# LED to GPIO Pin 15
# 220 ohm resistor for LED
#
# Programmer: Adrian Josele G. Quional
# modules
# from machine import Pin
# from time import sleep
# LED = Pin(15, Pin.OUT) # creating LED object, setting it as OUT
# BUTTON = Pin(16, Pin.IN) # creating Push Button object, setting it as IN
# # continuously read the signal from the push button while the board has power
# while True:
# # if the signal from the button is HIGH (button is pressed), turn LED on
# if BUTTON.value() == 1:
# LED.on()
# sleep(0.1)
# print("test")
# # otherwise, turn the LED off
# else:
# LED.off()
import RPi.GPIO as GPIO
import time
# Set GPIO mode
GPIO.setmode(GPIO.BCM)
# Define GPIO pins for LED and switch
LED_PIN = 15
SWITCH_PIN = 16
# Setup LED pin as output and switch pin as input
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while True:
# Check if switch is pressed
if GPIO.input(SWITCH_PIN) == GPIO.LOW:
# Turn on LED
GPIO.output(LED_PIN, GPIO.HIGH)
else:
# Turn off LED
GPIO.output(LED_PIN, GPIO.LOW)
finally:
# Clean up GPIO
GPIO.cleanup()