7.5 Breathing LED

Share for us

Knowing how to change the brightness of an LED, we will learn how to make a breathing LED with its luminance changing continuously. – It’s kind of like the notification indicator on your cell phone when there’s new messages or missed calls.

Breathing LED is cool. If you have some knowledge about electronic technology products, you should know of the breathing LED definitely. This simple stuff will make your creations more interesting.

Save a copy of the code in the last chapter and name it as breath_led.py.

With a display, you can use the Python IDLE directly. Click File -> Save as in the upper left corner, or just press Ctrl + Shift + S.

For remote login, you can use the cp command:

cp pwm_led.py breath_led.py.

Let’s get started!

We have learnt the while loop before, and let’s continue to learn another one: the for loop. The for loop is perfect for counting, so it can be applied just well in breathing LED.

Let’s have a glance at the code:

import RPi.GPIO as GPIO

import time

 

led_pin = 17

delay = 0.01                 # delay for LED breathing speed

freq = 100

 

GPIO.setmode(GPIO.BCM)

GPIO.setup(led_pin, GPIO.OUT)

p = GPIO.PWM(led_pin, freq)

p.start(0)

 

def set_led_value(value):

    p.ChangeDutyCycle(100-value)

 

while True:

    for i in range(0, 101):   # Loop "i" from 0 to 100

        set_led_value(i)      # Set led value to i

        time.sleep(delay)     # delay for LED breathing speed

Change delay to the LED “breathing” speed, and replace the code in while True with the for loop.

In for i in range(0, 101), range is a function to generate an integer list, and the parameters mean that the list ranges from 0 to 100. It also has a hidden parameter step whose value is 1 by default so you need no concern. In each cycle, the for loop will assign the values in the list after in to i sequentially. In other words, i will take a value in sequence from range(0, 101) in each cycle. The LED’s brightness will be set different in each cycle, and a delay is added to control the breathing speed.

One more thing: when two kinds of loop are used together, four more spaces will be needed at the beginning of the embedded line, so there will have 8 spaces in total for the second loop part, which thus follows the indent rule in the programming.

Save and run to try. We can see the LED only has a “breathe in” process, that is, it changes from dim to bright. Now, let’s add a similar “breathe out” code:

import RPi.GPIO as GPIO

import time

 

led_pin = 17

delay = 0.01                 # delay for LED breathing speed

freq = 100

 

GPIO.setmode(GPIO.BCM)

GPIO.setup(led_pin, GPIO.OUT)

p = GPIO.PWM(led_pin, freq)

p.start(0)

 

def set_led_value(value):

    p.ChangeDutyCycle(100-value)

 

while True:

    for i in range(0, 101):   # Loop "i" from 0 to 100

        set_led_value(i)      # Set led value to i

        time.sleep(delay)     # delay for LED breathing speed

   

    for i in range(100, -1, -1):   # Loop "i" from 100 to 0

        set_led_value(i)      # Set led value to i

        time.sleep(delay)     # delay for LED breathing speed

The code is similar to that for “breathe in”; only difference is the order of the range and step. The parameter step here is step=-1, and the range list is reversely 100~0, not 0~100.

Just save and run it again. Now we will have a completed “breathing” LED. We can adjust its speed by delay.

Next, with what we’ve mastered till here, we can apply this PWM technology for brightness adjustment to try controlling an RGB LED to flash various kinds of color.