The first thing we're going to do is test IR receive with a NEC remote. NEC is a electronics manufacturer, one of several, that defined their own IR coding scheme which has also been used by other folks in products. You can try any remotes you have sitting around the house (although they might use an encoding other than NEC). We have this handy little one available in the store which we're going to use for our test.

Mini Remote Control with 21 buttons
This little remote control would be handy for controlling a robot or other project from across the room. It has 21 buttons and a layout we thought was handy: directional buttons and...
$4.95
In Stock

Copy the following code to your code.py:

# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import pulseio
import board
import adafruit_irremote

# Create a 'pulseio' input, to listen to infrared signals on the IR receiver
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
# Create a decoder that will take pulses and turn them into numbers
decoder = adafruit_irremote.GenericDecode()

while True:
    pulses = decoder.read_pulses(pulsein)
    try:
        # Attempt to convert received pulses into numbers
        received_code = decoder.decode_bits(pulses)
    except adafruit_irremote.IRNECRepeatException:
        # We got an unusual short code, probably a 'repeat' signal
        # print("NEC repeat!")
        continue
    except adafruit_irremote.IRDecodeException as e:
        # Something got distorted or maybe its not an NEC-type remote?
        # print("Failed to decode: ", e.args)
        continue

    print("NEC Infrared code received: ", received_code)
    if received_code == [255, 2, 255, 0]:
        print("Received NEC Vol-")
    if received_code == [255, 2, 127, 128]:
        print("Received NEC Play/Pause")
    if received_code == [255, 2, 191, 64]:
        print("Received NEC Vol+")

We create the pulsein object to listen for infrared signals on the IR receiver. Then we create the decoder object to take the pulses and turn them into numbers.

Then we take the decoder object and attempt to convert the received pulses into numbers. There's two errors we check for and tell the code to continue running if they're encountered.

One possible decoding error is an unusually short code, which is probably an NEC repeat signal. If you hold down a remote button, the remote control may 'save effort and time' by  sending a short code that means "keep doing that". For example, holding down the volume button to quickly increase or decrease the volume on a TV. We don't handle those repeat codes in this project, we're only looking for unique button presses

The second possible decoding error is when it fails to decode, which can mean the signal got distorted or you're not using a NEC remote.

Then we print the code we receive from the remote. If we receive the codes from the first three buttons on the remote, we print which button was pressed.

If you're using your own remote, you can check the serial console to find the codes you're receiving. They'll be printed after NEC Infrared code received:. Then, you can change code.py to reflect the specific button codes for your remote.

This guide was first published on Jul 24, 2018. It was last updated on Jul 24, 2018.

This page (IR Test with Remote) was last updated on Jun 05, 2023.

Text editor powered by tinymce.