It's easy to use the Infrared IR Demodulator Breakout with CircuitPython and the pulseio core module. This module allows you to easily write Python code for sending and receiving pulse signals.
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 LED or IR remote controller to use this example with the demodulator:
This board is specifically for proximity sensing or break-beam projects. It can receive 38KHz IR remote control signals but there isn't a filter system so you'll get a lot of spurious signals.
CircuitPython Microcontroller Wiring
First wire up the sensor to your board exactly as follows. The following is the demodulator wired to a Feather RP2040 with a JST PH cable.
-
Board 3V to demodulator JST PH V+ (red wire)
-
Board GND to demodulator JST PH GND (black wire)
- Board pin 5 to demodulator JST PH Sig (white wire)
The following is the demodulator wired to a Feather RP2040 using a solderless breadboard:
-
Board 3V to demodulator VIN (red wire)
-
Board GND to demodulator GND (black wire)
- Board pin 5 to demodulator 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 demodulator using a JST PH cable:
- Pi 3V to demodulator JST PH V+ (red wire)
- Pi GND to demodulator JST PH GND (black wire)
- Pi GPIO5 to demodulator JST PH Sig (white wire)
Here is how you'll wire the demodulator to a Raspberry Pi with a breadboard:
- Pi 3V to demodulator VIN (red wire)
- Pi GND to demodulator GND (black wire)
- Pi GPIO5 to demodulator SIG (white wire)
Python Installation
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!
CircuitPython Usage
To use with CircuitPython, 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 code.py file in a zip file. Extract the contents of the zip file, and the code.py file to your CIRCUITPY drive.
Only core modules are used for this example. No additional libraries need to be added to the /lib folder.
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 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import pulseio import board pulses = pulseio.PulseIn(board.D5) pulse = False pulse_count = 0 while True: # Wait for an active pulse while len(pulses) == 0: if pulse: pulse = False if len(pulses) != 0 and not pulse: pulse_count += 1 print(f"pulses detected {pulse_count} times") pulse = True # Pause while we do something with the pulses pulses.pause() # Print the pulses. pulses[0] is an active pulse unless the length # reached max length and idle pulses are recorded. print(pulses[0]) # Clear the rest pulses.clear() # Resume with an 80 microsecond active pulse pulses.resume(80)
In the code, if an IR pulse is detected, its pulses are printed to the serial monitor. An additional variable pulse_count
keeps track of how many times a new pulse is detected. The pulse
state tracks whether a pulse input is detected or not by the demodulator. You can use this code as a template for using the demodulator as a proximity sensor or break-beam.
Here is the output using an IR remote with the demodulator:
Here is the output using an IR LED emitter with the modulator:
Text editor powered by tinymce.