Thirty-two bits of NEC
Half of a NEC frame is a copy of the other half, inverted. That copy is the whole error check, it is why the library hands you two numbers out of four bytes, and it is why every NEC frame takes the same 68 milliseconds.
One frame, field by field
A NEC frame goes out in five parts. A header: 9 ms of carrier, then 4.5 ms of silence. It carries no data; it tells the decoder a frame is starting and gives the receiver's gain control something long to settle on. Then 32 bits: the address, the address inverted, the command, the command inverted. Then one last 560 µs mark, because the final gap needs an end.
IRremote's own source spells the header as 16 and 8 units of 560 µs: 8,960 and 4,480 µs. The round 9 and 4.5 ms are what people quote.
How a bit is sent
Every bit starts with the same 560 µs of carrier. The bit is in the gap that follows: 560 µs for a 0, three times that, 1,680 µs, for a 1. There is no clock, which is why the library talks in microseconds and never in baud.
Why everything goes twice
Infrared is one way. The remote fires and never learns whether anything heard
it, and there is no checksum. So NEC follows each byte with its complement: if
the receiver gets 0x16 and then 0xE9, the pair agrees and the byte is
almost certainly right. If they do not, the frame is thrown away.
It has a side effect. Whatever the payload, the complements make exactly sixteen of the thirty-two bits ones, so every frame is the same 67.8 ms. Drag the command in the figure and the total does not move.
It is also why the library hands you two numbers. IRremote checks each pair and, when it agrees, gives you the 8 bits that carry meaning. When the address pair disagrees it assumes an extended remote and gives you all 16.
Least significant bit first
0x16 is 00010110 on paper and goes out as 01101000. A logic analyser on
SIGNAL shows each byte reversed from what the library printed. Nothing is
wrong; reverse each group of eight. On the TK15's SIGNAL the whole trace is
also upside down, because the receiver is active low: a mark is the line
held LOW.
Holding a button
A held key does not repeat the frame. It sends a repeat frame, 9 ms of carrier, 2.25 ms of silence and a closing mark, about 12 ms in all, every 110 ms, with no data in it. Your sketch decides what that means: volume wants repeats, a power toggle does not. One button, one job is that decision.
Add it up: about nine messages a second, one way. Fine for a remote, hopeless for two boards that need to talk properly; for that, use ESP-NOW.
When it does not work
The bits go out least significant first. Read left to right off a trace, each byte comes out backwards: 0x16 looks like 0x68. Reverse each group of eight, or let the library do it.
The address and the byte after it were not complements, so IRremote treats the two as one 16-bit address: extended NEC. Some remotes really use the extra byte. Record what the library printed rather than assuming eight bits.
Normal. The address names a kind of device, not a serial number, and plain NEC has only 256 of them. Tell two remotes apart by their commands instead.
A frame is about 68 ms and a held key repeats every 110 ms, start to start. That is roughly nine messages a second, one way, with no acknowledgement.
Why the number you wrote down means nothing to the remote in the next drawer.
Codes belong to remotes →Edit this page — content/books/ir-receiver/thirty-two-bits-of-nec.mdx
Questions about this product
See what other owners have asked, and read their solutions.
This page covers several products. Choose yours to see the right 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.