While I am waiting for answers about starting Python interpreter in privilege mode mentioned in my previous blog Step by Step Build Trick or Trivia Halloween Candy Dispenser #3, I'd like to continue my journey. This is going to be a short blog about how I work on LED blink.
I wrote a small piece code LED_Blink_Test.py which blinks LED(toggling red/green LEDs every second) until I hit the enter key. It works as expected.
import RPi.GPIO as GPIO import time import sys import select GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(26, GPIO.OUT) GPIO.setup(19, GPIO.OUT) state = True def toggle_leds(): global state if state: GPIO.output(26, True) GPIO.output(19, False) state = False else: GPIO.output(26, False) GPIO.output(19, True) state = True # endless loop until enter key is stroked, green & red LEDs alternately on/off for 1 second while True: while sys.stdin in select.select([sys.stdin], [], [], 0)[0]: line = sys.stdin.readline() if line: GPIO.cleanup() exit(0) else: toggle_leds() time.sleep(1)
I used the same pins to drive LEDs as Charles Gantt used in his blog, but I didn't directly connect LEDs to those pins. The reason for it because each pin will consume more than 25mA if they are directly driven by pins. I am not very comfortable to pull such a big current from an I/O pin unless I see it's specified in its datasheet. Some kind of current limit is required. I don't have appropriate resistors to limit the current to 5 to 10mA per pin, however, the kit includes a few diodes, so I put two diodes in serie to limit the current to about 3mA. The LED isn't super bright, but definitely visible when it lights up.
Make sure you run Python in privilege sudo python LED_Blink_Test.py. Otherwise, you will have run-time problem.
To check the GUI interface, I have to comment out all GPIO related statements. Then run python TrickorTriviaQuiz.py and GUI shows up like this:
Stay tune for the next blog.