How to Create Breathing LEDs

Last modified by Microchip on 2025/01/02 09:39

Overview

One of the simplest ways to dim LEDs is to use Pulse Width Modulation (PWM). With PWM, the duty cycle (on time) is modulated faster than the human eye can see to create the illusion of brightness. The breathing LED technique uses PWM to increase then decrease the brightness cyclically.

MPLAB Melody CLB Breathing LED Schematic

Figure 1

Information

Note: The PIC16F13145_counter block is a premade module available in the Configurable Logic Block (CLB) tool for convenient access under the "Module" section of MPLAB® Code Configurator (MCC) Melody’s CLB Driver.

MPLAB Melody CLB Modules Selector

Figure 2

Verilog Code for “up_down_PWM” and “counter3” Module

module up_down_PWM ( CLK, pulse, out0, out1, out2 );
input CLK, pulse;
output out0, out1, out2;
reg [2:0] count;
reg direction; // 1 = count down, 0 = count up

assign out0 = count[0];
assign out1 = count[1];
assign out2 = count[2];

// Direction Control
always @(posedge CLK) begin
   if (count == 7)
       direction <= 1;
   else if (count == 0)
       direction <= 0;
end

// Up-down Counter
always @(posedge CLK) begin
   if (pulse == 1) begin
       if (direction == 0)
           count <= count + 1;
       else
           count <= count - 1;
   end
end
endmodule
module counter3 (CLK, out0, out1, out2);
   input CLK;
   output out0, out1, out2;
   reg [2:0] count;

   assign out0 = count[0];
   assign out1 = count[1];
   assign out2 = count[2];

   always @(posedge CLK) begin
       count <= count + 1;
   end
endmodule

Back to Top

Requirements

Procedure

Create the necessary up_down_PWM  and counter3 verilog files in the CLB tool. Refer to the Verilog Code for “up_down_PWM” and “counter3” module sections for the verilog code needed.

Back to Top


Implement the CLB Logic

Configure the CLB to implement the breathing LED logic. Refer to Figure 1 for guidance.

Information

Note: If using MPLAB Code Configurator, use the CLB Synthesizer tool or download the pre-configured CLB file.

Back to Top


Select a Clock Source for the CLB

The clock source is important in this example as it is used to derive the frequency of the PWM and the speed of the breathing effect.

Clock settings:

  • Clock Source: LFINTOSC
  • Clock Divider: 32x

Back to Top


Configure the Look-Up Tables (LUTs)

Click on each LUT and look at properties.

At the bottom of the table, there is a text field for entering a hexadecimal number corresponding to the truth table:

  • LSB Compare – 8CEF
  • MSB Compare – 00B2
  • Clock Pre-Scaler – 007F

Back to Top


Assign the CLB output to the pin where the LED is connected to produce the breathing effect.

Back to Top

Results

In the logic capture in Figure 3, the changing duty cycle is visible in the trace. To reduce the frequency of the breathing effect further, another counter is needed to pre-scale the clock source beyond 16x. If using an external signal or clock source, watch out for synchronization, as the source has to be slower than the base clock of the CLB, or the logic will not function correctly. 

A demonstration highlighting the LED's breathing effect and dynamic duty cycle changes is available for reference:

Breathing LED Output Waveform

Figure 3

Breathing LED Demonstration

Example: Breathing LED demonstration

Back to Top

Learn More

Back to Top