It's easy to use the STEMMA Reflective Photo Interrupt Sensor with CircuitPython and the digitalio core module. This module allows you to easily write Python code for accessing basic digital inputs and outputs.
You can use this driver with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython Microcontroller Wiring
First wire up the sensor to your board exactly as follows. The following is the sensor wired to a Feather RP2040 with a JST PH cable.
-
Board 3V to sensor JST PH VIN (red wire)
-
Board GND to sensor JST PH GND (black wire)
- Board pin 5 to sensor JST PH SIG (white wire)
The following is the switch wired to a Feather RP2040 using a solderless breadboard:
-
Board 3V to sensor VIN (red wire)
-
Board GND to sensor GND (black wire)
- Board pin 5 to sensor 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 sensor using a JST PH cable:
- Pi 3V to sensor JST PH VIN (red wire)
- Pi GND to sensor JST PH GND (black wire)
- Pi GPIO5 to sensor JST PH SIG (white wire)
Here is how you'll wire the sensor to a Raspberry Pi with a breadboard:
- Pi 3V to sensor VIN (red wire)
- Pi GND to sensor GND (black wire)
- Pi GPIO5 to sensor SIG (white wire)
CircuitPython Usage
To use with CircuitPython, you need to update code.py with the example script.
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 copy the code.py file to your CIRCUITPY drive.
No additional libraries are needed in the /lib folder for this example.
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: 2023 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from digitalio import DigitalInOut, Direction, Pull ir = DigitalInOut(board.D5) ir.direction = Direction.INPUT ir.pull = Pull.UP while True: if not ir.value: print("object detected!") else: print("waiting for object...") time.sleep(0.01)
The SIG output from the sensor is setup as a digital input. In the loop, if an input is detected object detected!
is printed to the serial monitor. Otherwise waiting for object...
is printed. You can experiment with the potentiometer position to increase and decrease the sensitivity of the sensor.
Text editor powered by tinymce.