SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
ltr553als_sensor.ino
Go to the documentation of this file.
1
30#include <Wire.h>
31#include <SPI.h>
32#include <Arduino.h>
33#include <LightSensorDrv.hpp>
34
35#ifndef SENSOR_SDA
36#define SENSOR_SDA 5
37#endif
38
39#ifndef SENSOR_SCL
40#define SENSOR_SCL 6
41#endif
42
43#ifndef SENSOR_IRQ
44#define SENSOR_IRQ 21
45#endif
46
48
49void setup()
50{
51 Serial.begin(115200);
52 while (!Serial);
53
54 pinMode(SENSOR_IRQ, INPUT_PULLUP);
55
56 if (!als.begin(Wire, LTR553_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
57 Serial.println("Failed to find LTR553 - check your wiring!");
58 while (1) {
59 delay(1000);
60 }
61 }
62
63 Serial.println("Init LTR553 Sensor success!");
64
65 // Set the ambient light high and low thresholds.
66 // If the value exceeds or falls below the set value, an interrupt will be triggered.
68
69 // Set the high and low thresholds of the proximity sensor.
70 // If the value exceeds or falls below the set value, an interrupt will be triggered.
72
73 // Controls the Light Sensor N number of times the measurement data is outside the range
74 // defined by the upper and lower threshold limits before asserting the interrupt.
76
77 // Controls the Proximity N number of times the measurement data is outside the range
78 // defined by the upper and lower threshold limits before asserting the interrupt.
80
81 /*
82 * ALS_IRQ_ACTIVE_LOW, // INT pin is considered active when it is a logic 0 (default)
83 * ALS_IRQ_ACTIVE_HIGH // INT pin is considered active when it is a logic 1
84 * * */
86
87 /*
88 * ALS_IRQ_ONLY_PS, // Only PS measurement can trigger interrupt
89 * ALS_IRQ_ONLY_ALS, // Only ALS measurement can trigger interrupt
90 * ALS_IRQ_BOTH, // Both ALS and PS measurement can trigger interrupt
91 * * * */
93
94 /*
95 * ALS_GAIN_1X , -> 1 lux to 64k lux (default)
96 * ALS_GAIN_2X , -> 0.5 lux to 32k lux
97 * ALS_GAIN_4X , -> 0.25 lux to 16k lux
98 * ALS_GAIN_8X , -> 0.125 lux to 8k lux
99 * ALS_GAIN_48X , -> 0.02 lux to 1.3k lux
100 * ALS_GAIN_96X , -> 0.01 lux to 600 lux
101 * */
103
104 /*
105 * PS_LED_PLUSE_30KHZ,
106 * PS_LED_PLUSE_40KHZ,
107 * PS_LED_PLUSE_50KHZ,
108 * PS_LED_PLUSE_60KHZ,
109 * PS_LED_PLUSE_70KHZ,
110 * PS_LED_PLUSE_80KHZ,
111 * PS_LED_PLUSE_90KHZ,
112 * PS_LED_PLUSE_100KHZ,
113 * * * * * * * * * * */
115
116
117 /*
118 * PS_LED_DUTY_25,
119 * PS_LED_DUTY_50,
120 * PS_LED_DUTY_75,
121 * PS_LED_DUTY_100,
122 * * * */
124
125
126 /*
127 * PS_LED_CUR_5MA,
128 * PS_LED_CUR_10MA,
129 * PS_LED_CUR_20MA,
130 * PS_LED_CUR_50MA,
131 * PS_LED_CUR_100MA,
132 * * * * * * * */
134
135 /*
136 * PS_MEAS_RATE_50MS,
137 * PS_MEAS_RATE_70MS,
138 * PS_MEAS_RATE_100MS,
139 * PS_MEAS_RATE_200MS,
140 * PS_MEAS_RATE_500MS,
141 * PS_MEAS_RATE_1000MS,
142 * PS_MEAS_RATE_2000MS,
143 * PS_MEAS_RATE_10MS
144 * * * * * * * */
146
147 // Number of pulses
149
150 // Enable proximity sensor saturation indication
152
153 // Enable ambient light sensor
155
156 // Enable proximity sensor
158
159}
160
162{
163#if SENSOR_IRQ != -1
164 return digitalRead(SENSOR_IRQ) == LOW;
165#else
166 static uint32_t lastReadMillis;
167 if (millis() > lastReadMillis) {
168 lastReadMillis = millis() + 500;
169 return true;
170 }
171 return false;
172#endif
173}
174
175void loop()
176{
177 /*
178 * PS Saturation Flag is used for monitoring the internal IC saturation.
179 * It will be flagged when the IC has reached saturation
180 * and not able to perform any further PS measurement
181 * */
182 bool saturated = false;
183 if (canRead()) {
184 Serial.print(" ALS: CH1:"); Serial.print(als.getLightSensor(1));
185 Serial.print(" - CH0:"); Serial.print(als.getLightSensor(0));
186 Serial.print(" - PS:"); Serial.print(als.getProximity(&saturated));
187 Serial.print(" - "); Serial.println(saturated ? "PS saturated" : "PS not saturated");
188 }
189 delay(5);
190}
191
192
193
@license MIT License
bool begin(SensorCommCustom::CustomCallback callback, uint8_t addr)
Initialize the sensor using custom callback interface.
Driver for the LTR-553ALS-01 ambient light + proximity sensor (I2C).
void setProximityThreshold(uint16_t low, uint16_t high)
Set PS threshold window.
void enablePsIndicator()
Enable PS indicator.
void setIRQLevel(IrqLevel level)
Set interrupt pin polarity.
int getLightSensor(uint8_t ch)
Read ALS raw data from channel 0 or 1.
void setLightSensorThreshold(uint16_t low, uint16_t high)
Set ALS threshold window.
@ ALS_IRQ_ACTIVE_LOW
INT pin is active-low (default)
void setLightSensorGain(LightSensorGain gain)
Set ALS gain.
void setPsLedCurrent(PsLedCurrent cur)
Set PS LED peak current (REG_PS_LED bits 2:0).
void setPsLedPulsePeriod(PsLedPeriod period)
Set PS LED pulse modulation frequency (REG_PS_LED bits 7:5).
int getProximity(bool *saturated=NULL)
Read proximity raw data.
void setLightSensorPersists(uint8_t count)
Configure ALS interrupt persistence count.
@ ALS_IRQ_BOTH
Both ALS and PS measurement can trigger interrupt.
void enableProximity()
Enable proximity measurement.
void setProximityRate(PsRate rate)
Set PS measurement rate.
void enableIRQ(IrqMode mode)
Enable interrupts and select which measurement sources can trigger them.
void setPsLedDutyCycle(PsLedDuty duty)
Set PS LED duty cycle (REG_PS_LED bits 4:3).
void setPsLedPulses(uint8_t pulesNum)
Set number of PS LED pulses (REG_PS_N_PULSES bits 3:0).
@ ALS_GAIN_8X
0.125 lux to 8k lux
void setProximityPersists(uint8_t count)
Configure PS interrupt persistence count.
void enableLightSensor()
Enable ALS measurement.
SensorLTR553 als
void setup()
#define SENSOR_SCL
bool canRead()
#define SENSOR_SDA
#define SENSOR_IRQ
void loop()