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