It's easy to use the Infrared IR Demodulator Breakout with CircuitPython and the pulseio module. This module allows you to easily write Python code for sending and receiving IR signals with IR remotes.
You can use this driver with any CircuitPython microcontroller board or with a computer that has GPIO, pulseio support and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library. Not all single board computers (SBCs) have pulseio support. Make sure to check if it is supported on your board.
You'll need an IR remote controller to use this example with the receiver:
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 sensor to your board exactly as follows. The following is the receiver wired to a Feather RP2040 with a JST PH cable.
-
Board 3V to receiver JST PH V+ (red wire)
-
Board GND to receiver JST PH GND (black wire)
- Board pin 5 to receiver JST PH Sig (white wire)
The following is the receiver wired to a Feather RP2040 using a solderless breadboard:
-
Board 3V to receiver V+ (red wire)
-
Board GND to receiver GND (black wire)
- Board pin 5 to receiver Sig (white wire)
Python Computer Wiring
Since there are dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired to the receiver using a JST PH cable:
- Pi 3V to receiver JST PH V+ (red wire)
- Pi GND to receiver JST PH GND (black wire)
- Pi GPIO5 to receiver JST PH Sig (white wire)
Here is how you'll wire the receiver to a Raspberry Pi with a breadboard:
- Pi 3V to receiver V+ (red wire)
- Pi GND to receiver GND (black wire)
- Pi GPIO5 to receiver Sig (white wire)
Python Installation of IR Remote Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
pip3 install adafruit-circuitpython-irremote
If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_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
Python Usage
Once you have the library pip3
installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
Example Code
If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
If running Python: The console output will appear wherever you are running Python.
# SPDX-FileCopyrightText: Copyright (c) 2024 ladyada for Adafruit Industries # # SPDX-License-Identifier: MIT import board import pulseio import adafruit_irremote # IR receiver setup ir_receiver = pulseio.PulseIn(board.D5, maxlen=120, idle_state=True) decoder = adafruit_irremote.GenericDecode() def decode_ir_signals(p): codes = decoder.decode_bits(p) return codes while True: pulses = decoder.read_pulses(ir_receiver) try: # Attempt to decode the received pulses received_code = decode_ir_signals(pulses) if received_code: hex_code = ''.join(["%02X" % x for x in received_code]) print(f"Received: {hex_code}") except adafruit_irremote.IRNECRepeatException: # Signal was repeated, ignore pass except adafruit_irremote.IRDecodeException: # Failed to decode signal print("Error decoding")
In the code, if an IR remote command is received, its HEX (hexadecimal) code is printed to the serial monitor.
Text editor powered by tinymce.