IR remote/What the remote is saying/05. Thirty-two bits of NEC
What the remote is saying · 05 of 11

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

Thirty-two bits, sixteen of them a check
67.8 ms
Command byte (which button)0x16
Every frame
67.8 ms
Bits on the wire
32
Bits of meaning
16
Silence. Between frames the line sits high and the receiver is idle. A one takes three times as long as a zero — and yet the total at the top does not move as you drag the slider. It cannot: every byte is sent with its complement, so whatever you choose, exactly sixteen of the thirty-two bits are ones. Every NEC frame ever sent takes the same 67.8 ms. That is also why the library talks in microseconds everywhere and never about a baud rate: there is no clock here, only durations.

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

My logic analyser capture does not match the command

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 library reports a 16-bit address

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.

Two different remotes report the same address

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.

How many presses a second can it carry?

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.

Where this goes next

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

Community

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.

Browse Modules and blocks on the forum