]> git.donarmstrong.com Git - qmk_firmware.git/blob - docs/feature_backlight.md
Correct backlight on state docs (#6358)
[qmk_firmware.git] / docs / feature_backlight.md
1 # Backlighting
2
3 Many 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.
4
5 The 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.
6
7 ## Usage
8
9 Most 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:
10
11 ```make
12 BACKLIGHT_ENABLE = yes
13 ```
14
15 You should then be able to use the keycodes below to change the backlight level.
16
17 ## Keycodes
18
19 |Key      |Description                               |
20 |---------|------------------------------------------|
21 |`BL_TOGG`|Turn the backlight on or off              |
22 |`BL_STEP`|Cycle through backlight levels            |
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                |
28
29 ## Caveats
30
31 This 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
33 Hardware PWM is only supported on certain pins of the MCU, so if the backlighting is not connected to one of them, a software PWM implementation triggered by hardware timer interrupts will be used.
34
35 Hardware PWM is supported according to the following table:
36
37 | Backlight Pin | Hardware timer          |
38 |---------------|-------------------------|
39 |`B5`           | Timer 1                 |
40 |`B6`           | Timer 1                 |
41 |`B7`           | Timer 1                 |
42 |`C6`           | Timer 3                 |
43 |`D4`           | Timer 1 (ATmega32A only)|
44 | other         | Software PWM            |
45
46 The [audio feature](feature_audio.md) also uses hardware timers. Please refer to the following table to know what hardware timer the software PWM will use depending on the audio configuration:
47
48 | Audio Pin(s) | 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 | `Bx` & `Cx`  | Timer 1 & 3 | None               |
57
58 When all timers are in use for [audio](feature_audio.md), the backlight software PWM will not use a hardware timer, but instead will be triggered during the matrix scan. In this case the backlight doesn't support breathing and might show lighting artifacts (for instance flickering), because the PWM computation might not be called with enough timing precision.
59
60 ## Configuration
61
62 To change the behaviour of the backlighting, `#define` these in your `config.h`:
63
64 |Define               |Default      |Description                                                                                                  |
65 |---------------------|-------------|-------------------------------------------------------------------------------------------------------------|
66 |`BACKLIGHT_PIN`      |`B7`         |The pin that controls the LEDs. Unless you are designing your own keyboard, you shouldn't need to change this|
67 |`BACKLIGHT_PINS`     |*Not defined*|experimental: see below for more information                                                                 |
68 |`BACKLIGHT_LEVELS`   |`3`          |The number of brightness levels (maximum 31 excluding off)                                                   |
69 |`BACKLIGHT_CAPS_LOCK`|*Not defined*|Enable Caps Lock indicator using backlight (for keyboards without dedicated LED)                             |
70 |`BACKLIGHT_BREATHING`|*Not defined*|Enable backlight breathing, if supported                                                                     |
71 |`BREATHING_PERIOD`   |`6`          |The length of one backlight "breath" in seconds                                                              |
72 |`BACKLIGHT_ON_STATE` |`0`          |The state of the backlight pin when the backlight is "on" - `1` for high, `0` for low                        |
73
74 ## Backlight On State
75
76 Most 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*.
77 Sometimes, 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.
78
79 This functionality is configured at the keyboard level with the `BACKLIGHT_ON_STATE` define.
80
81 ## Multiple backlight pins
82
83 Most keyboards have only one backlight pin which control all backlight LEDs (especially if the backlight is connected to an hardware PWM pin).
84 In 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.
85 This 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.
86
87 To activate multiple backlight pins, you need to add something like this to your user `config.h`:
88
89 ~~~c
90 #define BACKLIGHT_LED_COUNT 2
91 #undef BACKLIGHT_PIN
92 #define BACKLIGHT_PINS { F5, B2 }
93 ~~~
94
95 ## Hardware PWM Implementation
96
97 When 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.
98 The 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.
99 In 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.
100
101 The 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.
102 In 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.
103
104 ## Software PWM Implementation
105
106 When `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.
107 When resetting to 0, the CPU will fire an OVF (overflow) interrupt that will turn the LEDs on, starting the duty cycle.
108 The 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.
109 In 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.
110
111 The breathing effect is the same as in the hardware PWM implementation.
112
113 ## Backlight Functions
114
115 |Function  |Description                                                |
116 |----------|-----------------------------------------------------------|
117 |`backlight_toggle()`    |Turn the backlight on or off                 |
118 |`backlight_enable()`    |Turn the backlight on                        |
119 |`backlight_disable()`   |Turn the backlight off                       |
120 |`backlight_step()`      |Cycle through backlight levels               |
121 |`backlight_increase()`  |Increase the backlight level                 |
122 |`backlight_decrease()`  |Decrease the backlight level                 |
123 |`backlight_level(x)`    |Sets the backlight level to specified level  |
124 |`get_backlight_level()` |Return the current backlight level           |
125 |`is_backlight_enabled()`|Return whether the backlight is currently on |
126
127 ### Backlight Breathing Functions
128
129 |Function  |Description                                               |
130 |----------|----------------------------------------------------------|
131 |`breathing_toggle()`  |Turn the backlight breathing on or off        |
132 |`breathing_enable()`  |Turns on backlight breathing                  |
133 |`breathing_disable()` |Turns off backlight breathing                 |