Fading LED with PIC microcontroller

This article shows how to create a fading/dimming effect of the LED using PIC microcontroller. Controlling LED brightness using PWM is a relatively simple task and can be accomplished by configuring PIC16F877  CCP module to generate varying PWM output by slowly changing its duty cycle setting from about 97% to 2%.  The code is written in assembly language and basically uses 2 loops to gradually increase the pulse duty cycle until MAX intensity is reached and then decrease it with the same rate. Currently it takes about 1 second to get a maximum of LED power. The fading rate can be changed by editing the DelayS function. Circuit schematic and a prototype board are shown next.

The source code can be downloaded here. I’ve tested this code only on PIC16F877 however it can be easily modified for similar microcontrollers.Use In-Circuit Debugger like PICkit 3 to program your target drvice. High efficiency 3mm LED can be driven directly from PIC output, so no need for additional output transistor. I’ve also made a short video clip demonstrating circuit operation at the bottom of the page. If you look closely you can see gradual changes in LED brightness.

Posted in PIC16F87X Projects

Leave a Reply

Your email address will not be published. Required fields are marked *