An Arduino-compatible library for implementing light-based (typically infrared) beam-break and reflectance sensors.
Disclaimer: This README file was AI-generated, but checked for accuracy by a human.
- Auto-Thresholding: Automatically adjusts detection thresholds based on ambient conditions
- Digital Low-Pass Filtering: Optional filtering to reduce noise and false triggers
- Configurable Sensitivity: Adjustable detection sensitivity from 0-100%
- Multi-Photobeam Support: Manage multiple photodetectors with shared status bitmasks
- Polarity Control: Support for both active-high and active-low sensor configurations
- PWM Emitter Control: Optional PWM control for IR emitter brightness
- Historical Min/Max Tracking: Monitors signal history for adaptive thresholding
This library requires the Vulintus_Digital_Filter library. Install it before using this library.
- Download or clone this repository into your Arduino libraries folder
- Download and install the Vulintus_Digital_Filter library
- Restart the Arduino IDE
#include <Vulintus_Photodetector.h>
#define BEAM_IN A0
Vulintus_Photodetector beam(BEAM_IN);
void setup() {
Serial.begin(115200);
beam.set_sensitivity(0.8); // 80% sensitivity
beam.lowpass_cutoff(20); // 20 Hz low-pass filter
beam.begin();
}
void loop() {
if (beam.read()) { // Returns true if status changed
Serial.print("Beam ");
Serial.println(beam.is_blocked ? "BLOCKED" : "CLEAR");
}
}#include <Vulintus_Photodetector.h>
#define BEAM_L A2
#define BEAM_C A1
#define BEAM_R A0
const uint8_t NUM_BEAMS = 3;
Vulintus_Photodetector beam[] = {
Vulintus_Photodetector(BEAM_L, 0),
Vulintus_Photodetector(BEAM_C, 1),
Vulintus_Photodetector(BEAM_R, 2),
};
void setup() {
Serial.begin(115200);
for (uint8_t i = 0; i < NUM_BEAMS; i++) {
beam[i].set_sensitivity(0.8);
beam[i].begin();
}
}
void loop() {
bool changed = false;
for (uint8_t i = 0; i < NUM_BEAMS; i++) {
changed |= beam[i].read();
}
if (changed) {
Serial.print("Bitmask: ");
Serial.println(beam[0].bitmask, BIN); // Static variable shared across all instances
}
}Vulintus_Photodetector(uint8_t pin_detector, uint8_t beam_index = 0, bool blocked_val = HIGH)pin_detector: Analog input pin for the photodetectorbeam_index: Index for multi-beam configurations (0-7)blocked_val: Polarity setting -HIGH(default) means blocked beam reads high,LOWmeans blocked beam reads low
Initialize the photodetector. Call this in setup() after configuring parameters.
Update the photodetector status. Returns true if the status changed, false otherwise.
Set detection sensitivity (0.0 to 1.0, where higher values are more sensitive). Default is 0.5.
Manually set the threshold in ADC ticks. Disables auto-thresholding.
Get the current threshold value.
Set the low-pass filter cutoff frequency in Hz. Set to 0 to disable filtering.
Get the current low-pass filter cutoff frequency.
Assign a PWM pin to control the IR emitter.
Set emitter brightness (0-255).
Reset the historical min/max values. Useful when ambient conditions change significantly.
bool is_blocked: Current blocked/unblocked statusstatic uint8_t bitmask: Shared status bitmask across all photodetector instances (bit positions correspond to beam_index)uint8_t index: Beam index for multi-beam configurations (0-7)bool polarity: Detection polarity -HIGHmeans blocked beam reads high,LOWmeans blocked beam reads low (default:HIGH)uint16_t reading: Current ADC readinguint32_t read_time: Timestamp of last reading (microseconds)uint16_t history[2]: Historical minimum [0] and maximum [1] ADC valuesuint16_t min_range: Minimum required range between min and max (default 100)uint16_t reset_timeout: Timeout for resetting history in milliseconds (default 30000)bool auto_thresh: Enable/disable auto-thresholding (default true)
The library includes two examples:
- Vulintus_Photobeam_Filter_Test: Demonstrates the low-pass filter feature with serial plotter output
- Vulintus_Three_Photobeam_Test: Shows multi-beam configuration with status bitmask
Access them via File > Examples > Vulintus Photodetector Library in the Arduino IDE.
The library continuously tracks the minimum and maximum ADC values from the photodetector. The threshold is automatically calculated as:
threshold = min + (max - min) × sensitivity
This allows the system to adapt to varying ambient light conditions and sensor characteristics without manual calibration.
- Compatible with all Arduino-compatible boards
- Requires analog input capability
- Tested with infrared photobeam sensors and reflectance sensors
Copyright (c) 2024-2025, Vulintus, Inc. All rights reserved.
See license.txt for details.
For issues, questions, or contributions:
- 0.1.0 (2025-06-06): Initial release as standalone library (previously part of Vulintus_OmniTrak)