Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ''' rp_output_test102.py
- use the Raspberry Pi computer to exercise an LED (light emitting diode)
- turn on and off, blink and dim via Pulse Width Modulation
- 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)
- GPIO_17 (pin 11)
- Connect the LED (cathode, flat spot, shorter lead toward GND) in series with
- a 200 to 500 ohm current limiting resistor between GPIO_17 and GND
- Note: each GPIO is limited to no more than 15mA current draw
- the sum of all GPIO current draws is limited to 50mA
- to execute this code run IDLE as a super user via terminal command
- sudo idle
- or from the terminal (needs shebang line)
- assume that the code is in directory rpi_python ...
- cd rpi_python
- sudo python rp_output_test102.py
- for parts used see ...
- http://www.canakit.com/raspberry-pi-starter-ultimate-kit.html
- To run from the LXTerminal -->
- cd rpi_python
- sudo python rp_output_test102.py
- code stored at
- '''
- import RPi.GPIO as gpio
- import time
- from datetime import datetime as dt
- def output_17_on():
- # GPIO_17 will go to about +3.3 volt
- # you can use gpio.HIGH, True, or 1
- gpio.output(17, gpio.HIGH)
- def output_17_off():
- # GPIO_17 goes to 0 volt
- # you can use gpio.LOW, False, or 0
- gpio.output(17, gpio.LOW)
- def output_17_blink(blinks, delay=1):
- for n in range(blinks):
- print("Blink {}".format(n+1))
- output_17_on()
- time.sleep(delay)
- output_17_off()
- time.sleep(delay)
- # use Broadcom GPIO 00..nn numbers (recommended)
- gpio.setmode(gpio.BCM)
- # set GPIO_17 as an output
- gpio.setup(17, gpio.OUT)
- # let's do some blinking
- blinks = 5
- delay = 0.5
- print("LED blinking {} times with {} second delay".format(blinks, delay))
- output_17_blink(blinks, delay)
- print("LED on for 5 seconds ...")
- output_17_on()
- # keep LED on for 5 seconds
- time.sleep(5)
- output_17_off()
- # wait 1 second
- time.sleep(1)
- print("Use pulse width modulation to dim the LED ...")
- # faster than the eye can detect
- frequency = 60
- # you can change it with
- # pwm.ChangeFrequency(frequency)
- pwm = gpio.PWM(17, frequency)
- # the LED is off at a dutycycle of 0 and fully on at 100
- dutycycle = 0
- pwm.start(dutycycle)
- try:
- print("Gradually increasing brightness ...")
- for dutycycle in range(0, 101):
- pwm.ChangeDutyCycle(dutycycle)
- time.sleep(0.1)
- print("dutycycle now at = {}%".format(dutycycle))
- print("Gradually decreasing brightness ...")
- for dutycycle in range(100, -1, -1):
- pwm.ChangeDutyCycle(dutycycle)
- time.sleep(0.1)
- print("dutycycle now at = {}%".format(dutycycle))
- # respond to ctrl/c (in case you can't wait any more)
- except KeyboardInterrupt:
- pass
- # also turns LED off
- pwm.stop()
- # reset all GPIO channels
- gpio.cleanup()
- #help(gpio)
- #help(gpio)
Advertisement
Add Comment
Please, Sign In to add comment