User Tools

Site Tools


user:bmurphy7:hpc0:breathingled

BreathingLED

The Circuit:

The Code:

  1 #!/usr/bin/env python3                                           
  2 ########################################################################
  3 # Filename    : BreathingLED.py                                  
  4 # Description : Breathing LED                                    
  5 # Author      : www.freenove.com                                 
  6 # modification: 2019/12/27                                       
  7 ########################################################################
  8 import RPi.GPIO as GPIO                                          
  9 import time                                                      
 10                                                                  
 11 LedPin = 12                           # define the LedPin        
 12                                                                  
 13 def setup():                                                     
 14     global p                                                     
 15     GPIO.setmode(GPIO.BOARD)          # use PHYSICAL GPIO Numbering
 16     GPIO.setup(LedPin, GPIO.OUT)      # set LedPin to OUTPUT mode
 17     GPIO.output(LedPin, GPIO.LOW)     # make ledPin output LOW level to turn off LED 
 18                                                                  
 19     p = GPIO.PWM(LedPin, 500)         # set PWM Frequence to 500Hz
 20     p.start(0)                        # set initial Duty Cycle to 0
 21                                                                  
 22 #Function that handles the brightness of the LED                 
 23 def loop():                                                      
 24     while True:                                                  
 25         #While in brightness  ranges of 0 - 100 get brighter. Stop at 100
 26         for dc in range(0, 101, 1):                              
 27             p.ChangeDutyCycle(dc)     # set dc value as the duty cycle
 28             time.sleep(0.01)                                     
 29         #after hitting max birghtness wait 1 second              
 30         time.sleep(1)                                            
 31         #after hitting max brightness darken the LED till it is off
 32         for dc in range(100, -1, -1):                            
 33             p.ChangeDutyCycle(dc)     # set dc value as the duty cycle
 34             time.sleep(0.01)                                     
 35         #when the LED is off wait one second before starting over
 36         time.sleep(1)                                            
 37                                                                  
 38 #function that will end the program                              
 39 def destroy():                                                   
 40     p.stop()                          # stop PWM                 
 41     GPIO.cleanup()                    # Release all GPIO         
 42                                                                  
 43 if __name__ == '__main__':                                       
 44     print ('Program is starting ... ')                           
 45     setup()                                                      
 46     try:                                                         
 47         loop()                                                   
 48     except KeyboardInterrupt:         # When program ends cleanup                                           
 49         destroy()

In this project the LED will gradually get brighter and brighter till it hits max brightness. Then it will grow darker and darker till the LED is turned off. This was a simple setup only requiring a resistor, LED, and two jumpers. Otherwise I went through the code making the comments look neater and explaining the functions.

user/bmurphy7/hpc0/breathingled.txt · Last modified: 2020/05/10 13:09 by 127.0.0.1