Bread Crumb Link
header_left
Home Free_Shipping Contact_Us Designed_By
Categories
Cat_bot
 
 
Shop Pages
Cat_bot
 
Shop Newsletter

Add my Store to your
Favourites and receive my email newsletters about new items and
special promotions!

General Interest
Cat_bot
 
Fast_Shipping
PayPal

 

 

 
 
Raspberry Pi GPIO Introduction
 
General purpose input / ouput (GPIO)

General Purpose Input/Output (GPIO) is a generic pin on a chip whose behavior can be controlled by the user at run time.

Below is a picture of the GPIO pin header for the Raspberry Pi

GPIO Header

The GPIO connector has a number of different types of connection:

  • True GPIO (General Purpose Input Output) pins that you can use to turn LEDs on and off etc.
  • I2C interface pins that allow you to connect hardware modules with just two control pins
  • SPI interface with SPI devices, a similar concept to I2C but uses a different standard
  • Serial Rx and Tx pins for communication with serial peripherals

GPIO pins can be used as both digital outputs and digital inputs.

Output: Turn a particular pin HIGH or LOW.

  • Setting it HIGH sets it to 3.3V; setting it LOW sets it to 0V.

Input: Detect the pin being at HIGH or LOW

  • We can connect switches and simple sensors to a pin and check whether it is open or closed (that is, activated or not)

Below is pictures of the two Raspberry Pi P1 header for REV1 and REV2

Raspberry Pi Rev 1 Raspberry Pi Rev 2
Pinouts Rev1 Pinouts Rev2

 

Pins are 3.3V not 5V like on the Arduino, They are connected directly to the Broadcom chip Sending 5V to a pin may kill the Pi (Maximum permitted current draw from a 3.3V pin is 50mA)


 
GPIO Numbering on the raspberry pi

There are two differnet pin numbering systems that the RPI.GPIO module uses

  • GPIO.BCM (BCM chip numbering commonly used on the cobbler breakout pcbs)
  • GPIO.BOARD (P1 Pin Header numbering usefull when not working with cobbler breakout pcbs)

To use the pin numbers from a cobbler breakout pcb:

GPIO.setmode(GPIO.BCM)

To use the pin numbers on raspberry pi board:

GPIO.setmode(GPIO.BOARD)

 

 
GPIO as output

Experiment 1: Controlling LED

Breadboard

  • Build circuit easily without soldering

Pin numbering 1

Smart Elex Breakout kit

  • Use Smart Elex Breakout kit to extend the GPIO pins to the breadboard

Pin numbering 1

Light-emitting diode (LED)

Current flows from the anode to cathode.

  • Anode: longer pin
  • Cathode: shorter pin

Use a multimeter to test the polarity

  • Check resistance
  • In both directions.

Pin numbering 1

 

Simple LED Circuit

Pin numbering 1

 

Task 1: Turn LED on for 3 seconds and off for 1 second.

In this example, we use Diagram (b), i.e. controlling the LED by controlling the voltage at the anode (+).

in this task we will use GPIO17

Python Code for task 1

# Smart-Elex Tutorial 1
# Turn LED on for 3 secs then off
# Revision 1
# Note
# Lines with a # at the start are 
# comment lines python ignores these lines.

print "The LED should be ON for 3 secs."

#To import GPIO module
import RPi.GPIO as GPIO #Import time module
from time import sleep

#To disable GPIO warnings
GPIO.setwarnings(False)

# To change to BCM GPIO numbers
GPIO.setmode(GPIO.BCM)

# Set GPIO LED output Channels
GPIO.setup(17,GPIO.OUT) # Sets GPIO 17 to an output.

# Sets the GPIO outputs to high (LEDs on)
GPIO.output(17,GPIO.LOW) # Sets GPIO 17 to LOW (ON)

#Python sleep for 3 secs (pause)
sleep(3)

# Sets the GPIO outputs to high (LEDs OFF)
GPIO.output(17,GPIO.HIGH) # Sets GPIO 17 to HIGH (OFF) # End of Script
 

 

 
GPIO as input
  • When the switch is not pushed: GPIO detects Vcc (HIGH)
  • When the switch is pushed: GPIO detects GND (LOW)

Pin numbering 1

Task 2: Turn LED on when Switch is pressed.

In this example, we use the above diagram to turn the the led from task 1 on by using a switch.

in this task we will use GPIO23

Python Code for task 2

# Smart-Elex Tutorial 5 # turn led on with switch
# Revision 1
# Note
# Lines with a # at the start are #comment lines python ignores these lines.

print "Press switch and LED should come ON

#To import GPIO module
import RPi.GPIO as GPI

#Import time module
from time import sleep

#To disable GPIO warnings GPIO.setwarnings(False)

# To change to BCM GPIO numbers
GPIO.setmode(GPIO.BCM)

# Set GPIO LED output Channel GPIO.setup(17,GPIO.OUT) # Sets GPIO 17 to an output.

# Set GPIO switch input Channel GPIO.setup(23,GPIO.IN) # Sets GPIO 23 to an input.

while 1: if GPIO.input(23): # If tactile switch is pressed
GPIO.output(17,GPIO.LOW) # Sets GPIO 17 to LOW (ON) else:
GPIO.output(17,GPIO.HIGH) # Sets GPIO 17 to HIGH (OFF)

# End of Script

 

 

 
Experiment: Display digit on 7-segment LED

GPIO Header

Most direct way to control display:

  • Connect pin 1 or 8 of 7-seg-LED to Vcc
  • Connect the other 8 pins to 8 GPIO pins
  • Configure the 8 GPIO pins as outputs
  • Output LOW: turn on LED segment
  • Output HIGH: turn off LED segment

GPIO Header

For example: Display digit “2”

Turn on segments A, B, D, E, G and turn off segments C, F, DP

Set A,B,D,E,G to LOW

  • Set Pin 7, 6, 2, 1, 10 LOW

and set C, F, DP to HIGH

  • Set pin 4, 9, 5 HIGH

Good Luck