how to use gpio pins on raspberry pi - buttons and led tutorial - oscarliang

13
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

Upload: setideba

Post on 22-Oct-2015

90 views

Category:

Documents


3 download

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

(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 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 :

Download the RPi GPIO Library1.

Extract the files2.

Browse to the extracted folder3.

Install the library4.

The Library should be ready now.

Usage of the Python RPi.GPIO Library

sudo python

import RPi.GPIO as GPIO

wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.3.1a.tar.gz

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

cd RPi.GPIO-0.3.1a

sudo python setup.py install

1 # import library2 3 import RPi.GPIO as GPIO

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

future.

If you find this article useful.

To help us maintain and improve this website.

Related Articles:

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 pressed58 if ( GPIO.input(18) == True ):59 60 state = 061 inc = 162 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);

How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

7 of 13 1/23/2014 12:25 PM

← 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/)

12 thoughts on “How To Use GPIO Pins On Raspberry Pi –Buttons And LED Tutorial”

Raspberry Pi

and Arduino

(http://blog.oscarliang.net

/raspberry-pi-and-

arduino-

connected-serial-

gpio/)

Connect

Raspberry Pi

(http://blog.oscarliang.net

/connect-

raspberry-pi-and-

arduino-

usb-cable/)

Raspberry Pi

and Arduino

(http://blog.oscarliang.net

/raspberry-

pi-arduino-

connected-i2c/)

Raspberry Pi

Color Tracking

(http://blog.oscarliang.net

/raspberry-

pi-color-tracking-

opencv-pid/)

This entry was posted in Featured (http://blog.oscarliang.net/category/featured/), Raspberry Pi

(http://blog.oscarliang.net/category/raspberry-pi-2/) and tagged raspberry pi (http://blog.oscarliang.net

/tag/raspberry-pi/). Bookmark the permalink (http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/).

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)

How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

8 of 13 1/23/2014 12:25 PM

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 doesn’t 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, it’s 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

How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

9 of 13 1/23/2014 12:25 PM

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 haven’t 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 it’s false, now set it to true.

Or vice verse.

Try this first

How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

10 of 13 1/23/2014 12:25 PM

Reply (/use-gpio-pins-on-raspberry-pi/?replytocom=662#respond)

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 that’s 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)

How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

11 of 13 1/23/2014 12:25 PM

i did a stupid mistake.

See Picture 2?

The Breadboard is seperated … Ground + 3.3v don’t 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:

How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

12 of 13 1/23/2014 12:25 PM

Leave a Reply

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)

How To Use GPIO Pins On Raspberry Pi - Buttons And LED Tutorial - ... http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

13 of 13 1/23/2014 12:25 PM