4. gpio access

24
Pi Maker Workshop Powered by: http://www.inventrom.com/ http://www.robotechlabs.com/ Mayank Joneja botmayank.wordpress.com [email protected] 4.GPIO Access

Upload: mayank-joneja

Post on 13-May-2015

469 views

Category:

Engineering


6 download

DESCRIPTION

This is a part of the slide set used at the MakerSpace Noida (India) launch event, Pi Maker Workshop. This slide set is designed to help people get started with the Raspberry Pi and also serves as a collection of innovative projects and some core basic concepts that can aid anybody with their first few steps into the world of DIY electronics or maybe serve as a refresher for the experienced. Feel free to refer and share but please don't alter the watermarks :)

TRANSCRIPT

Page 1: 4. GPIO Access

Pi Maker

Workshop

Powered by:

http://www.inventrom.com/http://www.robotechlabs.com/

Mayank Jonejabotmayank.wordpress.com

[email protected]

4.GPIO Access

Page 2: 4. GPIO Access

Mayank Joneja

Warning!

While the GPIO pins can provide lots of useful control and sensing ability to the Raspberry Pi, it is important to remember they are wired directly into the internal core of the system.

This means that they provide a very easy way to introduce bad voltages and currents into the delicate heart of the Raspberry Pi (this is not good and means it is easy to break it without exercising a little care).

http://elinux.org/RPi_Tutorial_EGHS:GPIO_Protection_Circuits

http://www.rhydolabz.com/index.php?main_page=product_info&cPath=80&products_id=1045

Page 3: 4. GPIO Access

Mayank Joneja

Things we need to protect:

1) Drawing excess current from the pins (or short-circuiting an output)

2) Driving over-voltage on an input pin (anything above 3.3V should be

avoided). The PI has protection diodes between the pin and 3V3 and GND,

negative voltages are shorted to GND, but positive voltages greater than

3V3 + one "diode drop" (normally 0.5V) will be shorted to 5V, this means that

if you put a 5V power supply on the GPIO pin you will "feed" the 3V3 supply

with 4.5 Volt (5V - the diode drop) and that may damage 3V3 logic if the 5V

source succeeds in lifting the PI's 3V3 supply to an unsafe value. Note that if

you limit the current (for example with a 10K resistor) the small amount of

current flowing into the 3V3 supply will probably do no harm.

3) Static shocks, from touching pins without suitable grounding (often called

ESD - ElectroStatic Discharge, occurs when your clothes etc build up an

electrical charge as you move around)

All of these can potentially break your Raspberry Pi, damage the GPIO

circuits or weaken it over time (reducing its overall life).

Page 4: 4. GPIO Access

Mayank Joneja

GPIO in Python

The RPi.GPIO python module offers easy access to the GPIO

The GPIO module comes pre-installed in Raspbian.

However, in the rare case that you end up flashing a really old image on your SD card, I

guess you’ll need to install/download the RPi.GPIO module

sudo apt-get update

sudo apt-get install python-dev

sudo apt-get install python-rpi.gpio

Page 5: 4. GPIO Access

Mayank Joneja

BCM or BOARD

There are 2 ways of numbering the IO pins on the 26 pin header on a Raspberry Pi within RPi.GPIO

BOARD Mode

This refers to the pin numbers on the P1 header of the Raspberry Pi board

Advantage:

Your hardware will always work, regardless of the board revision of the RPi

You won’t need to rewire your connector or change your code

BCM Mode:

Lowest level way of working

It refers to the channel numbers on the Broadcom SoC

Disadvantage:

You will always have to work with a diagram of which channel number goes to which pin on the RPi board

Your script could break between revisions of the RasPi boards.

Page 6: 4. GPIO Access

Mayank Joneja

Pinout

Page 7: 4. GPIO Access

Mayank Joneja

Condensed Version

Page 8: 4. GPIO Access

Mayank Joneja

To specify which you are using:

GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

Page 9: 4. GPIO Access

Mayank Joneja

Hookup an LED

Connect the +ve pin of the LED to 3.3V via a resistor

Connect the –ve pin of the LED to Ground

Page 10: 4. GPIO Access

Mayank Joneja

Blink!

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7,GPIO.OUT)

GPIO.output(7,True) #to switch on,

#GPIO.output(7,False) #to switch off

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(4,GPIO.OUT)

GPIO.output(4,True) #to switch on,

#GPIO.output(4,False) #to switch off

Page 11: 4. GPIO Access

Mayank Joneja

To prevent warnings:

GPIO.setwarnings(False)

Delay Library:

time.sleep

Page 12: 4. GPIO Access

Mayank Joneja

The real Blink!

import RPi.GPIO as GPIO

from time import sleep

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7,GPIO.OUT)

while(True):

GPIO.output(7,True) #to switch on,

sleep(1) #stay on for 1 second

GPIO.output(7,False) #to switch off

sleep(1) #stay off for 1 second

Page 13: 4. GPIO Access

Mayank Joneja

