(My code technically doesn't handle SIGQUIT properly, since it doesn't dump core, but I've never seen people using it properly.)įinally, it also uses try/except to catch Ctrl C, just to play it safe.In this tutorial you’ll write your first Python program on your Raspberry Pi. * SIGQUIT is sent by Ctrl \, which asks the program to quit and dump core if core dumping is enabled. * SIGHUP is sent by the kernel when your program loses the terminal it's attached to. * SIGTERM is sent by things like task managers politely asking your program to TERMinate itself. * SIGINT is sent by Ctrl C, which INTerrupts the program. If you specifically want to catch Ctrl C but nothing else, you can use try/except to catch KeyboardInterrupt.įinally, if you want a more robust way to ensure some cleanup gets done, take a look at lines 312 through 322 in this revision of my Procrastinator's Timeclock utility.įirst, it assigns the cleanup function to sys.exitfunc so it'll get called on clean exit, then it hooks all the common POSIX signals the kernel might send s ensure that they trigger a clean exit. The simplest way to ensure some cleanup code gets run is to wrap your while loop in a try/finally block. I hope this was helpful For more Raspberry Pi projects and other programmatic artworks, you can find me here: or Scott Kildall You can test your LED and the GPIO connection by connecting the LED to the 3.3V output of the GPIO and to the 270 Ohm resistor and to the ground of the GPIO. Longer lead is oriented to the positive electron flow. Make sure your LED is facing the right direction. Make sure you have the right components in the right places. Here are some suggestions for things to look for: - If you're getting no flashing LED or the switch doesn't seem to work, check your breadboard connections or the switch doesn't work, check your breadboard connections. Troubleshooting There's a lot that can go awry with electronics and interfacing with the outside world. Adafruit, as always has some excellent material on the GPIO and Raspberry Pi. Here is the GitHub repository for some of the GPIO scripts that I reference, and am adding to. The time.sleep(0.05) is there to account for any debouncing in the button. We set the output of Pin 4 to match the input of Pin 22. how it works This is like the previous script, except that we are designating Pin 22 as an input pin. Congratulations, you have an input and output into your Raspberry Pi. Sudo python gpio_switch.py If you press the switch, the LED should turn on and when you let it go, it should turn off. # gpio_swtich.py # by Scott Kildall (# LED is on pin 4, use a 270 Ohm reistor to ground # Switch is on pin 22, use a pull-down resistor (10K) to ground import RPi.GPIO as GPIO import time twarnings(False) tmode(GPIO.BCM) tup(4, GPIO.OUT) tup(22,GPIO.IN) # input of the switch will change the state of the LED while True: GPIO.output(4,GPIO.input(22)) time.sleep(0.05) Cntl-X, Y to save the file (note the indentations af Now run the script: Sudo nano gpio_blink.py You can also refer to the GitHub repository, if need be. Now, we'll try adding a switch, using the same circuit. Setting the mode to BCM means that the pin numbers etched on the Raspberry Cobbler match the ones that you are using in your code. I turn warnings off because I was getting errors in my script because the GPIO wasn't properly closed (this shouldn't matter and I found it an annoyance). We alternate between high (True) and low (False) for 1 second at a time. Ctrl-C to exit the script how it works - Pin 4 is an input pin. We're not using the switch at all at this point. You should have a blinking LED on your circuit. Sudo python gpio_blink.py Note: you have to invoke sudo - root access for the GPIO library. # gpio_blink.py # by Scott Kildall (# LED is on pin 4, use a 270 Ohm resistor to ground import RPi.GPIO as GPIO import time twarnings(False) tmode(GPIO.BCM) tup(4, GPIO.OUT) state = True # endless loop, on/off for 1 second while True: GPIO.output(4,True) time.sleep(1) GPIO.output(4,False) time.sleep(1) Ctrl-X, Y to save the file. Alternatively, I have this on a GitHub repository. The advantage with using ssh is that you can just copy-and-paste the script. Sudo nano gpio_blink.py And enter in this script. On the command line, create a new python script by invoking nano. At this point, I run the Raspberry Pi from my monitor with a keyboard plugged in - I usually do the package updates from ssh.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |