Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ''' rp_output_test104.py
- Turn 4 different LEDs on and off
- The black terminal strip is located on the upper side of the Raspberry B+ board
- there a two rows of 20 physical pins each
- the upper row starts with the even pins 2 (upper left corner, ulc) to 40
- the lower row starts with the odd pins 1 (lower left corner, llc) to 39
- We will be using the following pins:
- GND (pin 9)
- GPIO17 (pin 11)
- GPIO27 (pin 13)
- GPIO22 (pin 15)
- GPIO23 (pin 16)
- 17, 22, 23, 27 are the internal Broadcom (BCM) numbers
- For this experiment I used the LED colors red, yellow, blue, green
- Each of the LEDs is in series with a 220 ohm current limiting resistor
- connect the LED cathode (has flat spot, shorter lead) toward GND and
- the free end of the resistor to the proper GPIO
- GPIO HIGH will be about +3 volt (each LED draws about 5 mA)
- GPIO LOW will be 0 volt
- 220 ohm --> red(2) red(2) brown(1) gold(5%) bands
- (you can use a 200 to 500 ohm resistor to limit the current)
- Note:
- each GPIO is limited to no more than 15mA current draw
- the sum of all currents flowing through GPIOs is limited to 50mA
- If you want to draw more current at a higher voltage simply use
- an inexpensive power MOSFET that can drive a motor and also isolates
- the Raspberry Pi from damaging voltages. See the MOSFET video at:
- https://www.youtube.com/watch?v=I1h-HZmIJ4E
- eg. IRF540N (100V, 30A, 0.1ohm)
- To execute this code from the idle IDE start idle as root (super user)
- via terminal command: sudo idle
- To run from the LXTerminal -->
- (assume that the code is in directory rpi_python)
- cd rpi_python
- sudo python rp_output_test104.py
- picture at: prntscr.com/5kbm3s
- code stored at: http://pastebin.com/riezGXTE
- for parts used see ...
- http://www.canakit.com/raspberry-pi-starter-ultimate-kit.html
- pastebin ...
- http://pastebin.com/N5mxYfVA
- tested with a $35 Raspberry PI B+, Linux/Rasbian OS, Python27
- by vegaseat 23jan2015
- '''
- import RPi.GPIO as gpio
- import time
- def output_on(nn):
- # GPIOnn will go to +3.3 volt
- # you can use gpio.HIGH, True, or 1
- gpio.output(nn, gpio.HIGH)
- def output_off(nn):
- # GPIOnn goes to 0 volt
- # you can use gpio.LOW, False, or 0
- gpio.output(nn, gpio.LOW)
- def output_blink(nn, blinks, delay=1):
- for k in range(blinks):
- print("Blink {}".format(k+1))
- output_on(nn)
- time.sleep(delay)
- output_off(nn)
- time.sleep(delay)
- # use Broadcom GPIO 00..nn numbers (recommended)
- gpio.setmode(gpio.BCM)
- led_list = ['red', 'yellow', 'blue', 'green']
- # these are the BCM GPIO numbers we are using
- # the red LED is connected to GPIO17 and so on
- gpio_list = [17, 22, 23, 27]
- # set them all to output
- for nn in gpio_list:
- gpio.setup(nn, gpio.OUT)
- delay = 3
- for color, nn in enumerate(gpio_list):
- sf = "LED {} on for {} seconds ..."
- print(sf.format(led_list[color], delay))
- output_on(nn)
- # keep LED on for delay seconds
- time.sleep(delay)
- output_off(nn)
- # wait 1 second
- time.sleep(1)
- # let's do some blinking
- nn = 17
- blinks = 5
- delay = 0.5
- sf = "LED {} blinking {} times with {} second delay"
- for color, nn in enumerate(gpio_list):
- print(sf.format(led_list[color], blinks, delay))
- output_blink(nn, blinks, delay)
- print("Real quick action ...")
- for k in range(20):
- for nn in gpio_list:
- output_on(nn)
- time.sleep(0.1)
- output_off(nn)
- time.sleep(0.05)
- print("Turn all LEDs on for 1 minute ...")
- for nn in gpio_list:
- output_on(nn)
- time.sleep(60)
- # reset all GPIO channels used by this program
- gpio.cleanup()
- #help(gpio)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement