3.18 Breathing LED

3.18.1 Overview

PWM breathing LED utilizes on-board programmable PWM to output analog waveform. After powering on, LED brightness can be adjusted through duty cycle of the waveform to eventually realize a breathing light.

In this way, ambient light can be simulated by changing LED brightness along with time. Also, breathing LED can form a colorful mini light show to construct a tranquil and warm environment.

3.18.2 Working Principle

PWM controls analog output via digital means, which are able to adjust the duty cycle of the wave (a signal circularly shifting between high level and low level).

digital ports of voltage output are LOW and HIGH, which respectively correspond to 0V and 5V. Generally, we define LOW as 0 and HIGH as 1. will output 500 signals of 0 or 1 within 1s. If they are 500 “1”s, 5V will be output. Oppositely, if they are all 0s, the output will be 0V. Or if they are 010101010101…, the average output will be 2.5V. In other words, output ratio of 0 and 1 affects the voltage value.

Honestly, it differs from real continuous output, yet the more 0 and 1 signals are output per unit time, the more accurate the control will be.

6-18

3.18.2 Code Blocks

  1. j10

    This block will increase or decrease one on the value of the variable when it is execute each time. The variable name can be set. There are ++ and – – can be chosen: ++ add 1; – – minus 1.

  2. 1

    This block the brightness of the LED. You need to set LED pin, channel and analog value of brightness(ranging from 0-255).

3.18.3 Test Code

3.18.3.1 Code Flow

6-18

3.18.3.2 Build Code

There are two ways to upload the code: directly open the code file we provide; or manually build blocks.

Directly open the code file we provide:

  1. Click and choose Load from your computer

  1. We have already downloaded the codes on computer desktop, so open the file and choose 3-18-gradientLight.sb3

Manually build blocks:

  1. In events, find j1 block.

  2. In control, find and place j2 block under j1.

6-1-4-1-1

  1. In variable, drag block j7 (keep the default) and put it under j1

6-18

  1. In control, drag j6 and set times to 255, put it in j2

  2. In variable, put j10 in 6-3-3-2-2

6-18

  1. In , put under j10

  2. In variable, find j8 and do not need to modify the name, put it in the analog value of .

  3. Add a delay of 0.01S.

    6-18

  4. Duplicate 6-18, modify ++ in j10 into – –.

Complete Test Code

6-18

3.18.3.3 Test Result

After uploading code, the red LED gradually lights on and dims out, in a circulation. It “breathes” evenly.