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

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

GPIO Pins On Raspberry Pi and LEDs

What Are The GPIO Pins on Raspberry Pi?

A great feature on the Raspberry Pi is the GPIO pins (stands for General PurposeInput 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.

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.

 

arduino-raspberry-pi-serial-gpio

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 on your Raspbian OS, to verify this, fire up Python:

sudo python

and type in this line

import RPi.GPIO as GPIO

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 :

  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 # import library
2
3 import RPi.GPIO as GPIO
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)

The difference between GPIO.setmode(GPIO.BOARD) andGPIO.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

Connection of GPIO Pins On Raspberry Pi and LEDs/buttons

There are 8 available GPIO Pins on Raspberry Pi.

raspberry  pi gpio pins

See connection shown in the diagram.

GPIO Pins On Raspberry Pi circuit connection

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.

IMAG0721

Source Code

1 from time import sleep
2 import RPi.GPIO as GPIO
3
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 off
17 state = 0
18
19 # increment - the direction of states
20 inc = 1
21
22 while True:
23
24     # state toggle button is pressed