A colour in 24 bits
The LED under the key takes a colour as 24 bits on one wire: eight for green, eight for red, eight for blue, in that order. A 1 is a long pulse and a 0 a short one, and the library does the timing. It keeps the colour until it is sent another.
A controller inside the LED
A WS2812 is three LED chips, red, green and blue, and a small controller in one package. The controller listens on its data pin, takes the first 24 bits it hears, and drives each colour to the level those bits name. Then it keeps that colour, with no help from your board, until new data arrives or the power goes.
That is why the light can mean something: a sketch sends red once, and the key stays red while the sketch does other work.
24 bits, green first
Each colour is one byte, a level from 0 to 255. The LED reads them in the
order green, red, blue, most significant bit first. Libraries hide this:
you ask for (red, green, blue) and the library reorders. Adafruit NeoPixel is
told the order with NEO_GRB; MicroPython's neopixel module already sends
green first. Tell a library the wrong order and red and green swap, which is
the first thing When it misbehaves
checks.
Long and short pulses
There is no clock wire. Every bit is a HIGH pulse followed by a LOW, one bit every 1.2 to 1.25 µs, and the LED tells a 1 from a 0 by how long the HIGH lasts. XINGLIGHT's sheet wants a 0 HIGH for at most 0.47 µs and a 1 HIGH for 0.58 to 1 µs. Adafruit NeoPixel on an ESP32 sends 0.4 and 0.8 µs, inside both windows. A LOW of 80 µs or more ends the message and the LED shows what it took.
All 24 bits take under 30 µs. That is why a key can change colour between two reads of BUTTON without the sketch noticing the time.
What it draws
The sheet's figure is 5 mA per colour at full level and 0.5 mA dark. So full red is about 5.5 mA and full white about 15.5 mA: a single LED is nothing to a board's supply, unlike a strip of them.
When it does not work
The library is sending the bytes in the wrong order. This LED reads green first, then red, then blue. In Adafruit NeoPixel that is NEO_GRB in the constructor; MicroPython's neopixel module sends green first by default, so pass it (red, green, blue) as usual.
Setting a pixel only changes the library's copy in memory. Nothing goes down the wire until you call show() in Arduino or write() in MicroPython. Both sketches in this book call it after every change.
Yes: each colour has 256 levels, 0 to 255, and (40, 0, 0) is a dim red. The LED makes the level by switching the colour on and off about 1500 times a second, too fast to see. setBrightness() in Adafruit NeoPixel scales them all.
No. The LED's data output, which would feed the next one, is connected to nothing on this board. Give each TK96 its own data pin, or use a TK33 bar where five LEDs are already chained.
Four wires and a sketch that lights the key red while it is held.
The first press →Edit this page — content/books/mechanical-key/a-colour-in-24-bits.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Mechanical Key and LED
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.