python in raspberry pi

25
Python in Raspberry Pi Sudar Muthu (@sudarmuthu) http://github.com/sudar http://hardwarefun.com

Upload: sudar-muthu

Post on 28-Jan-2015

152 views

Category:

Technology


3 download

DESCRIPTION

Slides from my talk about Python and Raspberry Pi in Pycon

TRANSCRIPT

Page 1: Python in raspberry pi

Python in Raspberry Pi

Sudar Muthu (@sudarmuthu)http://github.com/sudarhttp://hardwarefun.com

Page 2: Python in raspberry pi

I love Python ;)

Page 3: Python in raspberry pi

Credit Card Sized Computer

Page 4: Python in raspberry pi

Basic Electronics

http://en.wikipedia.org/wiki/File:OhmsLaw.svg

Page 5: Python in raspberry pi

GPIO Pins

http://learn.adafruit.com/assets/3052

Page 6: Python in raspberry pi

Setup Python

sudo apt-get install python-dev

sudo apt-get install python-rpi.gpio

Page 7: Python in raspberry pi

Set the status of GPIO Pins

https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py

Page 8: Python in raspberry pi

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

Page 9: Python in raspberry pi

Demo

Let there be Light

https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py

Page 10: Python in raspberry pi

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

Page 11: Python in raspberry pi

Demo

Can you see the brightness changing?

https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/pwm.py

Page 12: Python in raspberry pi

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

Page 13: Python in raspberry pi

Reading the status of the Pin

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py

Page 14: Python in raspberry pi

Demo

What happens when the button is pressed?

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py

Page 15: Python in raspberry pi

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

Page 16: Python in raspberry pi

Combining Input and Output

https://github.com/sudar/raspberry-pi-sketches/blob/master/button-and-led/button-and-led.py

Page 17: Python in raspberry pi

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

Page 18: Python in raspberry pi

What more can be done?

Page 19: Python in raspberry pi

More protocols

• I2C• SPI• Serial

Page 20: Python in raspberry pi

Interacting with webcam

• “PyGame” provides easy interface• Can get fancy using “opencv”• Both USB and GPIO interface are supported

Page 21: Python in raspberry pi

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/

Page 22: Python in raspberry pi

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

Page 23: Python in raspberry pi

Best of two worlds

http://learn.adafruit.com/assets/3199 http://learn.adafruit.com/assets/2123

Page 25: Python in raspberry pi

Thank you

Sudar Muthuhttp://hardwarefun.com