Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
1 of 13
1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
There are eight of these pins can be used directly for digital output and input (Hight and Low). These pins can be set high by connecting it to a voltage supply, or set low by connecting it to ground. So you can control electronics devices such as LEDs, Motor Driver and so on using these GPIO pins. Input devices like push buttons and toggle switches can also be used to control the Raspberry Pi.
2 of 13
1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
If you dont get any error, you are should be fine. If you do get an error, then do the following to install the library. To install it launch a command line (i.e. LXTerminal) and enter the following commands : 1. Download the RPi GPIO Library
wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.3.1a.tar.gz
3 of 13
1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... 4 5 # to use Raspberry Pi board pin numbers 6 7 GPIO.setmode(GPIO.BOARD) # or GPIO.setmode(GPIO.BCM) 8 9 # set up the GPIO Pins - for input or output 10 11 GPIO.setup(11, GPIO.IN) 12 13 GPIO.setup(13, GPIO.OUT) 14 15 # taking input value from Pin 11 16 input_value = GPIO.input(11) 17 18 # setting output value to Pin 13 19 GPIO.output(13, GPIO.HIGH) 20 21 #GPIO.output(13, GPIO.LOW)
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
The difference (http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=34273) between GPIO.setmode(GPIO.BOARD) and GPIO.setmode(GPIO.BCM) is the pin numbering system. BOARD signifies using the physical pin numbers on the Raspberry Pi P1 connector. BCM signifies the Broadcom SOC channel designation. However you should know the BCM channels changed a little between revision 1 and revision 2 of the Raspberry Pi board, and the BOARD numbering system stays working between board revisions.
4 of 13
1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
Resistors value can be caculated as this. My 5mm LEDs forward current is around 20mA (might be different to yours), voltage supply from RPi is 3.3V, so the resistor for LED is 3.3 V / 20 mA = 165 omh. For the push buttons, I used 1K ohm resistors.
5 of 13 1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(16, GPIO.IN) GPIO.setup(18, GPIO.IN) GPIO.setup(11, GPIO.OUT) GPIO.setup(13, GPIO.OUT) GPIO.setup(15, GPIO.OUT) GPIO.output(11, GPIO.LOW) GPIO.output(13, GPIO.LOW) GPIO.output(15, GPIO.LOW) # state - decides what LED should be on and off state = 0 # increment - the direction of states inc = 1 while True: # state toggle button is pressed if ( GPIO.input(16) == True ): if (inc == 1): state = state + 1; else: state = state - 1; # reached the max state, time to go back (decrement) if (state == 3): inc = 0 # reached the min state, go back up (increment) elif (state == 0): inc = 1
6 of 13
1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... 38 39 if (state == 1): 40 GPIO.output(11, GPIO.HIGH) 41 GPIO.output(13, GPIO.LOW) 42 GPIO.output(15, GPIO.LOW) 43 elif (state == 2): 44 GPIO.output(11, GPIO.HIGH) 45 GPIO.output(13, GPIO.HIGH) 46 GPIO.output(15, GPIO.LOW) 47 elif (state == 3): 48 GPIO.output(11, GPIO.HIGH) 49 GPIO.output(13, GPIO.HIGH) 50 GPIO.output(15, GPIO.HIGH) 51 else: 52 GPIO.output(11, GPIO.LOW) 53 GPIO.output(13, GPIO.LOW) 54 GPIO.output(15, GPIO.LOW) 55 print("pressed B1 ", state) 56 57 # reset button is pressed 58 if ( GPIO.input(18) == True ): 59 60 state = 0 61 inc = 1 62 GPIO.output(11, GPIO.LOW) 63 GPIO.output(13, GPIO.LOW) 64 GPIO.output(15, GPIO.LOW) 65 66 print("pressed B2 ", state) 67 68 sleep(0.2);
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
Conclusion
Although the Raspberry Pi has much more powerful computational capacity than the Arduino, its shortage in digital pins and analogue pins is still a disadvantage for many DIY, electronics hobbyists. However there are possibilities in expanding the number of digital pins and adding analogue pins. I might do some projects around these area in the near future.
If you find this article useful. To help us maintain and improve this website.
Related Articles:
7 of 13
1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
Connect Raspberry Pi
connected-serialgpio/)
arduinousb-cable/)
connected-i2c/)
opencv-pid/)
12 thoughts on How To Use GPIO Pins On Raspberry Pi Buttons And LED Tutorial
Ryat says:
September 5, 2013 at 10:28 pm (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-610)
Great read =D
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=610#respond)
knipskopp says:
September 18, 2013 at 4:18 pm (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-653)
Hi, i extended your button layout, adding gpio 17 as input and dropped all led wireing. 3 button work like a charm. however, if i try adding a fourth button via gpio25 it randomly flickers to high, without pressing a button. any suggestions appreciated. k.
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=653#respond)
8 of 13
1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
is your RPI rev 1 or 2? the Pin layout diagram on in this post is Rev2, make sure the pin are correctly initialized as Input or output. (in our case, should all be inputs) NOTE: pin numbers are different from GPIO numbers! if that doesnt help , please post your code here so we can check it..
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=654#respond)
knipskopp says:
September 19, 2013 at 7:58 am (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-658)
Yes, its PI Rev 2 B with LAN + USB. Prev, Next + Toggle work, if i attach another button, it flickers. Code: from time import sleep import subprocess import RPi.GPIO as GPIO GPIO.cleanup() GPIO.setmode(GPIO.BOARD) GPIO.setup(16, GPIO.IN) GPIO.setup(15, GPIO.IN) GPIO.setup(18, GPIO.IN) GPIO.setup(22, GPIO.IN) def next(): p = subprocess.Popen(["mpc", "next"], stdout=subprocess.PIPE) output, err = p.communicate() print *** Running mpc next command ***\n # , output def prev(): p = subprocess.Popen(["mpc", "prev"], stdout=subprocess.PIPE) output, err = p.communicate() print *** Running mpc prev command ***\n# , output def toggle(): p = subprocess.Popen(["mpc", "toggle"], stdout=subprocess.PIPE) output, err = p.communicate() print *** Running mpc toggle command ***\n, output
9 of 13 1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
def mpc(doit): p = subprocess.Popen(["mpc", doit], stdout=subprocess.PIPE) output, err = p.communicate() print *** Running mpc toggle command ***\n, output while True: # next button is pressed if ( GPIO.input(15) == True ): print(pressed GPIO 22 Pin 15 B3, state) next() mpc(current) # prev toggle button is pressed if ( GPIO.input(16) == True ): print(pressed GPIO 23 Pin 16 B1, state) prev() mpc(current) # toggle button is pressed if ( GPIO.input(18) == True ): print(pressed GPIO 24 PIN 18 B2 , state) toggle() # button4 is pressed if ( GPIO.input(22) == True ): print(pressed GPIO 25 PIN 22 B4 , state) sleep(0.2);
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=658#respond)
okay the code looks okay to me (although I havent tested it on my RPi). I would suggest debug the buttons first, and remove all the sub-routines. You can do this but having 4 button states variables (state1 state4). 1. initialize all states to false . 2. when button pressed, check corresponding button state boolean value, if its false, now set it to true. Or vice verse. Try this first
10 of 13 1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=662#respond)
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
knipskopp says:
September 19, 2013 at 8:33 am (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-659)
I also tried Pin 11 ( GPIO 17) instead of PIN 22 GPIO25. Same Oo above one line is missing: state = 0 Sorry, Copy+Paste Error
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=659#respond)
knipskopp says:
September 19, 2013 at 9:05 am (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-661)
Made a Photo with 3 Buttons : (working) http://www.img-host.de/bild.php/42531,4buttons5LFI4.png (http://www.img-host.de/bild.php /42531,4buttons5LFI4.png) And a Photo with 4 Buttons: ( Button 4 flickers) http://imgur.com/TavVZUt (http://imgur.com/TavVZUt) sorry for 3 Comments.
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=661#respond)
oh.. once you mention flickers here it hits me It might be the sampling frequency is too high for the buttons ( which is normal), you might also need add more logic to check if the button has been released and pressed again, and not pressed all the time (just ignore it if thats the case), otherwise the button is not going to work very well.
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=663#respond)
knipskopp says:
September 19, 2013 at 12:11 pm (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-664)
11 of 13
1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
i did a stupid mistake. See Picture 2? The Breadboard is seperated Ground + 3.3v dont go to the left side .. once i plugged button 4 (and 5 ) on the right side, everything worked
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=664#respond)
Aha!
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=666#respond)
Jeremy says:
September 25, 2013 at 3:05 pm (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-687)
Hi I have followed your example but i get the following error: ledpy.py, line 39 if (inc == 1): ^ My code looks like this. while True: # state toggle button is pressed if ( GPIO.input(7) == True ): if (inc == 1): state = state + 1; else: state = state 1; Please help !
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=687#respond)
Jeremy says:
12 of 13 1/23/2014 12:25 PM
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...
http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
Sorry i did add the indents in the code i pasted but the blog seems to have removed them.
Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=688#respond)
Leave a Reply
13 of 13
1/23/2014 12:25 PM