Sei sulla pagina 1di 13

How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ...

http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

Log in (http://blog.oscarliang.net/wp-login.php) Register (http://blog.oscarliang.net/wp-login.php?action=register)


Xbee Alternative XRF Wireless RF Radio Module And Arduino (http://blog.oscarliang.net/xbee-alternative-xrf-wireless-rf-radioarduino/) DIY Wireless RC Remote Controller for Robots, Quadcopter (http://blog.oscarliang.net/diy-wireless-rc-remote-controllerfor-robots/)

How To Use GPIO Pins On Raspberry Pi Buttons And LED Tutorial


Posted on July 13, 2013 (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/) by Oscar (http://blog.oscarliang.net/author/oscar/)

What Are The GPIO Pins on Raspberry Pi?


A great feature on the Raspberry Pi is the GPIO pins (stands for General Purpose Input Output). These GPIO pins on Raspberry Pi can be found in 213 header pins which can perform tasks include SPI, I2C, serial UART, 3V3 and 5V power.

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/

How To Control and Use GPIO Pins on Raspberry Pi?


This includes two steps, software on the Pi, and how to connect the hardware.

Install RPi.GPIO Python Library


The RPi.GPIO Python Library probably have come pre-installed (http://www.raspberrypi.org/phpBB3/viewtopic.php?t=40312&p=334386) on your Raspbian OS, to verify this, fire up Python:
sudo python

and type in this line


import RPi.GPIO as GPIO

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

2. Extract the files


tar zxf RPi.GPIO-0.3.1a.tar.gz

3. Browse to the extracted folder


cd RPi.GPIO-0.3.1a

4. Install the library


sudo python setup.py install

The Library should be ready now.

Usage of the Python RPi.GPIO Library


1 2 3 # import library import RPi.GPIO as GPIO

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.

A Simple LEDs and Push Button Test with Raspberry Pi GPIO

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/

Connection of GPIO Pins On Raspberry Pi and LEDs/buttons


There are 8 available GPIO Pins on Raspberry Pi.

See connection shown in the diagram.

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/

Raspberry Pi and Arduino

Connect Raspberry Pi

Raspberry Pi and Arduino

Raspberry Pi Color Tracking

(http://blog.oscarliang.net (http://blog.oscarliang.net (http://blog.oscarliang.net (http://blog.oscarliang.net


This entry was posted in /connectFeatured (http://blog.oscarliang.net/category/featured/), Raspberry Pi /raspberry-pi-and/raspberry/raspberry(http://blog.oscarliang.net/category/raspberry-pi-2/) and tagged raspberry pi (http://blog.oscarliang.net arduinoraspberry-pi-andpi-arduinopi-color-tracking/tag/raspberry-pi/). Bookmark the permalink (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/).

connected-serialgpio/)

arduinousb-cable/)

connected-i2c/)

opencv-pid/)

Xbee Alternative XRF Wireless RF Radio Module And Arduino (http://blog.oscarliang.net/xbee-alternative-xrf-wireless-rf-radioarduino/)

DIY Wireless RC Remote Controller for Robots, Quadcopter (http://blog.oscarliang.net/diy-wireless-rc-remote-controllerfor-robots/)

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/

Oscar (http://OscarLiang.net) says:


September 18, 2013 at 5:02 pm (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-654)

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)

Oscar (http://OscarLiang.net) says:


September 19, 2013 at 10:02 am (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-662)

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)

Oscar (http://OscarLiang.net) says:


September 19, 2013 at 10:12 am (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-663)

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)

Oscar (http://OscarLiang.net) says:


September 19, 2013 at 5:32 pm (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-666)

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/

September 25, 2013 at 3:07 pm (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/#comment-688)

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

Potrebbero piacerti anche