Pulse Width Modulation

PWM is one of the most commonly

used techniques of achieving a wide

range of output voltages on a digital

output pin

Applications:

Controlling the brightness of an LED

Speed Control of motors

Driving Servo motors (Position control)

Page 14: 4. GPIO Access

Mayank Joneja

PWM on the Pi

import RPi.GPIO as GPIO

from time import sleep

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7,GPIO.OUT)

while(True):

for n in range (0,3000):

#90 % duty cycle

GPIO.output(7,True) #to switch on,

sleep(0.010) #stay on for 0.010 second

GPIO.output(7,False) #to switch off

sleep(0.000) #stay off for 0.000 second (what ? I’m making a point you know :P)

for n in range (0,3000):

#10 % duty cycle

GPIO.output(7,True) #to switch on,

sleep(0.001) #stay on for 0.001 second

GPIO.output(7,False) #to switch off

sleep(0.009) #stay off for 0.009 second

The LED glows bright at a 100%

duty cycle first and then dull at

a 10% duty cycle.

The on/off switching is so fast,

that the human eye can’t see

the flickering/blinking at this

frequency due to persistence

of vision

Page 15: 4. GPIO Access

Mayank Joneja

Buttons!

The simplest input device is a push button

(also called a micro switch)

A push button maintains an electrical

connection between its terminals as long

as the button is pressed

When the button is pressed, a connection

is created between T1(or T1’) and T2

or(T2’)

Page 16: 4. GPIO Access

Mayank Joneja

Pulling up/down a pin

Many times, when nothing is connected to a micro-controller input pin, they remain in a

state of high impedance (HiZ)

This can be interpreted as either 1 or 0 randomly by the micro.

In order to avoid this unwanted input, Pull up or pull down resistances are used to force

the pin to a state (VCC or GND) depending on the connection of the pushbutton.

Pull up involves connecting the pin to VCC via a big resistance like 10k Ohm

Pull down involves connecting the pin to GND via a big resistance like 10k Ohm

In either case, the resistor limits the current drawn from the power supply and ensures that

the path through the switch is ,electrically, the path of least resistance for current to flow

through once the switch is pressed.

Page 17: 4. GPIO Access

Mayank Joneja

Connections for pull down

Page 18: 4. GPIO Access

Mayank Joneja

Giggles

import RPi.GPIO as GPIO

from time import sleep

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11,GPIO.IN)

while True:

mybutton = GPIO.input(11)

if mybutton == True:print ‘giggle’

#Debouncing ??

sleep(0.2)

Press [CTRL]+[C] to terminate

execution via keyboard interrupt when you’re done

Page 19: 4. GPIO Access

Mayank Joneja

POP QUIZ (ominous thunder..)

Write a small code to replicate the switch’s state on the LED (on if pressed, off if not) with:

GPIO 7 as o/p LED, and 11 as i/p switch

Page 20: 4. GPIO Access

Mayank Joneja

import RPi.GPIO as GPIO

from time import sleep

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11,GPIO.IN)

GPIO.setup(7,GPIO.OUT)

while True:

mybutton = GPIO.input(11)

if mybutton == True:

GPIO.output(7,True)

else:

GPIO.output(7, False)

#Debouncing ??

sleep(0.2)

Page 21: 4. GPIO Access

Mayank Joneja

Scratch

Scratch is an educational programming language and multimedia authoring tool

Excellent first language

Allows you to make interesting games

GPIO Access on the Raspberry Pi

http://cymplecy.wordpress.com/2013/04/22/scratch-gpio-version-2-introduction-for-

beginners/

Page 22: 4. GPIO Access

Mayank Joneja

WebIOPi

The Internet of Things (IoT) refers to uniquely identifiable

objects and their virtual representations in an Internet-like

structure

WebIOPi is developed and tested on Raspbian

Only dependency is Python 2.7 or 3.2

https://learn.adafruit.com/raspberry-pi-garage-door-

opener/web-io-pi

Cool Project: https://learn.adafruit.com/raspberry-pi-garage-

door-opener/web-io-pi-configuration

wget http://webiopi.googlecode.com/files/WebIOPi-0.6.0.tar.gz

tar xvzf WebIOPi-0.6.0.tar.gz

cd WebIOPi-0.6.0

sudo ./setup.sh

Page 23: 4. GPIO Access

Mayank Joneja

sudo python –m webiopi 8000

8000: the port number

Connect the Raspberry Pi to the network

Open a browser and go to http://RaspberryPiIP:8000/

RaspberryPiIP refers to the IP of you Pi eg:192.168.1.10

You can even add a port redirection on your router and/or use IPv6 to control the GPIO

pins over the internet !

Page 24: 4. GPIO Access

Mayank Joneja

Running WebIOPi

User is “webiopi”

Password is “raspberry”

Glow LED, press Switch

By choosing the GPIO Header Link on the main page, you will be able to control the GPIO

using a web UI

Click the out/in button to change the direction

Click the pins to change the GPIO o/p state