Code
Once you've finished setting up your Circuit Playground Express with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download to your computer as a zipped folder.
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import adafruit_irremote
import board
import neopixel
import pulseio
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10)
pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
last_command = None
brightness_up = 95 # Up arrow
brightness_down = 79 # Down arrow
command_to_color = { # button = color
247: (255, 0, 0), # 1 = red
119: (255, 40, 0), # 2 = orange
183: (255, 150, 0), # 3 = yellow
215: (0, 255, 0), # 4 = green
87: (0, 255, 120), # 5 = teal
151: (0, 255, 255), # 6 = cyan
231: (0, 0, 255), # 7 = blue
103: (180, 0, 255), # 8 = purple
167: (255, 0, 20), # 9 = magenta
207: (255, 255, 255), # 0 = white
127: (0, 0, 0), # Play/Pause = off
}
while True:
pulses = decoder.read_pulses(pulsein, max_pulse=5000)
command = None
try:
code = decoder.decode_bits(pulses)
if len(code) > 3:
command = code[2]
print("Decoded:", command)
print("-------------")
except adafruit_irremote.IRNECRepeatException: # Catches the repeat signal
command = last_command
except adafruit_irremote.IRDecodeException: # Failed to decode
pass
if not command:
continue
last_command = command
if command == brightness_up:
pixels.brightness += 0.1
elif command == brightness_down:
pixels.brightness -= 0.1
elif command in command_to_color:
pixels.fill(command_to_color[command])
Upload the Code and Libraries to the Circuit Playground Express
After downloading the Project Bundle, plug your Circuit Playground Express into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Feather Prop Maker's CIRCUITPY drive.
- lib folder
- code.py
Your Circuit Playground Express CIRCUITPY drive should look like this after copying the lib folder and code.py file:
How the CircuitPython Code Works
At the top of the code, we import the four libraries we'll be using in our code. Then we setup use of those libraries. First we assign last_command for use later. Then, we assign brightness_up to the IR command code associated with the up arrow on the IR remote, and brightness_down to the code for the down arrow.
import adafruit_irremote import board import neopixel import pulseio pixels = neopixel.NeoPixel(board.NEOPIXEL, 10) pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True) decoder = adafruit_irremote.GenericDecode() last_command = None brightness_up = 95 # Up arrow brightness_down = 79 # Down arrow
Commands Dictionary
The keys are the IR codes for the eleven buttons we're using and the values are their associated (r, g, b) tuples. NeoPixel colors are represented using red, green and blue in values of 0 - 255 to determine the amount of a given color. We've used comments on each line to identify which button on the remote and assigned color the dictionary is referring to.
command_to_color = { # button = color
247: (255, 0, 0), # 1 = red
119: (255, 40, 0), # 2 = orange
183: (255, 150, 0), # 3 = yellow
215: (0, 255, 0), # 4 = green
87: (0, 255, 120), # 5 = teal
151: (0, 255, 255), # 6 = cyan
231: (0, 0, 255), # 7 = blue
103: (180, 0, 255), # 8 = purple
167: (255, 0, 20), # 9 = magenta
207: (255, 255, 255), # 0 = white
127: (0, 0, 0), # Play/Pause = off
}
The Loop
The first two sections of code inside the loop are designed to read the incoming IR signals, decode them, and prepare them for practical use.
To simplify the amount of IR noise, the line if len(code) > 3: says the signal must be longer longer than three values before we do anything with it. The decoded signal from each button on this remote is four numbers in a list format: [0, 0, 0, 0]. You need only the third number from that list. When we get a code of the correct length, we assign command to be the third value from the list by assigning command = code[2].
The two print statements are here to identify the command code for the unused buttons on the remote. Some remotes will have different command. Reference the commands in the REPL output to update and change the values in the commands dictionary.
while True:
pulses = decoder.read_pulses(pulsein, max_pulse=5000)
command = None
try:
code = decoder.decode_bits(pulses)
if len(code) > 3:
command = code[2]
print("Decoded:", command)
print("-------------")
except adafruit_irremote.IRNECRepeatException: # Catches the repeat signal
command = last_command
except adafruit_irremote.IRDecodeException: # Failed to decode
pass
if not command:
continue
last_command = command
if command == brightness_up:
pixels.brightness += 0.1
elif command == brightness_down:
pixels.brightness -= 0.1
elif command in command_to_color:
pixels.fill(command_to_color[command])
Page last edited February 23, 2026
Text editor powered by tinymce.