day4

32
Electricit y Resistors Bread board Switches MP3 Player Module 4

Upload: avilay-parekh

Post on 27-Jun-2015

328 views

Category:

Engineering


0 download

DESCRIPTION

Python on Raspberry Pi - Day 4 slides. For the full course go to http://raspberrypyrates.wordpress.com

TRANSCRIPT

Page 1: Day4

Electricity Resistors Bread boardSwitches MP3 Player

Module 4

Page 2: Day4

Electricity & Current

Powerful pump

Force of water is too much for the wheel

It breaks!

Add artificial resistance in the pipe

Protects the wheel

Page 3: Day4

Electricity & Current

Page 4: Day4

Electricity & Current

Power Source (high voltage)

Ground (zero voltage)

Resistor (560 Ohms)

LED

Page 5: Day4

Electricity & CurrentFlow of current

Remember:

Current flows from high voltage (3.3V) to low voltage (zero V aka Ground)

Page 6: Day4

Why Bread Boards?

No need to solder simple circuits by hand

Easy to prototype

Page 7: Day4

Bread BoardThis is one connected

wire

Signs don’t really mean anything

Convention:

Connect the power source to the red + connector

Connect GND to the blue - connector

Page 8: Day4

Bread Board

(a)

(b)

(c)

How are the three wires connected?

Page 9: Day4

Bread Board

(a)

(b)

(c)

correct

How are the three wires connected?

Page 10: Day4

Electricity & Current

Page 11: Day4

Raspberry Pi GPIO pinsGeneral Purpose Input Output pins

Can connect any input or output device to each pin

e.g., motors, motion sensors, light sensors, etc.

Page 12: Day4

PI CobblerGPIO pins are not labelled on the R-Pi

Need to get the correct jumper wires

Annoying to read up the number and connect on bread board

Pi-Cobbler

Page 13: Day4

Program GPIO pins>>> import Rpi.GPIO as gpio>>> gpio.setmode(gpio.BCM)>>> gpio.setup(18, gpio.IN)>>> print(gpio.input(18))

Page 14: Day4

Switch ConceptSwitch Off Switch On

if gpio.input(18) == gpio.LOW

if gpio.input(18) == gpio.HIGH

Page 15: Day4

Switch ConceptWon’t Work!

When switch is off, pin 18 is in “floating” state

It will not give reliable reading

Sometimes it will be gpio.HIGH, sometimes gpio.LOW

Somehow connect pin 18 to GND when switch is on or off

Page 16: Day4

Switch Pull-Up Circuit

Page 17: Day4

Switch Pull-Up CircuitSwitch Off Switch On

if gpio.input(18) == gpio.HIGH

if gpio.input(18) == gpio.LOW

Page 18: Day4

Switch Pull Up Circuitimport RPi.GPIO as gpio

gpio.setmode(gpio.BCM)gpio.setup(18, gpio.IN)

while True:if gpio.input(18) == gpio.LOW:

print('************* SWITCH ON')

elif gpio.input(18) == gpio.HIGH:print('------------ SWITCH

OFF')

Page 19: Day4

Switch Pull Up Circuit

Computer is checking the while condition around 2000 times per second!

We are actually only interested in the state change from on off or from off on

Page 20: Day4

Switch Pull Up Circuit

Refer to https://bitbucket.org/avilay/raspberry_pyrates/src/master/switch_simple.py

Page 21: Day4

PhotocellIt is a resistor whose resistance changes with light

In light less resistance more current can pass through it

In dark very high resistance no current can pass through

Page 22: Day4

Photocell as Switch

Page 23: Day4

Photocell as SwitchIn Dark In Light

if gpio.input(18) == gpio.HIGH

if gpio.input(18) == gpio.LOW

Page 24: Day4

Photocell as Switch

Refer to https://bitbucket.org/avilay/raspberry_pyrates/src/master/photocell.py

Page 25: Day4

MP3 Player

def handle_state_change(curr_switch_state):os.system('espeak "Changing track."')time.sleep(2)if curr_switch_state == 'LIGHT':

os.system('mpg123 /home/pi/viva.mp3 &')elif curr_switch_state == 'DARK':

os.system('mpg123 /home/pi/waiting.mp3 &')

Page 26: Day4

Electricity Resistors Bread boardSwitches MP3 PlayerGot It!!

Module 4

Page 27: Day4

LEDsSensors Cool Projects

Module 4

What do you want to do next?

Page 28: Day4

Controlling LED

Page 29: Day4

Controlling LEDimport timeimport RPi.GPIO as gpio

gpio.setmode(gpio.BCM)gpio.setup(22, gpio.OUT)

gpio.output(22, gpio.HIGH)time.sleep(5)gpio.cleanup()

Note: Using pin 22 as an OUTPUT pin this time

Page 30: Day4

Blinking LED

Refer to https://bitbucket.org/avilay/raspberry_pyrates/src/master/led_blink.py

Page 31: Day4

Some Sensors to Play WithDC Motor – spins anything connected to it, like a wheel

Temperature sensor – measures the temperature of the environment

Vibration sensor – detects vibration to whatever it is attached to

Page 32: Day4

Some Sensors to Play WithDC Motor – spins anything connected to it, like a wheel

Camera board