Fading LED

In last projects, we have used only two output signals: high level and low level (or called 1 & 0, ON & OFF), which is called digital output.

However, in actual use, many devices do not simply ON/OFF to work, for example, adjusting the speed of the motor, adjusting the brightness of the desk lamp, and so on. In the past, a slider that can adjust the resistance was used to achieve this goal, but this is always unreliable and inefficient.

Therefore, Pulse width modulation (PWM) has emerged as a feasible solution to such complex problems. A digital output composed of a high level and a low level is called a pulse. The pulse width of these pins can be adjusted by changing the ON/OFF speed.

Simply put, when we are in a short period (such as 20ms, most people’s visual retention time), Let the LED turn on, turn off, and turn on again, we won’t see it has been turned off, but the brightness of the light will be slightly weaker. During this period, the more time the LED is turned on, the higher the brightness of the LED. In other words, in the cycle, the wider the pulse, the greater the “electric signal strength” output by the microcontroller. This is how PWM controls LED brightness (or motor speed).

There are some points to pay attention to when Pico uses PWM. Let’s take a look at this picture.

_images/pin_pic.png

Each GPIO pin of Pico supports PWM, but it actually has a total of 16 independent PWM outputs (instead of 30), distributed between GP0 to GP15 on the left, and the PWM output of the right GPIO is equivalent to the left copy.

What we need to pay attention to is to avoid setting the same PWM channel for different purposes during programming. (For example, GP0 and GP16 are both PWM_0A)

After understanding this knowledge, let us try to achieve the effect of Fading LED.

Schematic

_images/Hello_LED.png

Wiring

_images/wiring_fading_led.png
  1. Here we use the GP15 pin of the Pico board.

  2. Connect one end (either end) of the 220 ohm resistor to GP15, and insert the other end into the free row of the breadboard.

  3. Insert the anode lead of the LED into the same row as the end of the 220Ω resistor, and connect the cathode lead across the middle gap of the breadboard to the same row.

  4. Connect the LED cathode to the negative power bus of the breadboard.

  5. Connect the negative power bus to the GND pin of Pico.

Note

The color ring of the 220 ohm resistor is red, red, black, black and brown.

Code

When the program is running, the LED will alternate between gradually brightening and gradually dimming.

How it works?

As in the previous project, when using a pin, its input/output mode needs to be defined first. Then we can write values to it, a function analogWrite() in loop() assigns ledPin a PWM wave (brightness),which will add or decrease 5 for each cycle.

analogWrite(ledPin, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

Use an if statement to limit the range of brightness to 0-255, the brightness will accumulate from 0 to 255 and then decrement from 255 to 0, alternating repeatedly.

if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
}