Strictly speaking, ground is 0V not a -ve voltage (although it does correspond to the -ve terminal on a battery). This is what the above program’s output looks like… Print "Port 25 is 0/GPIO.LOW/False - button not pressed" Print "Port 25 is 1/GPIO.HIGH/True - button pressed" While True: # this will carry on until you hit CTRL+C #GPI SS4 NO FLYCUT CODE#Here’s the code with all the extras added to read and display the button’s status, and exit cleanly when CTRL+C is pressed…įrom time import sleep # this lets us have a time delay (see line 12) If you don’t have a breadboard, resistors and buttons, you could cheat and just use a jumper wire to connect two pins directly, but you’ll need to be aware of what you’re doing. We’ll cover a bit more on floating ports, pull-ups and pull-downs in another article (probably part 6). It’s not dangerous, (it could be if you had something like a motor attached) but it’s not fully in control either. If you don’t use the resistors, it might still work, but you risk having a “floating” port. When the button is pressed, GPIO25 is connected to 3.3V. The resistors are used to “pull down” the GPIO25 pin to 0 Volts GND (0V, 0, LOW, False) unless the button is pressed. In order to go any further with this, we need to make up a little circuit.Ĭircuit with button for manipulating GPIO25 (click to enlarge) a try: except KeyboardInterrupt: block so we can exit cleanly ( we covered this yesterday).a circuit with a button, so we can change the status of the port and see the input status change on the screen.a time delay, so it won’t read the port thousands of times per second.a loop, so we can make it read more than once.The above is not really complete yet, what we need to do is add… So, building on what we’ve already done, here’s a very simple program to read and display the status of port 25, but it only does it once, then it cleans up and exits. …or store its value in a variable to use in a different part of the program… You can read the value of a port with this code…īut it may be more useful to use it as part of your logic… Inputs are Boolean values: 1 or 0, GPIO.HIGH or GPIO.LOW, True or False (this corresponds to the voltage on the port: 0V=0 or 3.3V=1). Next we need to be able to “read” the input. tmode(GPIO.BCM) # set up BCM GPIO numbering I’m going to use the BCM GPIO numbering and port GPIO25, so it becomes… …changing Port_or_pin to the number of the GPIO port or pin you want to use. So, with a drumroll and a fanfare of trumpets, it’s now time for us to set up some inputs. # but you can't have both, so only use one!!! So, at the top of every script, after importing the RPi.GPIO module, we set our GPIO numbering mode. It’s also important to be aware of the other system in case you ever need to work on someone elses code. The important thing is to pick the one that makes sense to you and use it. If you want to control the LED on a Pi camera board (GPIO5) you also have to use GPIO numbering. If you want to use the P5 header for GPIO28-31, you have to use GPIO numbering. If you use GPIO numbers, your scripts will make better sense if you use a Gertboard, which also uses GPIO numbering. You still need to be aware of which pins you can and can’t use though, since some are power and GND. If you use pin numbers, you don’t have to bother about revision checking, as RPi.GPIO takes care of that for you. I habitually use the GPIO numbers, but neither way is wrong. In RPi.GPIO you can use either pin numbers (BOARD) or the Broadcom GPIO numbers (BCM), but you can only use one system in each program. #GPI SS4 NO FLYCUT HOW TO#How to set up BOARD and GPIO numbering schemes orange – SPI (Serial Peripheral Interface).If you need 8 or less ports, it’s best to use the yellow ones because you’re less likely to have a conflict with other things. The rest can all be used as GPIO ports, but do have other functions too. yellow ones are all dedicated general purpose I/O ports (OK, 18 does PWM as well, but forget that for now).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |