User Manual
52
Step 3: Compile the Code
make 04_breathLed
Step 4: Run the executable file above
sudo ./04_breathLed
Code Explanation
pinMode(LedPin, PWM_OUTPUT); // Set the I/O as pwn output
for(i=0;i<1024;i++){ // i,as the value of pwm, increases progressively during 0-1024.
pwmWrite(LedPin, i); // Write i into the LEDPin
delay(2); // wait for 2ms, interval time between the changes indicates the speed of
breathing.
} // the value of pwm add 1 every 2ms, when the value of pwm increases, the luminance of
the LED increases.
for(i=1023;i>=0;i--){
pwmWrite(LedPin, i);
delay(2);
} // the value of pwm minus 1 every 2ms, when the value of pwm decreases, the luminance
of the LED decreases.
For Python users:
Step 2: Open the code file
cd /home/pi/SunFounder_Super_Kit_V3.0_for_Raspberry_Pi/Python
Step 3: Run
sudo python 04_breathLed.py
Code Explanation
GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.LOW) # Set LedPin as OUTPUT, initialize the
pin as low level.
pLED = GPIO.PWM(LedPin, 1000) # use PWM in the RPi.GPIO library. Set LedPin as analog
PWM output, the frequency as 1000Hz, assign these configurations to pLed.
pLed.start(0) # Start pLed with 0% pulse width
time.sleep(0.05)
while True:
# Increase duty cycle from 0 to 100
for dc in range(0, 101, step): # set dc from 0 to 100 in for loop. Set step to
cycle.
# Change duty cycle to dc
SunFounder