🐼 Tutorial 32 - IR Receiver

The IR Receiver, also known as the Infrared Receiver, is a device that detects infrared signals from an IR remote control and converts them into electrical signals. It is commonly used in consumer electronics, such as TVs, DVD players, and home theater systems, to receive commands from remote controls.

The IR Receiver detects signals in the infrared range, which is the same range of frequencies used by most remote controls. When an IR remote control button is pressed, it sends a unique signal in the form of infrared light that is then detected by the IR Receiver.

The IR Receiver is usually connected to a microcontroller or other device using a digital input pin, and the signal is decoded using an infrared communication protocol such as the NEC protocol or RC5 protocol. The decoded signal is then used to control the device or perform some other action.

The IR Receiver can be used in a wide range of applications, including home automation, robotics, and other electronic devices that require remote control. It is easy to use and cost-effective, making it a popular choice for hobbyists and professionals alike.

Overall, the IR Receiver is a versatile and widely-used device that provides a simple and effective way to receive signals from IR remote controls. Its ease of use and compatibility with a wide range of devices make it a popular choice for hobbyists and professionals alike.

Components Needed

ComponentQuantity
Raspberry Pi Pico W1
Micro USB Cable1
Breadboard1
WiresSeveral
IR Receiver1

Fritzing Diagram

Code

import time
from machine import Pin, freq
from ir_rx.print_error import print_error
from ir_rx.nec import NEC_8

pin_ir = Pin(17, Pin.IN)

def decodeKeyValue(data):
    if data == 0x16:
        return "0"
    if data == 0x0C:
        return "1"
    if data == 0x18:
        return "2"
    if data == 0x5E:
        return "3"
    if data == 0x08:
        return "4"
    if data == 0x1C:
        return "5"
    if data == 0x5A:
        return "6"
    if data == 0x42:
        return "7"
    if data == 0x52:
        return "8"
    if data == 0x4A:
        return "9"
    if data == 0x09:
        return "+"
    if data == 0x15:
        return "-"
    if data == 0x7:
        return "EQ"
    if data == 0x0D:
        return "U/SD"
    if data == 0x19:
        return "CYCLE"
    if data == 0x44:
        return "PLAY/PAUSE"
    if data == 0x43:
        return "FORWARD"
    if data == 0x40:
        return "BACKWARD"
    if data == 0x45:
        return "POWER"
    if data == 0x47:
        return "MUTE"
    if data == 0x46:
        return "MODE"
    return "ERROR"

# User callback
def callback(data, addr, ctrl):
    if data < 0:  # NEC protocol sends repeat codes.
        pass
    else:
        print(decodeKeyValue(data))

ir = NEC_8(pin_ir, callback)  # Instantiate receiver
ir.error_function(print_error)  # Show debug information

try:
    while True:
        pass
except KeyboardInterrupt:
    ir.close()

Code Explanation