pythonpraexam
pythonpraexam
Write a Python script to allow a user to turn the LED on and off.
# Assuming Raspberry Pi with GPIO
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while True:
cmd = input("Enter 'on' to turn on LED, 'off' to turn off,
'exit' to quit: ")
if cmd == 'on':
GPIO.output(18, True)
elif cmd == 'off':
GPIO.output(18, False)
elif cmd == 'exit':
break
GPIO.cleanup()