Python in raspberry pi

4,999 views

Published on

Slides from my talk about Python and Raspberry Pi in Pycon

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,999
On SlideShare
0
From Embeds
0
Number of Embeds
410
Actions
Shares
0
Downloads
247
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Python in raspberry pi

  1. 1. Python in Raspberry Pi Sudar Muthu (@sudarmuthu) http://github.com/sudar http://hardwarefun.com
  2. 2. I love Python ;)
  3. 3. Credit Card Sized Computer
  4. 4. Basic Electronics http://en.wikipedia.org/wiki/File:OhmsLaw.svg
  5. 5. GPIO Pins http://learn.adafruit.com/assets/3052
  6. 6. Setup Python sudo apt-get install python-dev sudo apt-get install python-rpi.gpio
  7. 7. Set the status of GPIO Pins https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py
  8. 8. Set the status of GPIO Pins import RPi.GPIO as GPIO import 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
  9. 9. Demo Let there be Light https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/led-blink.py
  10. 10. Changing the brightness of the LED import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) p = GPIO.PWM(12, 50) # channel=12 frequency=50Hz p.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
  11. 11. Demo Can you see the brightness changing? https://github.com/sudar/raspberry-pi-sketches/blob/master/led-blink/pwm.py
  12. 12. Reading the status of the Pin import RPi.GPIO as GPIO import 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
  13. 13. Reading the status of the Pin https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py
  14. 14. Demo What happens when the button is pressed? https://github.com/sudar/raspberry-pi-sketches/blob/master/button-input/button-input.py
  15. 15. Combining Input and Output import RPi.GPIO as GPIO import 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
  16. 16. Combining Input and Output https://github.com/sudar/raspberry-pi-sketches/blob/master/button-and-led/button-and-led.py
  17. 17. 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
  18. 18. What more can be done?
  19. 19. More protocols • I2C • SPI • Serial
  20. 20. Interacting with webcam • “PyGame” provides easy interface • Can get fancy using “opencv” • Both USB and GPIO interface are supported
  21. 21. 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/t utorials/distributed-computing/
  22. 22. 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
  23. 23. Best of two worlds http://learn.adafruit.com/assets/3199 http://learn.adafruit.com/assets/2123
  24. 24. 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/tu torials/distributed-computing/
  25. 25. Thank you Sudar Muthu http://hardwarefun.com

×