It's easy to use the Infrared IR Remote Transceiver with CircuitPython and the Adafruit_CircuitPython_IRRemote module. This module allows you to easily write Python code that allows you to send and receive IR remote pulses using the pulseio core module.
You'll need an IR remote controller to use this example:
This receiver on this board is specifically for receiving 38KHz IR remote control signals - it isn't going to work for proximity/distance sensing or other frequency signals.
CircuitPython Microcontroller Wiring
First, wire up the transceiver to your board exactly as shown below. Here's an example of wiring a Feather RP2040 to the transceiver using one of the handy STEMMA JST PH cables:
- Board 3V to VIN (red wire)
- Board GND to GND (black wire)
- Board pin 5 to IRin (white wire)
- Board pin 6 to IRout (green wire)
You can also use standard 0.100" pitch headers to wire it up on a breadboard:
- Board 3V to VIN (red wire)
- Board GND to GND (black wire)
- Board pin 5 to IRin (white wire)
- Board pin 6 to IRout (green wire)
CircuitPython Usage
To use with CircuitPython, you need to first install the IRRemote library into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary library and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following file:
- adafruit_irremote.mpy
Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import array
import pulseio
import board
import adafruit_irremote
# Create a 'PulseOut' to send infrared signals on the IR transmitter @ 38KHz
pulseout = pulseio.PulseOut(board.D6, frequency=38000, duty_cycle=2**15)
# Create an encoder that will take numbers and turn them into NEC IR pulses
encoder = adafruit_irremote.GenericTransmit(header=[9000, 4500],
one=[560, 1700],
zero=[560, 560],
trail=0)
# IR receiver setup
ir_receiver = pulseio.PulseIn(board.D5, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
while True:
pulses = decoder.read_pulses(ir_receiver)
try:
# Attempt to decode the received pulses
received_code = decoder.decode_bits(pulses)
if received_code:
hex_code = ''.join(["%02X" % x for x in received_code])
print(f"Received: {hex_code}")
# Convert pulses to an array of type 'H' (unsigned short)
pulse_array = array.array('H', pulses)
# send code back using original pulses
pulseout.send(pulse_array)
print(f"Sent: {pulse_array}")
except adafruit_irremote.IRNECRepeatException: # Signal was repeated, ignore
pass
except adafruit_irremote.IRDecodeException: # Failed to decode signal
print("Error decoding")
ir_receiver.clear() # Clear the receiver buffer
time.sleep(1) # Delay to allow the receiver to settle
print()
The code begins by creating pulseio objects on pin D5 and D6. These will send and receive infrared signals at 38KHz. Then, emitter and decoder objects are created with the adafruit_irremote library.
In the loop, if an IR remote command is received, its HEX (hexadecimal) code is printed to the serial monitor. Then, that same command is sent via the IR LEDs as an array. ir_receiver.clear() is called to clear the buffer so that the receiver does not go into an infinite loop of receiving the code sent by the IR LEDs.
Page last edited January 22, 2025
Text editor powered by tinymce.