Skip to content

Commit c99b451

Browse files
committed
added API to support infrared channel values
1 parent 53aed6a commit c99b451

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

examples/ReadIR/ReadIR.ino

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Arduino LTR381RGB - Read Infrared values
3+
4+
This example reads the IR channel values from the LTR381RGB
5+
sensor and continuously prints them to the Serial Monitor
6+
or Serial Plotter.
7+
8+
The circuit:
9+
- Arduino Uno WiFi R4
10+
- Modulino light sensor
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include "Arduino_LTR381RGB.h"
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
while (!Serial);
20+
21+
if (!RGB.begin()) {
22+
Serial.println("Failed to initialize rgb sensor!");
23+
while (1);
24+
}
25+
}
26+
27+
void loop() {
28+
int ir;
29+
30+
if (RGB.readIR(ir)) {
31+
Serial.print("Value: ");
32+
Serial.println(ir);
33+
}
34+
35+
delay(1000);
36+
}

src/LTR381RGB.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ int LTR381RGBClass::readLux(int& lux) {
137137
return 1;
138138
}
139139

140+
int LTR381RGBClass::readIR(int& ir) {
141+
enableALS();
142+
unsigned long start = millis();
143+
while(!available() && (millis() - start) < _timeout);
144+
uint8_t buf[3] = {0};
145+
int res = readRegisters(LTR381RGB_CS_DATA_IR, buf, 3);
146+
if(res != 1) {
147+
return 0;
148+
}
149+
ir = buf[2] << 16 | buf[1] << 8 | buf[0];
150+
151+
return 1;
152+
}
140153

141154
void LTR381RGBClass::setGain(int gain) {
142155
writeRegister(LTR381RGB_ALS_CS_GAIN, (0x07 & gain));

src/LTR381RGB.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LTR381RGBClass {
2020

2121
int readAmbientLight(int& lux);
2222
int readLux(int& lux);
23-
23+
int readIR(int& ir);
2424
void setGain(int gain);
2525
void setADCResolution(int resolution);
2626
void setMeasurementRate(int rate);

0 commit comments

Comments
 (0)