Receiving Infrared Remote Codes
Unlike MakeCode, CircuitPython has some strengths and challenges. Communications between two boards is a challenge but decoding the commands from a remote control, something MakeCode cannot do, can be done in CircuitPython.
This example uses the Adafruit mini remote control. Different remotes send differing remote commands with possibly different encodings (the television industry has fractured the remote encoding methodologies, let's point at them). The Adafruit remote sends codes defined by the TV maker NEC. The codes it sends are known and will be in our program.
If you have another remote, you can learn how to read the codes sent using this guide.
Libraries
Adafruit has developed CircuitPython libraries to read IR signals and to translate them to remote codes. The pulseio library reads pulses in from any source pin. The adafruit_irremote library does the decoding.
The example will use the neopixel library again to light the NeoPixels on the Circuit Playground Express from 0 to 9 depending on the number pressed on the remote. IR codes are represented by a Python list of 20 numbers, called a mapping, where each remote button is in a certain place in a list and we look in that list to find which button was pressed by where the number is in the list.
Code
Here is the code for reading the remote. The example is considerably longer than previous examples in this guide. This is due to comments on the keycodes and the decoding and error checking during the "get key press" process.
# SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Read Adafruit Remote Codes with Circuit Playground Express
#
# Simplified code based on https://learn.adafruit.com/remote-
# control-tree-ornament-with-circuit-playground-express?view=all
#
import adafruit_irremote
import board
import neopixel
import pulseio
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10)
# Set up the reading of pulses on the IR receiver and the IR library
pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
# Example works with the Adafruit mini IR remote #389
# The size below must match what you are decoding! For NEC use 4
received_code = bytearray(4)
# IR Remote Mapping
'''
1: [255, 2, 247, 8]
2: [255, 2, 119, 136]
3: [255, 2, 183, 72]
4: [255, 2, 215, 40]
5: [255, 2, 87, 168]
6: [255, 2, 151, 104]
7: [255, 2, 231, 24]
8: [255, 2, 103, 152]
9: [255, 2, 167, 88]
0: [255, 2, 207, 48]
^ : [255, 2, 95, 160]
v : [255, 2, 79, 176]
> : [255, 2, 175, 80]
< : [255, 2, 239, 16]
Enter: [255, 2, 111, 144]
Setup: [255, 2, 223, 32]
Stop/Mode: [255, 2, 159, 96]
Back: [255, 2, 143, 112]
Vol - : [255, 2, 255, 0]
Vol + : [255, 2, 191, 64]
Play/Pause: [255, 2, 127, 128]
'''
# Use the third value in mappings above to identify each key in a list
keys = [207, 247, 119, 183, 215, 87, 151, 231, 103, 167, 207, 95,
79, 175, 239, 111, 223, 159, 143, 255, 191, 127]
last_command = None
while True:
try:
pulses = decoder.read_pulses(pulsein)
except MemoryError as e:
print("Memory error: ", e)
continue
command = None
try:
code = decoder.decode_bits(pulses, debug=False)
if len(code) > 3:
command = code[2]
print("Decoded:", command)
except adafruit_irremote.IRNECRepeatException: # repeat command
command = last_command
except adafruit_irremote.IRDecodeException: # failed to decode
pass
if not command:
continue
last_command = command
key_pressed = keys.index(command)
if key_pressed <= 9: # if the keys 0-9 pressed
pixels[key_pressed] = (0, 30, 0) # make the neopixel green
else:
pixels.fill((0, 0, 0)) # clear on any non-numeric key
Use
Aim the remote at the Circuit Playground Express. Press any of the number keys and the NeoPixel corresponding to that number will light up. Pressing Enter, Back, or any other button will blank the lights to start again.
For a more involved project using these concepts, see the following Adafruit guides:
Page last edited January 22, 2025
Text editor powered by tinymce.