Question

Questions 3 Write a Python program that implements the following functionality: 1. Makes an LED blink. 2. Then, on a push of

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Python program to make LED blink

import RPi.GPIO as GPIO    # Import Raspberry Pi GPIO library
from time import sleep     # Import the sleep function from the time module


GPIO.setmode(GPIO.BOARD)    # Use physical pin numbering

led = 18     #Physical Pin 18 (GPIO24) and LED is connected to GPIO24

# Set pin 18 to be an output pin                                    
# it also sets initial value to low (off)
GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)

while True:
 GPIO.output(18, GPIO.HIGH)         # Turn on
 sleep(2)                          # Sleep for 2 seconds
 
GPIO.output(18, GPIO.LOW)          # Turn off
sleep(2)                          # Sleep for 2 seconds

2 and 3 : Python program to make LED on and off using push button

Upto here the LED is blinking with a delay of 2 seconds.

#LED is already connected to GPIO24 of Raspberry Pi.

# Connect the push button to Raspberry Pi GPIO on GPIO23.

# pin which is connected to push-button will be an input pin as it will read the data from the button.

# pin connected to LED is an output pin

import RPi.GPIO as GPIO

import time

button = 16
led = 18

def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led, GPIO.OUT)

def loop():
while True:
button_state = GPIO.input(button)
if button_state == False:
GPIO.output(led, True)
print('Button Pressed...')
while GPIO.input(button) == False:
time.sleep(0.2)
else:
GPIO.output(led, False)

def endprogram():
GPIO.output(led, False)
GPIO.cleanup()


if __name__ == '__main__':
  
setup()
  
try:
loop()
  
except KeyboardInterrupt:
print 'keyboard interrupt detected'
endprogram()

Add a comment
Know the answer?
Add Answer to:
Questions 3 Write a Python program that implements the following functionality: 1. Makes an LED blink....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with doing these tasks for code composer Lab 3 - Branching, Push Button...

    I need help with doing these tasks for code composer Lab 3 - Branching, Push Button and LEDs-Reading Assignment in this lab, we are going to control a LED via a push button- using general purpose digital 10 for both input (button) and output (LED) on port 1 - using Code Composer Studio. Furthermore, we are going to use a branch instruction to create an IF-ELSE structure in assembly to determine if the LED should be lit up based on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT