Questions And Answers

More Tutorials

Raspberry Breathing Light

In this project, we will realize the effect of breathing light. That is, the brightness of LED will be changed from dark to light, and then from light to dark.

Hardware Connection

In this project, the electronic hardware we need to use is as follows:

• Raspberry Pi Pico

• Grove Shield for Pi Pico

• Grove - LED Pack

Connect the LED to D18.

Write a Program

To realize the effect of breathing light, we need to use arithmetic operators to operate the duty cycle of the PWM signal.First, declare the functions in the library we need to use, and set the pins.

1 from machine import Pin, PWM 
2 import utime
4 LED_PWM=PWM(Pin(18))

After setting the pin, use the “freq()" function to set the frequency of the PWM signal to 500:

1 LED_PWM. freq(500)

Through analysis, we can find that the breathing light is actually composed of two parts. The first part is that the LED lights up gradually from the dim state, and enters the second part when the brightness reaches the maximum. In the second part, the brightness of the LED will begins to decay until it goes out. Repeating these two parts in alternation, we can realize the effect of a breathing light.

We can use two while-loop statements to realize the effects of these two parts. First, we set a variable "val" to help us adjust the duty cycle of the PWM signal. At the beginning of the program, we set val to 0:

1 val = 0

Then we’ll work on the first part, where the LED lights up gradually:

1 while val<65535:
2 val=val+50 
3 utime. sleep_ms(1)
4 LED_PWM. duty_u16(val)

In this program, we increase the value of “val" by 50 every millisecond until it is greater than 65535, and use the “duty_u16" function to adjust the duty cycle of the PWM signal using this same value.

When the value of “val" is greater than 65535, our code will exit the first while-loop and enter the next while-loop. Similarly, to make the light dim gradually, we keep decreasing the value of “val" until it is 0.

1 while val>o: 
2 val=val-50
3 utime.sleep_ms(1) 
4 LED_PWM. duty_u16(val)

Then, we will place both loops in another “while True" loop to make the two parts of the program run continuously, and the effect of breathing light is completed.

1 import utime 
2 from machine import Pin, PWM
4 LED_PWM=PWM(Pin(18))
6 val=0 
7 LED_PWM. freq(500) 
8 while True:
9 while val<65535:
10 val=val+50 
11 utime. sleep_ms(1)
12 LED_PWM. duty_u16(val)
13 while val>0:
14 val=val-50 
15 utime.sleep_ms(1) 
16 LED_PWM. duty_u16(val)

Use a USB cable to connect Pico to the computer, click the “run" button to save the program to any location, you can see the program in action.


In this page (written and validated by ) you learned about Raspberry Breathing Light . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Make Buzzer Sing the Use of Function.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.