day4
DESCRIPTION
Python on Raspberry Pi - Day 4 slides. For the full course go to http://raspberrypyrates.wordpress.comTRANSCRIPT
Electricity Resistors Bread boardSwitches MP3 Player
Module 4
Electricity & Current
Powerful pump
Force of water is too much for the wheel
It breaks!
Add artificial resistance in the pipe
Protects the wheel
Electricity & Current
Electricity & Current
Power Source (high voltage)
Ground (zero voltage)
Resistor (560 Ohms)
LED
Electricity & CurrentFlow of current
Remember:
Current flows from high voltage (3.3V) to low voltage (zero V aka Ground)
Why Bread Boards?
No need to solder simple circuits by hand
Easy to prototype
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
Bread Board
(a)
(b)
(c)
How are the three wires connected?
Bread Board
(a)
(b)
(c)
correct
How are the three wires connected?
Electricity & Current
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.
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
Program GPIO pins>>> import Rpi.GPIO as gpio>>> gpio.setmode(gpio.BCM)>>> gpio.setup(18, gpio.IN)>>> print(gpio.input(18))
Switch ConceptSwitch Off Switch On
if gpio.input(18) == gpio.LOW
if gpio.input(18) == gpio.HIGH
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
Switch Pull-Up Circuit
Switch Pull-Up CircuitSwitch Off Switch On
if gpio.input(18) == gpio.HIGH
if gpio.input(18) == gpio.LOW
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')
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
Switch Pull Up Circuit
Refer to https://bitbucket.org/avilay/raspberry_pyrates/src/master/switch_simple.py
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
Photocell as Switch
Photocell as SwitchIn Dark In Light
if gpio.input(18) == gpio.HIGH
if gpio.input(18) == gpio.LOW
Photocell as Switch
Refer to https://bitbucket.org/avilay/raspberry_pyrates/src/master/photocell.py
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 &')
Electricity Resistors Bread boardSwitches MP3 PlayerGot It!!
Module 4
LEDsSensors Cool Projects
Module 4
What do you want to do next?
Controlling LED
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
Blinking LED
Refer to https://bitbucket.org/avilay/raspberry_pyrates/src/master/led_blink.py
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
Some Sensors to Play WithDC Motor – spins anything connected to it, like a wheel
Camera board