Skip to content

Commit 5db3ec9

Browse files
Olivier LeDiourisgohai
authored andcommitted
IO: Add TSL2561 environmental sensor example
Donated by @OlivierLD, minor cleanups by @gohai
1 parent 2e05386 commit 5db3ec9

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import processing.io.*;
2+
TSL2561 sensor;
3+
4+
// see setup.png in the sketch folder for wiring details
5+
6+
// this variable will contain the measured brightness
7+
// Lux (lx) is the unit of illuminance
8+
float lux;
9+
10+
void setup() {
11+
size(700, 100);
12+
textSize(72);
13+
//printArray(I2C.list());
14+
sensor = new TSL2561("i2c-1", 0x39);
15+
}
16+
17+
void draw() {
18+
background(0);
19+
stroke(255);
20+
lux = sensor.lux();
21+
text(String.format("Light: %.02f Lux", lux), 10, 75);
22+
}
23+
24+
void dispose() {
25+
// turn the sensor off
26+
sensor.stop();
27+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import processing.io.I2C;
2+
3+
// TSL2561 is light sensor using I2C
4+
// datasheet: https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf
5+
// code contributed by @OlivierLD
6+
7+
public class TSL2561 extends I2C {
8+
9+
public final static int TSL2561_ADDRESS = 0x39;
10+
11+
public final static int TSL2561_ADDRESS_LOW = 0x29;
12+
public final static int TSL2561_ADDRESS_FLOAT = 0x39;
13+
public final static int TSL2561_ADDRESS_HIGH = 0x49;
14+
15+
public final static int TSL2561_COMMAND_BIT = 0x80;
16+
public final static int TSL2561_WORD_BIT = 0x20;
17+
public final static int TSL2561_CONTROL_POWERON = 0x03;
18+
public final static int TSL2561_CONTROL_POWEROFF = 0x00;
19+
20+
public final static int TSL2561_REGISTER_CONTROL = 0x00;
21+
public final static int TSL2561_REGISTER_TIMING = 0x01;
22+
public final static int TSL2561_REGISTER_CHAN0_LOW = 0x0C;
23+
public final static int TSL2561_REGISTER_CHAN0_HIGH = 0x0D;
24+
public final static int TSL2561_REGISTER_CHAN1_LOW = 0x0E;
25+
public final static int TSL2561_REGISTER_CHAN1_HIGH = 0x0F;
26+
public final static int TSL2561_REGISTER_ID = 0x0A;
27+
28+
public final static int TSL2561_GAIN_1X = 0x00;
29+
public final static int TSL2561_GAIN_16X = 0x10;
30+
31+
public final static int TSL2561_INTEGRATIONTIME_13MS = 0x00; // rather 13.7ms
32+
public final static int TSL2561_INTEGRATIONTIME_101MS = 0x01;
33+
public final static int TSL2561_INTEGRATIONTIME_402MS = 0x02;
34+
35+
public final static double TSL2561_LUX_K1C = 0.130; // (0x0043) // 0.130 * 2^RATIO_SCALE
36+
public final static double TSL2561_LUX_B1C = 0.0315; // (0x0204) // 0.0315 * 2^LUX_SCALE
37+
public final static double TSL2561_LUX_M1C = 0.0262; // (0x01ad) // 0.0262 * 2^LUX_SCALE
38+
public final static double TSL2561_LUX_K2C = 0.260; // (0x0085) // 0.260 * 2^RATIO_SCALE
39+
public final static double TSL2561_LUX_B2C = 0.0337; // (0x0228) // 0.0337 * 2^LUX_SCALE
40+
public final static double TSL2561_LUX_M2C = 0.0430; // (0x02c1) // 0.0430 * 2^LUX_SCALE
41+
public final static double TSL2561_LUX_K3C = 0.390; // (0x00c8) // 0.390 * 2^RATIO_SCALE
42+
public final static double TSL2561_LUX_B3C = 0.0363; // (0x0253) // 0.0363 * 2^LUX_SCALE
43+
public final static double TSL2561_LUX_M3C = 0.0529; // (0x0363) // 0.0529 * 2^LUX_SCALE
44+
public final static double TSL2561_LUX_K4C = 0.520; // (0x010a) // 0.520 * 2^RATIO_SCALE
45+
public final static double TSL2561_LUX_B4C = 0.0392; // (0x0282) // 0.0392 * 2^LUX_SCALE
46+
public final static double TSL2561_LUX_M4C = 0.0605; // (0x03df) // 0.0605 * 2^LUX_SCALE
47+
public final static double TSL2561_LUX_K5C = 0.65; // (0x014d) // 0.65 * 2^RATIO_SCALE
48+
public final static double TSL2561_LUX_B5C = 0.0229; // (0x0177) // 0.0229 * 2^LUX_SCALE
49+
public final static double TSL2561_LUX_M5C = 0.0291; // (0x01dd) // 0.0291 * 2^LUX_SCALE
50+
public final static double TSL2561_LUX_K6C = 0.80; // (0x019a) // 0.80 * 2^RATIO_SCALE
51+
public final static double TSL2561_LUX_B6C = 0.0157; // (0x0101) // 0.0157 * 2^LUX_SCALE
52+
public final static double TSL2561_LUX_M6C = 0.0180; // (0x0127) // 0.0180 * 2^LUX_SCALE
53+
public final static double TSL2561_LUX_K7C = 1.3; // (0x029a) // 1.3 * 2^RATIO_SCALE
54+
public final static double TSL2561_LUX_B7C = 0.00338; // (0x0037) // 0.00338 * 2^LUX_SCALE
55+
public final static double TSL2561_LUX_M7C = 0.00260; // (0x002b) // 0.00260 * 2^LUX_SCALE
56+
public final static double TSL2561_LUX_K8C = 1.3; // (0x029a) // 1.3 * 2^RATIO_SCALE
57+
public final static double TSL2561_LUX_B8C = 0.000; // (0x0000) // 0.000 * 2^LUX_SCALE
58+
public final static double TSL2561_LUX_M8C = 0.000; // (0x0000) // 0.000 * 2^LUX_SCALE
59+
60+
private int gain = TSL2561_GAIN_1X;
61+
private int integration = TSL2561_INTEGRATIONTIME_402MS;
62+
private int pause = 800;
63+
64+
private int address;
65+
66+
67+
public TSL2561(String dev) {
68+
this(dev, TSL2561_ADDRESS);
69+
}
70+
71+
public TSL2561(String dev, int address) {
72+
super(dev);
73+
this.address = address;
74+
start();
75+
}
76+
77+
public void start() {
78+
command(TSL2561_COMMAND_BIT, (byte) TSL2561_CONTROL_POWERON);
79+
}
80+
81+
public void stop() {
82+
command(TSL2561_COMMAND_BIT, (byte) TSL2561_CONTROL_POWEROFF);
83+
}
84+
85+
public void setGain() {
86+
setGain(TSL2561_GAIN_1X);
87+
}
88+
89+
public void setGain(int gain) {
90+
setGain(gain, TSL2561_INTEGRATIONTIME_402MS);
91+
}
92+
93+
public void setGain(int gain, int integration) {
94+
if (gain != TSL2561_GAIN_1X && gain != TSL2561_GAIN_16X) {
95+
throw new IllegalArgumentException("Invalid gain value");
96+
}
97+
if (gain != this.gain || integration != this.integration) {
98+
command(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, (byte) (gain | integration));
99+
//println("Setting low gain");
100+
this.gain = gain;
101+
this.integration = integration;
102+
delay(pause); // pause for integration (pause must be bigger than integration time)
103+
}
104+
}
105+
106+
/**
107+
* Read visible+IR diode from the I2C device
108+
*/
109+
public int readFull() {
110+
int reg = TSL2561_COMMAND_BIT | TSL2561_REGISTER_CHAN0_LOW;
111+
return readU16(reg);
112+
}
113+
114+
/**
115+
* Read IR only diode from the I2C device
116+
*/
117+
public int readIR() {
118+
int reg = TSL2561_COMMAND_BIT | TSL2561_REGISTER_CHAN1_LOW;
119+
return readU16(reg);
120+
}
121+
122+
/**
123+
* Device lux range 0.1 - 40,000+
124+
* see https://learn.adafruit.com/tsl2561/overview
125+
*/
126+
public float lux() {
127+
int ambient = this.readFull();
128+
int ir = this.readIR();
129+
130+
//println("IR Result: " + ir);
131+
//println("Ambient Result: " + ambient);
132+
133+
if (ambient >= 0xffff || ir >= 0xffff) {
134+
throw new RuntimeException("Gain too high, values exceed range");
135+
}
136+
double ratio = (ir / (float) ambient);
137+
138+
/*
139+
* For the values below, see https://github.com/adafruit/_TSL2561/blob/master/_TSL2561_U.h
140+
*/
141+
float lux = 0.0f;
142+
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K4C)) {
143+
lux = (float)((TSL2561_LUX_B1C * ambient) - (0.0593 * ambient * (Math.pow(ratio, 1.4))));
144+
} else if (ratio <= TSL2561_LUX_K5C) {
145+
lux = (float)((TSL2561_LUX_B5C * ambient) - (TSL2561_LUX_M5C * ir));
146+
} else if (ratio <= TSL2561_LUX_K6C) {
147+
lux = (float)((TSL2561_LUX_B6C * ambient) - (TSL2561_LUX_M6C * ir));
148+
} else if (ratio <= TSL2561_LUX_K7C) {
149+
lux = (float)((TSL2561_LUX_B7C * ambient) - (TSL2561_LUX_M7C * ir));
150+
} else if (ratio > TSL2561_LUX_K8C) {
151+
lux = 0.0f;
152+
}
153+
return lux;
154+
}
155+
156+
157+
private void command(int register, byte value) {
158+
beginTransmission(address);
159+
write(register);
160+
write(value);
161+
endTransmission();
162+
}
163+
164+
private int readU8(int register) {
165+
beginTransmission(this.address);
166+
write(register);
167+
byte[] ba = read(1);
168+
endTransmission();
169+
return (int)(ba[0] & 0xFF);
170+
}
171+
172+
private int readU16(int register) {
173+
int lo = readU8(register);
174+
int hi = readU8(register + 1);
175+
int result = (hi << 8) + lo; // Big Endian
176+
//println("(U16) I2C: Device " + toHex(TSL2561_ADDRESS) + " returned " + toHex(result) + " from reg " + toHex(register));
177+
return result;
178+
}
179+
180+
private String toHex(int i) {
181+
String s = Integer.toString(i, 16).toUpperCase();
182+
while (s.length() % 2 != 0) {
183+
s = "0" + s;
184+
}
185+
return "0x" + s;
186+
}
187+
}

0 commit comments

Comments
 (0)