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-radio- arduino/ ) DIY Wireless RC Remote Controller for Robots, Quadcopter → ( http://blog.oscarliang.net/diy-wireless-rc-remote-controller- for-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 2×13 header pins which can perform tasks include SPI, I2C, serial UART, 3V3 and 5V power. How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/ 1 of 13 1/23/2014 12:25 PM
13
Embed
How to Use GPIO Pins on Raspberry Pi - Buttons and LED Tutorial - OscarLiang
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
How To Use GPIO Pins OnRaspberry Pi – Buttons And LEDTutorialPosted 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 2×13 header pins which can
perform tasks include SPI, I2C, serial UART, 3V3 and 5V power.
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
1 of 13 1/23/2014 12:25 PM
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.
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
2 of 13 1/23/2014 12:25 PM
How To Control and Use GPIO Pins onRaspberry 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:
and type in this line
If you don’t 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 :
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
3 of 13 1/23/2014 12:25 PM
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 withRaspberry Pi GPIO
4 5 # to use Raspberry Pi board pin numbers6 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 1116 input_value = GPIO.input(11)17 18 # setting output value to Pin 1319 GPIO.output(13, GPIO.HIGH)20 21 #GPIO.output(13, GPIO.LOW)
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
4 of 13 1/23/2014 12:25 PM
Connection of GPIO Pins On Raspberry Pi andLEDs/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 LED’s 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.
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
5 of 13 1/23/2014 12:25 PM
Source Code
1 from time import sleep2 import RPi.GPIO as GPIO3 4 GPIO.setmode(GPIO.BOARD)5 6 GPIO.setup(16, GPIO.IN)7 GPIO.setup(18, GPIO.IN)8 9 GPIO.setup(11, GPIO.OUT)
10 GPIO.setup(13, GPIO.OUT)11 GPIO.setup(15, GPIO.OUT)12 GPIO.output(11, GPIO.LOW)13 GPIO.output(13, GPIO.LOW)14 GPIO.output(15, GPIO.LOW)15 16 # state - decides what LED should be on and off17 state = 018 19 # increment - the direction of states20 inc = 121 22 while True:23 24 # state toggle button is pressed25 if ( GPIO.input(16) == True ):26 27 if (inc == 1):28 state = state + 1;29 else:30 state = state - 1;31 32 # reached the max state, time to go back (decrement)33 if (state == 3):34 inc = 035 # reached the min state, go back up (increment)36 elif (state == 0):37 inc = 1
How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/
6 of 13 1/23/2014 12:25 PM
Conclusion
Although the Raspberry Pi has much more powerful computational capacity than the
Arduino, it’s 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