[go: up one dir, main page]

0% found this document useful (0 votes)
5 views3 pages

Coding Practice

The document contains two parts of Python code for Raspberry Pi GPIO control. Part A demonstrates how to flash an LED on and off, while Part B controls three LEDs, a buzzer, and a push button, activating the buzzer and LEDs in sequence when the button is pressed. Both parts include exception handling for cleanup on keyboard interrupt.

Uploaded by

Neokiiey Bokies
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)
5 views3 pages

Coding Practice

The document contains two parts of Python code for Raspberry Pi GPIO control. Part A demonstrates how to flash an LED on and off, while Part B controls three LEDs, a buzzer, and a push button, activating the buzzer and LEDs in sequence when the button is pressed. Both parts include exception handling for cleanup on keyboard interrupt.

Uploaded by

Neokiiey Bokies
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/ 3

PART A

1. Flashing LED

import RPi.GPIO as GPIO

import time

LED_PIN = 17

GPIO.setmode(GPIO.BCM)

GPIO.setup(LED_PIN, GPIO.OUT)

try:

while True:

GPIO.output(LED_PIN, GPIO.HIGH)

time.sleep(1)

GPIO.output(LED_PIN, GPIO.LOW)

time.sleep(1)

except KeyboardInterrupt:

GPIO.cleanup()

PART B

2. Three LED’s, buzzer and push button.

import RPi.GPIO as GPIO

import time

RED = 18

YELLOW = 23

GREEN = 24

BUZZER = 12

BUTTON = 25

GPIO.setmode(GPIO.BCM)
GPIO.setup(RED, GPIO.OUT)

GPIO.setup(YELLOW, GPIO.OUT)

GPIO.setup(GREEN, GPIO.OUT)

GPIO.setup(BUZZER, GPIO.OUT)

GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def reset_leds():

GPIO.output(RED, GPIO.LOW)

GPIO.output(YELLOW, GPIO.LOW)

GPIO.output(GREEN, GPIO.LOW)

try:

while True:

if GPIO.input(BUTTON) == GPIO.HIGH:

GPIO.output(RED, GPIO.HIGH)

GPIO.output(BUZZER, GPIO.HIGH)

time.sleep(0.2)

GPIO.output(BUZZER, GPIO.LOW)

time.sleep(1)

GPIO.output(YELLOW, GPIO.HIGH)

time.sleep(1)

GPIO.output(GREEN, GPIO.HIGH)

time.sleep(1)

reset_leds()

time.sleep(0.1)

except KeyboardInterrupt:
GPIO.cleanup()

You might also like