Improve backlight PWM pin support (#6202)
[jackhill/qmk/firmware.git] / docs / feature_backlight.md
CommitLineData
9b879b12 1# Backlighting
2
98fa82ce 3Many keyboards support backlit keys by way of individual LEDs placed through or underneath the keyswitches. QMK is able to control the brightness of these LEDs by switching them on and off rapidly in a certain ratio, a technique known as *Pulse Width Modulation*, or PWM. By altering the duty cycle of the PWM signal, it creates the illusion of dimming.
9b879b12 4
98fa82ce 5The MCU can only supply so much current to its GPIO pins. Instead of powering the backlight directly from the MCU, the backlight pin is connected to a transistor or MOSFET that switches the power to the LEDs.
9b879b12 6
9b7a3a0b 7## Usage
98fa82ce 8
9b7a3a0b 9Most keyboards have backlighting enabled by default if they support it, but if it is not working for you, check that your `rules.mk` includes the following:
98fa82ce 10
9b7a3a0b 11```make
12BACKLIGHT_ENABLE = yes
13```
14
15You should then be able to use the keycodes below to change the backlight level.
98fa82ce 16
17## Keycodes
9b879b12 18
4c675a83 19|Key |Description |
20|---------|------------------------------------------|
21|`BL_TOGG`|Turn the backlight on or off |
22|`BL_STEP`|Cycle through backlight levels |
cfcf0fd3 23|`BL_ON` |Set the backlight to max brightness |
24|`BL_OFF` |Turn the backlight off |
25|`BL_INC` |Increase the backlight level |
26|`BL_DEC` |Decrease the backlight level |
27|`BL_BRTG`|Toggle backlight breathing |
4931510a 28
9b7a3a0b 29## Caveats
30
31This feature is distinct from both the [RGB underglow](feature_rgblight.md) and [RGB matrix](feature_rgb_matrix.md) features as it usually allows for only a single colour per switch, though you can obviously use multiple different coloured LEDs on a keyboard.
32
b61baf42
BF
33Hardware PWM is supported according to the following table:
34
4d72aa42 35|Backlight Pin|AT90USB64/128|ATmega16/32U4|ATmega16/32U2|ATmega32A|
36|-------------|-------------|-------------|-------------|---------|
37|`B5` |Timer 1 |Timer 1 | | |
38|`B6` |Timer 1 |Timer 1 | | |
39|`B7` |Timer 1 |Timer 1 |Timer 1 | |
40|`C4` |Timer 3 | | | |
41|`C5` |Timer 3 | |Timer 1 | |
42|`C6` |Timer 3 |Timer 3 |Timer 1 | |
43|`D4` | | | |Timer 1 |
44|`D5` | | | |Timer 1 |
45
46All other pins will use software PWM. If the [Audio](feature_audio.md) feature is disabled or only using one timer, the backlight PWM can be triggered by a hardware timer:
47
48|Audio Pin|Audio Timer|Software PWM Timer|
49|---------|-----------|------------------|
50|`C4` |Timer 3 |Timer 1 |
51|`C5` |Timer 3 |Timer 1 |
52|`C6` |Timer 3 |Timer 1 |
53|`B5` |Timer 1 |Timer 3 |
54|`B6` |Timer 1 |Timer 3 |
55|`B7` |Timer 1 |Timer 3 |
56
57When both timers are in use for Audio, the backlight PWM will not use a hardware timer, but will instead be triggered during the matrix scan. In this case, breathing is not supported, and the backlight might flicker, because the PWM computation may not be called with enough timing precision.
9b7a3a0b 58
98fa82ce 59## Configuration
4931510a 60
98fa82ce 61To change the behaviour of the backlighting, `#define` these in your `config.h`:
4931510a 62
98fa82ce 63|Define |Default |Description |
64|---------------------|-------------|-------------------------------------------------------------------------------------------------------------|
65|`BACKLIGHT_PIN` |`B7` |The pin that controls the LEDs. Unless you are designing your own keyboard, you shouldn't need to change this|
317b8095 66|`BACKLIGHT_PINS` |*Not defined*|experimental: see below for more information |
3895b4b8 67|`BACKLIGHT_LEVELS` |`3` |The number of brightness levels (maximum 31 excluding off) |
df251d7a 68|`BACKLIGHT_CAPS_LOCK`|*Not defined*|Enable Caps Lock indicator using backlight (for keyboards without dedicated LED) |
317b8095 69|`BACKLIGHT_BREATHING`|*Not defined*|Enable backlight breathing, if supported |
98fa82ce 70|`BREATHING_PERIOD` |`6` |The length of one backlight "breath" in seconds |
317b8095 71|`BACKLIGHT_ON_STATE` |`0` |The state of the backlight pin when the backlight is "on" - `1` for high, `0` for low |
72
73## Backlight On State
74
5b4187ad 75Most backlight circuits are driven by an N-channel MOSFET or NPN transistor. This means that to turn the transistor *on* and light the LEDs, you must drive the backlight pin, connected to the gate or base, *high*.
76Sometimes, however, a P-channel MOSFET, or a PNP transistor is used. In this case, when the transistor is on, the pin is driven *low* instead.
77
78This functionality is configured at the keyboard level with the `BACKLIGHT_ON_STATE` define.
4931510a 79
b61baf42
BF
80## Multiple backlight pins
81
82Most keyboards have only one backlight pin which control all backlight LEDs (especially if the backlight is connected to an hardware PWM pin).
83In software PWM, it is possible to define multiple backlight pins. All those pins will be turned on and off at the same time during the PWM duty cycle.
84This feature allows to set for instance the Caps Lock LED (or any other controllable LED) brightness at the same level as the other LEDs of the backlight. This is useful if you have mapped LCTRL in place of Caps Lock and you need the Caps Lock LED to be part of the backlight instead of being activated when Caps Lock is on.
85
86To activate multiple backlight pins, you need to add something like this to your user `config.h`:
87
88~~~c
89#define BACKLIGHT_LED_COUNT 2
90#undef BACKLIGHT_PIN
91#define BACKLIGHT_PINS { F5, B2 }
92~~~
93
98fa82ce 94## Hardware PWM Implementation
4931510a 95
98fa82ce 96When using the supported pins for backlighting, QMK will use a hardware timer configured to output a PWM signal. This timer will count up to `ICRx` (by default `0xFFFF`) before resetting to 0.
97The desired brightness is calculated and stored in the `OCRxx` register. When the counter reaches this value, the backlight pin will go low, and is pulled high again when the counter resets.
98In this way `OCRxx` essentially controls the duty cycle of the LEDs, and thus the brightness, where `0x0000` is completely off and `0xFFFF` is completely on.
4931510a 99
98fa82ce 100The breathing effect is achieved by registering an interrupt handler for `TIMER1_OVF_vect` that is called whenever the counter resets, roughly 244 times per second.
101In this handler, the value of an incrementing counter is mapped onto a precomputed brightness curve. To turn off breathing, the interrupt handler is simply disabled, and the brightness reset to the level stored in EEPROM.
eb19fb5b 102
b61baf42
BF
103## Software PWM Implementation
104
105When `BACKLIGHT_PIN` is not set to a hardware backlight pin, QMK will use a hardware timer configured to trigger software interrupts. This time will count up to `ICRx` (by default `0xFFFF`) before resetting to 0.
106When resetting to 0, the CPU will fire an OVF (overflow) interrupt that will turn the LEDs on, starting the duty cycle.
107The desired brightness is calculated and stored in the `OCRxx` register. When the counter reaches this value, the CPU will fire a Compare Output match interrupt, which will turn the LEDs off.
108In this way `OCRxx` essentially controls the duty cycle of the LEDs, and thus the brightness, where `0x0000` is completely off and `0xFFFF` is completely on.
109
110The breathing effect is the same as in the hardware PWM implementation.
111
eb19fb5b
DJ
112## Backlight Functions
113
0cda2f43
PT
114|Function |Description |
115|----------|-----------------------------------------------------------|
116|`backlight_toggle()` |Turn the backlight on or off |
117|`backlight_enable()` |Turn the backlight on |
118|`backlight_disable()` |Turn the backlight off |
119|`backlight_step()` |Cycle through backlight levels |
120|`backlight_increase()` |Increase the backlight level |
121|`backlight_decrease()` |Decrease the backlight level |
122|`backlight_level(x)` |Sets the backlight level to specified level |
123|`get_backlight_level()` |Return the current backlight level |
124|`is_backlight_enabled()`|Return whether the backlight is currently on |
eb19fb5b
DJ
125
126### Backlight Breathing Functions
127
128|Function |Description |
129|----------|----------------------------------------------------------|
130|`breathing_toggle()` |Turn the backlight breathing on or off |
131|`breathing_enable()` |Turns on backlight breathing |
132|`breathing_disable()` |Turns off backlight breathing |