vegaseat

Raspberry Pi 4 LED output

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