Gamma Correction

Most of your senses experience the external world in a logarithmic manner. We are all familiar with power measurements and the logarithmic basis of decibels. Well, our eyes also respond to outside stimulus in the same manner. If you are playing with these serial LEDs, and you reduce the binary value sent to a single color register by half, you will notice that the LED does not look half as bright as previously. The logarithmic response of your eyes is the reason why.

To compensate for this, a method called “gamma correction” is used. This method takes an arithmetically linear set of numbers (the binary numbers you want to send to your color registers) and converts them to a “corrected” set of numbers. This set of numbers is generally placed in an array or some other language structure that can be used as a “look-up table”. You use the binary intensity number as an index into the table to recover the “correct” value. That value is then sent to the LED color register.

I found a WEB site that has a pretty good explanation of what is going on. This site has also printed out a table you can use directly in your code. Their table was generated with a gamma correction factor of 2.8. It seems that someone has done their homework fairly well as this table of values seems to work well on my LEDs. They also have a short program written in the “Processing” language (new one on me) so that you can generate your own gamma tables.

I don’t know anything about the “Processing” language. It looks a lot like C. I really do not want to install a new compiler an learn its usage just to generate these tables. Everyone who runs Linux has python installed. So I rewrote it in python and added the ability to accept a command line argument for the correction factor. Just run it as " N.N". N.N is a floating point number composed of 2 single-digit values from 1.0 to 9.9. It creates a file named “gamma_N-N.tbl”. There is also an python script to convert this table to a hexadecimal version of index/value pairs. This table can be used to manually find values if you want to fat-finger numbers into the circuit as demonstrated in the video. It takes a “gamma_N-N.tbl” file and generates a “gammma_N.N_hex.tbl” file. Both of these scripts are included in the download.

Breadboard Schematic

I‘ve added a 4×4 keypad to the the breadboard to facilitate entering specific colors into the LEDs. Here is the schematic:

Breadboard Schematic

C 4×4 Keypad Driver

Included on the breadboard is a 4×4 matrix keypad. This one has extra functions on the keys beyond the typical 0-F hexidicmal keypads. Of course, you have to support those functions in your driver code. So take a look at the included C code (download below). You may be able to use this driver with just a little modification on any project. This is one time that I appreciated writing in C. I know the driver if written in assembly would be a lot more difficult to read, understand, and maintain. But in this case the C saved me a lot of time. This was my first project of any significance using C on a microcontroller. I was suprised how much I had forgotten when it came to driving hardware with C. But it was fun getting back into the swing of things. This does not make me a C convert in general, however. This project does not have time critical requirements for the keypad. And the data management for the LED color registers is not time critical either. That, too, no doubt was easier to write in C. But the critical part, the actual driving of the LED string, had to be written in assembly. There is no way you can possibly write to the LED color registers using any other method. So, just goes to show you, assembly language can save your butt. It’s a great skill to maintain.


Included is the C driver for this video, breadboard schematic in png, and the two python scripts for gamma table creation.

Download WS2812 Serial LED Project - zip: