Python in raspberry pi
-
Upload
sudar-muthu -
Category
Technology
-
view
134 -
download
3
description
Transcript of Python in raspberry pi
Python in Raspberry Pi
Sudar Muthu (@sudarmuthu)http://github.com/sudarhttp://hardwarefun.com
I love Python ;)
Credit Card Sized Computer
Basic Electronics
http://en.wikipedia.org/wiki/File:OhmsLaw.svg
GPIO Pins
http://learn.adafruit.com/assets/3052
Setup Python
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio
Set the status of GPIO Pins
https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py
Set the status of GPIO Pinsimport RPi.GPIO as GPIOimport time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
try: while True: GPIO.output(12, GPIO.HIGH) time.sleep(1) GPIO.output(12, GPIO.LOW) time.sleep(1)finally: GPIO.cleanup()
https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py
Demo
Let there be Light
https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py
Changing the brightness of the LEDimport RPi.GPIO as GPIOimport time
GPIO.setmode(GPIO.BOARD)GPIO.setup(12, GPIO.OUT)
p = GPIO.PWM(12, 50) # channel=12 frequency=50Hzp.start(0)
try: while True: for dc in range(0, 101, 5): p.ChangeDutyCycle(dc) time.sleep(0.1) for dc in range(100, -1, -5): p.ChangeDutyCycle(dc) time.sleep(0.1)finally: p.stop() GPIO.cleanup()
https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/pwm.py
Demo
Can you see the brightness changing?
https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/pwm.py
Reading the status of the Pinimport RPi.GPIO as GPIOimport time
GPIO.setmode(GPIO.BOARD)GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
try: while True: if GPIO.input(11): print "Button is on" else: print "Button is off" time.sleep(0.1)
finally: GPIO.cleanup()
https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py
Reading the status of the Pin
https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py
Demo
What happens when the button is pressed?
https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py
Combining Input and Outputimport RPi.GPIO as GPIOimport time
GPIO.setmode(GPIO.BOARD)GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)GPIO.setup(12, GPIO.OUT)
try: while True: if GPIO.input(11): print "Button is on" GPIO.output(12, 1) else: GPIO.output(12, 0) time.sleep(0.1)
finally: GPIO.cleanup()
https://github.com/sudar/raspberry-pi-sketches/blob/master/button-and-led/button-and-led.py
Combining Input and Output
https://github.com/sudar/raspberry-pi-sketches/blob/master/button-and-led/button-and-led.py
Demo
Let’s control the LED by pressing the button
https://github.com/sudar/raspberry-pi-sketches/blob/master/button-and-led/button-and-led.py
What more can be done?
More protocols
• I2C• SPI• Serial
Interacting with webcam
• “PyGame” provides easy interface• Can get fancy using “opencv”• Both USB and GPIO interface are supported
Distributed Computing
• Each Pi can be used as cheap node• Form grids using a cluster of Pi’s• Can share CPU, memory and disk space
http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/distributed-computing/
Limitations
• No built-in Analog to Digital support• Can’t run Inductive load (motors)• Is not real-time (CPU might be busy)• No “safe circuits” present• Operates at 3.3V and is not directly
compatible with Arduino voltage
Best of two worlds
http://learn.adafruit.com/assets/3199 http://learn.adafruit.com/assets/2123
Links
• Source code - https://github.com/sudar/raspberry-pi-sketches/
• My blog - http://hardwarefun.com• Python GPIO -
https://code.google.com/p/raspberry-gpio-python/
• Distributed computing using Pi - http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/distributed-computing/