Welcome back, schemers!
If you’re just arriving, then here’s a little background on the current project: We’re creating a remote-controlled relay board to switch on and off power to the Western Digital WDTV. In part one of the project, we designed and built a printed circuit board containing the hardware for the project. In this part, we will discuss the source code for the PIC micro that acts as the brains of the operation.
The WDTV remote uses the NEC infrared protocol at 38kHz to transmit commands to the WDTV. This 38kHz stream of blinks is demodulated by the IR receiver to a relatively slow serial datastream which is then piped to the PIC. An example of an NEC datastream is shown below, and was borrowed from here, FYI. There’s a billion different pages on the NEC IR protocol, but this one is kind of nice because all the timings are shown as well as clear differentiation between 1 and 0 states.
Fig 1 – Annotated Example of NEC IR Format
One thing to remember is that the output of the IR module is open-collector. So when you see a logic high in the above waveform, the PIC is actually going to be reading a 0. We’ll refer to this state as MARK, and to the inactive state as SPACE to avoid confusion.
To start out with, our code can simply loop away idly, looking for the input pin to drop from 5v (space) to 0v (mark). At that point, we’re interested. We will first check this leading pulse to make sure it’s about 9mS long, give or take about 10-20% to account for timing variations in the PIC internal oscillator as well as whatever crappy osc is used in the actual remote. If the data stays at mark for 9mS and then goes to space for 4.5mS – we’ve got data!
The NEC stream consists of 4 8-bit bytes. Well, two bytes actually – but they are repeated in normal and inverse mode so it’s best to just read in all 32 bits and then do our deciphering internally. The first two bytes are the device (address) and it’s inverse, and the next two are the command and it’s inverse.
A bit is represented by one mark pulse and one space pulse, and the duration of the space pulse determines whether the bit is a 1 or a 0. Specifically, 565uS of mark followed by 565uS of space signifies a 0. And 565uS of mark followed by 1.685mS of space signifies a 1. So really, once we’re convinced that we’ve got data all we need to do is measure 32 space pulses and set the corresponding bits in our data reg high and low.
That’s pretty simple, so NEC must now complicate matters. If a button is held down on the remote the main command will be sent once, followed by a “repeat” command sent every 109mS. This repeat command consists of the same 9mS of mark used as a start pulse followed by 2.25mS of space and a single 565uS mark.

Fig 2 – NEC Protocol in Repeat
So here’s the (software) scheme.
- Sit around waiting for the start sequence
- Decode 32 bits of data
- If it’s a power cmd, then turn on WDTV if it’s off. Go back to #1
- If WDTV is on and we receive a power command, flag that we now need to wait for 10 repeats. Go back to #1
- If WDTV is on and we’re repeating, watch for 10 repeats and then turn off WDTV.
- If some other command comes in, clear our “repeat” counter and start the whole thing over
Continued on Next Page Jump to Page 2







[...] With all the technology around us there will often come a time when you wish it would work in a slightly different way. Well unless you work as one of the design engineers in the company that made the product chances are you will just have to suck it up and live with what was produced. Well when Matt from Openschemes was in this situation with his Western Digital WDTV he decided to whip up a small circuit to make it work as he wanted. Turns out that the WDTV doesn’t really turn off, it just goes into a mode that looks like it is sleeping. Matt made the WDTV Remote Control External Power Circuit shown above to allow him to remotely turn the darn thing off for real. The board design and construction is well documented, a photo etch method is used etch a copper clad board and the coding of the PIC chip microcontroller can be seen here in this follow-up article. [...]