Sub topic 10
Making Fades Look Even with Gamma Correction
This chapter assumes that you know basic Arduino sketches, setup() and loop(), pinMode(), analogWrite(), and how PWM controls an LED. You should also understand that an Arduino Uno uses PWM values from 0 to 255.
Why linear PWM looks uneven
A simple fade increases the PWM value by the same amount each time:
0, 1, 2, 3, ... 255This is a linear change in the electrical signal. However, human vision does not respond linearly to light. A change from PWM 10 to 20 can look much larger than a change from 200 to 210, even though both changes are 10 counts.
At low brightness, the eye is sensitive to small changes. At high brightness, it is less sensitive to the same numerical change. A fade made from raw PWM values may therefore appear to stay dark for too long and then become bright quickly near the end.
Gamma correction adjusts the PWM value before it reaches the LED. It makes equal steps in the requested brightness look more nearly equal to a person watching the LED.
The gamma curve
A common correction uses a gamma value of about 2.2:
corrected PWM = (requested level / 255) ^ 2.2 × 255The requested level represents the brightness you want the viewer to perceive. The corrected PWM level is the electrical value sent to analogWrite().
For example, a requested level of 128 is halfway through the input range. With gamma correction, it produces a PWM value of about 56 instead of 128. The lower electrical output compensates for the eye's response.
The exact best value depends on the LED, diffuser, surrounding light, and viewer. A gamma value between 2.0 and 2.4 is a useful starting point.
Applying gamma correction in code
The pow() function raises a number to a power. The following sketch treats level as the visually desired brightness and converts it before writing to the LED.
#include <math.h>
const uint8_t LED_PIN = 9;
const float GAMMA = 2.2;
uint8_t gammaCorrect(uint8_t level) {
float normalized = level / 255.0;
float corrected = pow(normalized, GAMMA) * 255.0;
return (uint8_t)(corrected + 0.5);
}
int level = 0;
int step = 1;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
analogWrite(LED_PIN, gammaCorrect(level));
if (level + step < 0 || level + step > 255) {
step = -step;
}
level += step;
delay(8);
}The + 0.5 before conversion rounds the floating-point result instead of always discarding its fractional part. The result is then stored in an unsigned 8-bit value, uint8_t, which can hold values from 0 through 255.
The fade still changes level linearly. Only the value passed to analogWrite() is curved. This separation is useful because the rest of your program can continue to work with a simple 0-to-255 brightness scale.
Choosing and testing a gamma value
Gamma correction is a model, not a guarantee. A value of 2.2 may look good with one LED and diffuser but too dark with another. Test several values, such as 1.8, 2.2, and 2.4.
A lower gamma produces higher PWM values in the middle of the range. A higher gamma produces lower middle-range values and usually makes the fade appear darker for longer.
Gamma correction also does not fix hardware problems. Use a suitable resistor, keep the LED within the pin's current limits, and use a transistor or driver for larger loads.
Exercise
Why does gamma correction usually produce a PWM value below 128 when the requested brightness level is 128?
Show answer
Human vision responds non-linearly to light. Raising the normalized level to a gamma greater than 1 reduces the electrical output in the middle of the range, making the perceived brightness closer to halfway.