Using the STEMMA Reflective Photo Interrupt Sensor with Arduino involves wiring up the sensor to your Arduino-compatible microcontroller and running the provided example code.
Wiring
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the breakout VIN.
Here is an Adafruit Metro wired up to the sensor using a JST PH cable.
-
Board 5V 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)
Here is an Adafruit Metro wired up using a solderless breadboard:
-
Board 5V to sensor VIN (red wire)
-
Board GND to sensor GND (black wire)
- Board pin 5 to sensor SIG (white wire)
No additional libraries are needed for the example code.
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries // // SPDX-License-Identifier: MIT void setup() { //start serial connection Serial.begin(115200); pinMode(5, INPUT_PULLUP); } void loop() { int sensorVal = digitalRead(5); if (sensorVal == LOW) { Serial.println("object detected!"); } else { Serial.println("waiting for object.."); } delay(200); }
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see object detected!
printed to the Serial Monitor if an input is detected. 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.