Gamma Correction for Pulsing LED’s

Gamma correction by itself might immediately read as a somewhat boring looking topic to educate yourself on, and that may be true. I had heard about it a few times in the past, and honestly didn’t take much note of it because it applies to such a mundane and low priority task. That task is blinking an LED. While for many people it’s their favorite visual output when programming a microcontroller project, most will not consider the style or technique used for blinking an LED.


What is Gamma Correction?


Gamma correction applies to pulsing an LED rather than simply blinking one. Pulsing in the sense that the LED is off and then the brightness is slowly ramped up to 100% then back to 0% to create a pulsing effect. I won’t go into a full blown explanation about the math behind gamma correction, you can read that here. All you need to know is the human eye doesn’t perceive brightness in a linear fashion. When we program our LED to pulse on and off in a linear fashion we don’t see a gradual transition. We need to ramp up/down the brigtness in a non-linear fashion that mimics our eyes to see a more uniform pulse from 0-100%


Implementing Gamma Correction


The most common approach to performing gamma correction is by the use of a look-up table. The table contains the pre-calculated values needed to set the PWM output to get a gamma corrected LED pulse. Since the formula for calculating the correction values is an exponential function it is a processor intensive process. It makes more sense to pre-calculate the values and store them in an array in the code.

The Gamma correction look-up table can be generated automatically with the script below. All you need to do is set the three variables based on your specific application. Your specific Gamma value, number of steps from LED off to full brightness and maximum PWM value based on the PWM resolution. For example, an 8-bit PWM would have a maximum value of 255 and the generated lookup table would range from 0-255.

Copy the below script and paste it into your own Python interpreter, or use the online one linked below.



Two things to watch out for in this script. In printing out the lookup table, the last comma needs to be deleted. Just lazy programming there, but you could add a check where if the loop is on its last iteration then print the value without a final comma. Lastly, the variable type used. In the code below the resolution is 10-bit so uint16_t is used for gamma_table. If you had an 8-bit or less resolution on you PWM you could go with a uint8_t which would save you precious memory.

If this script is run with the above parameters, the result is the below look-up table code that will provide gamma correction in pulsing an LED.



The Result

Since I only have 1 of the MPLAB Xpress development boards I couldn’t do a side-by-side comparison to see the difference. The next best thing is a video, and with a little editing magic I patched the videos together and synced the pulse. I think the result is pretty good!




Lastly, If you want to quickly try for yourself, and have a MPLAB Xpress development board handy here is a link to the project…


Leave a Reply

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