vegaseat

Raspberry Pi Output Test 101

Jan 23rd, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. #!/usr/bin/python
  2. ''' rp_output_test101.py
  3.  
  4. use the Raspberry Pi computer to exercise an LED (light emitting diode)
  5. turn on and off, blink and dim via Pulse Width Modulation
  6.  
  7. the black terminal strip is located on the upper side of the Raspberry B+ board
  8. there a two rows of 20 physical pins each
  9. the upper row starts with the even pins 2 (upper left corner, ulc) to 40
  10. the lower row starts with the odd pins 1 (lower left corner, llc) to 39
  11.  
  12. We will be using the following pins:
  13. GND (pin 9)
  14. GPIO_17 (pin 11)
  15. Connect the LED (cathode, flat spot, shorter lead toward GND) in series with
  16. a 200 to 500 ohm current limiting resistor between GPIO_17 and GND
  17.  
  18. Note: each GPIO is limited to no more than 15mA current draw
  19. the sum of all GPIO current draws is limited to 50mA
  20.  
  21. to execute this code run IDLE as a super user via terminal command
  22. sudo idle
  23.  
  24. or from the terminal (needs shebang line)
  25. assume that the code is in directory rpi_python ...
  26. cd rpi_python
  27. sudo python rp_output_test101.py
  28.  
  29. for parts used see ...
  30. http://www.canakit.com/raspberry-pi-starter-ultimate-kit.html
  31.  
  32. To run from the LXTerminal -->
  33. cd rpi_python
  34. sudo python rp_output_test101.py
  35.  
  36. code stored at
  37.  
  38. '''
  39.  
  40. import RPi.GPIO as gpio
  41. import time
  42. from datetime import datetime as dt
  43.  
  44.  
  45. def output_17_on():
  46.     # GPIO_17 will go to about +3.3 volt
  47.     # you can use gpio.HIGH, True, or 1
  48.     gpio.output(17, gpio.HIGH)
  49.  
  50. def output_17_off():
  51.     # GPIO_17 goes to 0 volt
  52.     # you can use gpio.LOW, False, or 0
  53.     gpio.output(17, gpio.LOW)
  54.  
  55. def output_17_blink(blinks, delay=1):
  56.     for n in range(blinks):
  57.         print("Blink {}".format(n+1))
  58.         output_17_on()
  59.         time.sleep(delay)
  60.         output_17_off()
  61.         time.sleep(delay)
  62.  
  63. # use Broadcom GPIO 00..nn numbers (recommended)
  64. gpio.setmode(gpio.BCM)
  65.  
  66. # set GPIO_17 as an output
  67. gpio.setup(17, gpio.OUT)
  68.  
  69. # let's do some blinking
  70. blinks = 5
  71. delay = 0.5
  72. print("LED blinking {} times with {} second delay".format(blinks, delay))
  73. output_17_blink(blinks, delay)
  74.  
  75. print("LED on for 5 seconds ...")
  76. output_17_on()
  77. # keep LED on for 5 seconds
  78. time.sleep(5)
  79. output_17_off()
  80. # wait 1 second
  81. time.sleep(1)
  82.  
  83. print("Use pulse width modulation to dim the LED ...")
  84. # faster than the eye can detect
  85. frequency = 60
  86. # you can change it with
  87. # pwm.ChangeFrequency(frequency)
  88.  
  89. pwm = gpio.PWM(17, frequency)
  90.  
  91. # the LED is off at a dutycycle of 0 and fully on at 100
  92. dutycycle = 0
  93. pwm.start(dutycycle)
  94. for dutycycle in range(0, 101, 10):
  95.     print("dutycycle = {}".format(dutycycle))
  96.     pwm.ChangeDutyCycle(dutycycle)
  97.     time.sleep(2.5)
  98.  
  99. # also turns LED off
  100. pwm.stop()
  101.  
  102. # reset all GPIO channels
  103. gpio.cleanup()
  104.  
  105. #help(gpio)
  106.  
  107.  
  108. #help(gpio)
Advertisement
Add Comment
Please, Sign In to add comment