Writes the value to the PWM register for the given pin. The Raspberry Pi has one on-board PWM pin, pin
1 (BCM_GPIO 18, Phys 12) and the range is 0-1024. .
Python Code 4.1.1 BreathingLED
First observe the project result, and then analyze the code.
1. Use cd command to enter 04.1.1_BreathingLED directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/04.1.1_BreathingLED
2. Use python command to execute python code “BreathingLED.py”.
python BreathingLED.py
After the program is executed, you'll see that LED is turned from on to off and then from off to on gradually
like breathing.
The following is the program code:
im port RPi. GPIO as GPIO
im port time
LedPin = 12
def setup():
g lobal p
GPIO. setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO. setup(LedPin, GPIO. OUT) # Set LedPin's mode is output
GPIO. output(LedPin, GPIO. LOW) # Set LedPin to low
p = GPIO. PWM(LedPin, 1000) # Set Frequency to 1KHz
p. start(0) # Duty Cycle = 0
def loop():
w hile Tr ue:
for dc in range( 0, 101, 1): # Increase duty cycle: 0~100
p.ChangeDutyCycle(dc) # Change duty cycle
time.sleep(0.01)
time.sleep(1)
for dc in range( 100, - 1, -1): # Decrease duty cycle: 100~0
p.ChangeDutyCycle(dc)
time.sleep(0.01)
time.sleep(1)
def destroy():
p. stop()
GPIO. output(LedPin, GPIO. LOW) # turn off led
GPIO. cleanup()
if __name__ == '__main__': # Program start from here
setup()
try: