Advertisement
vegaseat

Raspberry Pi simple 4 led control

Feb 28th, 2015
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. #!/usr/bin/python
  2. ''' rp_output_test104.py
  3.  
  4. Turn 4 different LEDs on and off
  5.  
  6. The black terminal strip is located on the upper side of the Raspberry B+ board
  7. there a two rows of 20 physical pins each
  8. the upper row starts with the even pins 2 (upper left corner, ulc) to 40
  9. the lower row starts with the odd pins 1 (lower left corner, llc) to 39
  10.  
  11. We will be using the following pins:
  12. GND (pin 9)
  13. GPIO17 (pin 11)
  14. GPIO27 (pin 13)
  15. GPIO22 (pin 15)
  16. GPIO23 (pin 16)
  17. 17, 22, 23, 27 are the internal Broadcom (BCM) numbers
  18.  
  19. For this experiment I used the LED colors red, yellow, blue, green
  20.  
  21. Each of the LEDs is in series with a 220 ohm current limiting resistor
  22. connect the LED cathode (has flat spot, shorter lead) toward GND and
  23. the free end of the resistor to the proper GPIO
  24. GPIO HIGH will be about +3 volt (each LED draws about 5 mA)
  25. GPIO LOW will be 0 volt
  26.  
  27. 220 ohm --> red(2) red(2) brown(1) gold(5%) bands
  28. (you can use a 200 to 500 ohm resistor to limit the current)
  29.  
  30. Note:
  31. each GPIO is limited to no more than 15mA current draw
  32. the sum of all currents flowing through GPIOs is limited to 50mA
  33. If you want to draw more current at a higher voltage simply use
  34. an inexpensive power MOSFET that can drive a motor and also isolates
  35. the Raspberry Pi from damaging voltages.  See the MOSFET video at:
  36. https://www.youtube.com/watch?v=I1h-HZmIJ4E
  37. eg. IRF540N (100V, 30A, 0.1ohm)
  38.  
  39. To execute this code from the idle IDE start idle as root (super user)
  40. via terminal command:  sudo idle
  41.  
  42. To run from the LXTerminal -->
  43. (assume that the code is in directory rpi_python)
  44. cd rpi_python
  45. sudo python rp_output_test104.py
  46.  
  47. picture at: prntscr.com/5kbm3s
  48. code stored at: http://pastebin.com/riezGXTE
  49.  
  50. for parts used see ...
  51. http://www.canakit.com/raspberry-pi-starter-ultimate-kit.html
  52.  
  53. pastebin ...
  54. http://pastebin.com/N5mxYfVA
  55.  
  56. tested with a $35 Raspberry PI B+, Linux/Rasbian OS, Python27
  57. by  vegaseat   23jan2015
  58. '''
  59.  
  60. import RPi.GPIO as gpio
  61. import time
  62.  
  63. def output_on(nn):
  64.     # GPIOnn will go to +3.3 volt
  65.     # you can use gpio.HIGH, True, or 1
  66.     gpio.output(nn, gpio.HIGH)
  67.  
  68. def output_off(nn):
  69.     # GPIOnn goes to 0 volt
  70.     # you can use gpio.LOW, False, or 0
  71.     gpio.output(nn, gpio.LOW)
  72.    
  73.  
  74. def output_blink(nn, blinks, delay=1):
  75.     for k in range(blinks):
  76.         print("Blink {}".format(k+1))
  77.         output_on(nn)
  78.         time.sleep(delay)
  79.         output_off(nn)
  80.         time.sleep(delay)
  81.  
  82. # use Broadcom GPIO 00..nn numbers (recommended)
  83. gpio.setmode(gpio.BCM)
  84.  
  85. led_list = ['red', 'yellow', 'blue', 'green']
  86. # these are the BCM GPIO numbers we are using
  87. # the red LED is connected to GPIO17 and so on
  88. gpio_list = [17, 22, 23, 27]
  89. # set them all to output
  90. for nn in gpio_list:
  91.     gpio.setup(nn, gpio.OUT)
  92.  
  93. delay = 3
  94. for color, nn in enumerate(gpio_list):
  95.     sf = "LED {} on for {} seconds ..."
  96.     print(sf.format(led_list[color], delay))
  97.     output_on(nn)
  98.     # keep LED on for delay seconds
  99.     time.sleep(delay)
  100.     output_off(nn)
  101.     # wait 1 second
  102.     time.sleep(1)
  103.  
  104.  
  105. # let's do some blinking
  106. nn = 17
  107. blinks = 5
  108. delay = 0.5
  109. sf = "LED {} blinking {} times with {} second delay"
  110. for color, nn in enumerate(gpio_list):
  111.     print(sf.format(led_list[color], blinks, delay))
  112.     output_blink(nn, blinks, delay)
  113.  
  114. print("Real quick action ...")
  115. for k in range(20):
  116.     for nn in gpio_list:
  117.         output_on(nn)
  118.         time.sleep(0.1)
  119.         output_off(nn)
  120.         time.sleep(0.05)
  121.        
  122. print("Turn all LEDs on for 1 minute ...")
  123. for nn in gpio_list:
  124.     output_on(nn)
  125.  
  126. time.sleep(60)
  127.  
  128. # reset all GPIO channels used by this program
  129. gpio.cleanup()
  130.  
  131. #help(gpio)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement