IRRELib is a library for working with IR (infrared) receivers and processing remote control commands. The library handles the NEC infrared remote control protocol used in wide variete of devices today. The code is written specifically for ATtiny85 and is part of the Tinusaur project. As a receiver in the examples is used the TSOP4838 by Vishay.

Sending and Receiving

The transmission of the IR commands requires only a microcontroller and an infrared LED, available from a wide variety of sources. The easiest way to send commands is to use a general purpose (universal) remote control.

The reception of the modulated commands for the NEC protocols is easily accomplished with specialized IR receivers, most readily available from Sharp Corporation and Vishay Intertechnology. These receivers include a photo-diode, an automatic gain control (AGC) circuit, and a demodulator. The demodulated signal is then decoded with a microcontroller.

TSOP4838 Receiver

VISHAY TSOP4838 Infrared Receiver, Remote Control, 38 kHzAs part of the development of this library, an infrared receiver is used – more specifically the Vishay TSOP4838 that works at  38 kHz.

The TSOP4838 are miniaturized IR receiver modules for infrared remote control systems. A PIN diode and a preamplifier are assembled on lead frame, the epoxy package contains an IR filter. The demodulated output signal can be directly connected to a microprocessor for decoding. They are optimized to suppress almost all spurious pulses from Wi-Fi and CFL sources. They may suppress some data signals if continuously transmitted.

More technical information about the TSOP4838 receiver as PDF is available at:

The Protocol

There are many protocols for sending and receiving IR commands.

The most commonly used protocol is the NEC protocol, which specifies a carrier frequency of 38 kHz. The NEC protocol is used by the vast majority of Japanese-manufactured consumer electronics. In the NEC protocol, each manufacturer is assigned a unique code that is contained in the transmitted command, avoiding the possibility of false triggering by other remote handsets.

This library uses the NEC protocol.

The received data consists of 32 bits or 4 bytes where

  • 1st byte is the device id
  • 2nd byte is the same id but inverted
  • 3rd byte is the command code
  • 4th byte is the command but inverted

It is important to note that not all transmitters (remote controls) follow that format – sometimes the supposed inverted bytes contain other data instead.

NEC Transmission Code
NEC Transmission Code

More technical information about the communication protocols as PDF is available at:

The Library

The library is receiving the commands the calls a user defined function every time there is a command providing a data structure (rather a pointer to it) that contains the data – the 4 bytes.

Here is a source code fragment showing the user defined receiving function

void irrelib_received_userfunc(irrelib_data_p received_data) {
    switch ((*received_data).status) {
        case IRRELIB_STATUS_OK:
            // Every time a command is received ...
            PORTB ^= (1 << LED5_PORT);  // Flip the bit
            // Every time a command is repeated ...
            // Every time there is an error ...

The command is accessible through a (*received_data).com_id expression.

Since this library (the projects) is work in progress the best ways to see how it works and how to use it is to look at the source code – it has a lot of comments. It also has a sample testing program that lights up some LED’s upon receiving commands.

External Resources

The source code of the IRRELib library is available at:


As it is mentioned in the source code  part of this work was inspired by and based on the “ATtiny85 IR Remote Control Receiver” by David Johnson-Davies
IR Remote Control Receiver:
IR Remote Control Tool (NEC):

Other projects and useful links

Consumer IR

NEC Protocol IR (Infrared) Remote Control With a Microcontroller

NEC protocol compatible infrared remote decoder lib for Atmel AVR

TinyPCRemote – An ATtiny85 Based Infrared PC remote control


How to use the IR library with an attiny

ATtiny & IR remote control Wiebelbot

Infrared Dedicated Decoder

Scavenger Hunt Beacon

Rickrolling remote control prank

Rickrolling remote control prank

An ATTiny85 Home Automation IR Device

An ATTiny85 Home Automation IR Device