-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTSL2561_coffee_sensor_OLED.ino
440 lines (354 loc) · 13.1 KB
/
TSL2561_coffee_sensor_OLED.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* Coffee darkness meter - by V.Chan - Toronto, Canada, Oct 2021
* for testing the darkness of whole bean and ground coffee
* 1) uses an Adafruit TSL2561 breakout board (others can be
* substituded
* 2) an SSD1306 OLED for a display
* 3) arduino nano
*
* Note: I only use the IR sensor, the LUX sensor had a narrow range
* although I left the code commented out
*
* To enter calibration mode: press button while powering up.
* you need a cinnamon roast (beginning of first crack) this
* taken as eegtron = 70, and an Italian roast (just end of
* 2nd crack) eegtron = 30.
*
* To make measurements, just press button to start 4 avg readings
* Eggtron measurements are based off calibration above
*
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h> //for the IR sensor
#include <EEPROM.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h" //for the OLED display
#define I2C_ADDRESS 0x3C
#define RST_PIN -1
SSD1306AsciiWire oled;
//for separate broadband and ir measurements
int broadband = 0;
int infrared = 0;
int buttonState = 0; // variable for reading the pushbutton status
/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
which provides a common 'type' for sensor data and some helper functions.
To use this driver you will also need to download the Adafruit_Sensor
library and include it in your libraries folder.
You should also assign a unique ID to this sensor for use with
the Adafruit Sensor API so that you can identify this particular
sensor in any data logs, etc. To assign a unique ID, simply
provide an appropriate value in the constructor below (12345
is used by default in this example).
Connections
===========
Connect SCL to I2C SCL Clock
Connect SDA to I2C SDA Data
Connect VCC/VDD to 3.3V or 5V (depends on sensor's logic level, check the datasheet)
Connect GROUND to common ground
I2C Address
===========
The address will be different depending on whether you leave
the ADDR pin floating (addr 0x39), or tie it to ground or vcc.
The default addess is 0x39, which assumes the ADDR pin is floating
(not connected to anything). If you set the ADDR pin high
or low, use TSL2561_ADDR_HIGH (0x49) or TSL2561_ADDR_LOW
(0x29) respectively.
History
=======
2013/JAN/31 - First version (KTOWN)
*/
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
/**************************************************************************/
/*
Displays some basic information on this sensor from the unified
sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
oled.clear();
sensor_t sensor;
tsl.getSensor(&sensor);
oled.println("Coffee Darkness Sensor");
oled.println("------------------------------------");
oled.print ("Sensor: "); oled.println(sensor.name);
oled.print ("Driver Ver:"); oled.println(sensor.version);
oled.print ("Unique ID: "); oled.println(sensor.sensor_id);
oled.print ("Max Value: "); oled.print(sensor.max_value); oled.println(" lux");
oled.print ("Min Value: "); oled.print(sensor.min_value); oled.println(" lux");
oled.print ("Resolution:"); oled.print(sensor.resolution); oled.println(" lux");
oled.println("------------------------------------");
oled.println("");
delay(1000);
}
/**************************************************************************/
/*
Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
/* Update these values depending on what you've set above! */
oled.clear();
oled.println("------------------------------------");
oled.print ("Gain: "); oled.println("Auto");
oled.print ("Timing "); oled.println("13 ms");
oled.println("------------------------------------");
delay (1000);
}
/**************************************************************************/
/*
functions to take average of 4 measurements
*/
/**************************************************************************/
int average4reads(int farts)
{
int total = 0;
for (int i = 0; i < 4; i++) {
oled.clear();
oled.print("taking measure= "); oled.println(i+1);
digitalWrite(4, HIGH); //red led
digitalWrite(5, HIGH); //red led
delay (100);
digitalWrite(4, LOW); //red led
digitalWrite(5, LOW); //red led
delay (100);
digitalWrite(6, HIGH); //IR led
digitalWrite(7, HIGH); //IR led
tsl.getLuminosity (&broadband, &infrared);
oled.println("");
oled.print("infrared= "); oled.println(infrared);
oled.println("");
digitalWrite(6, LOW); //IR led
digitalWrite(7, LOW); //IR led
delay (250);
if (i < 3) {
oled.print("rotate to pos= "); oled.println(i+2);
}
delay (4000);
total = total + infrared;
}
int averageReading = total / 4;
oled.print("average reading = "); oled.println(averageReading);
delay(2000);
return averageReading ;
}
/**************************************************************************/
/*
calibrates the sensor between 1st crack and French roast
*/
/**************************************************************************/
void calibrateSensor(void)
{
oled.clear();
oled.println("*** CALIBRATION ***");
delay(3000);
oled.println("take cinnamon roast ");
oled.println("measurement - NOW");
oled.println("");
delay(3000);
/* Get a new sensor event */
//sensors_event_t event;
//tsl.getEvent(&event);
//digitalWrite(6, HIGH); //IR led
//digitalWrite(7, HIGH); //IR led
//tsl.getLuminosity (&broadband, &infrared);
infrared = average4reads(1);
oled.print("Cinnamon IR= "); oled.println(infrared);
//digitalWrite(6, LOW); //IR led
//digitalWrite(7, LOW); //IR led
oled.println("");
oled.print("writing to EEPROM..... ");
writeIntIntoEEPROM(0, infrared);
delay(10000);
oled.clear();
oled.println("take Italian roast ");
oled.println("measurement - NOW");
oled.println("");
delay(3000);
/* Get a new sensor event */
//sensors_event_t event;
//tsl.getEvent(&event);
//digitalWrite(6, HIGH); //IR led
//digitalWrite(7, HIGH); //IR led
//tsl.getLuminosity (&broadband, &infrared);
infrared = average4reads(1);
oled.print("Italian IR= "); oled.println(infrared);
//digitalWrite(6, LOW); //IR led
//digitalWrite(7, LOW); //IR led
oled.println("");
oled.print("writing to EEPROM..... ");
writeIntIntoEEPROM(2, infrared);
oled.println("");
oled.println("FINISHED CALIBRATION");
delay(5000);
oled.clear();
}
/**************************************************************************/
/*
functions to allow to write an int to EEPROM
*/
/**************************************************************************/
void writeIntIntoEEPROM(int address, int number)
{
EEPROM.write(address, number >> 8);
EEPROM.write(address + 1, number & 0xFF);
}
int readIntFromEEPROM(int address)
{
return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}
/**************************************************************************/
/*
Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
Serial.println("Light Sensor Test"); Serial.println("");
Wire.begin();
Wire.setClock(400000L);
#if RST_PIN >= 0
oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
oled.begin(&Adafruit128x64, I2C_ADDRESS);
#endif // RST_PIN >= 0
oled.setFont(Adafruit5x7);
oled.clear();
oled.println("Coffee Darkness Sensor");
oled.println();
sensors_event_t event;
//set up the LED's
pinMode(4, OUTPUT); //red LED
pinMode(5, OUTPUT); //red LED
pinMode(6, OUTPUT); //IR LED
pinMode(7, OUTPUT); //IR LED
pinMode(2, INPUT); //calibrate button
/* Initialise the sensor */
//use tsl.begin() to default to Wire,
//tsl.begin(&Wire2) directs api to use Wire2, etc.
if(!tsl.begin())
{
/* There was a problem detecting the TSL2561 ... check your connections */
oled.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
while(1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
/* Setup the sensor gain and integration time */
configureSensor();
/* We're ready to go! */
oled.println("setup done");
//check to see if the calibrate button is pushed
// read the state of the pushbutton value:
buttonState = digitalRead(2);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// go to calibrate routine
oled.println("going to calibrations routine");
calibrateSensor();
}
oled.println("");
oled.println("Press button to");
oled.println("take reading");
oled.println("");
}
/**************************************************************************/
/*
Arduino loop function, called once 'setup' is complete (your own code
should go here)
*/
/**************************************************************************/
void loop(void)
{
//turn on LEDs
//digitalWrite(4, HIGH); //red led
//digitalWrite(5, HIGH); //red led
//digitalWrite(6, HIGH); //IR led
//digitalWrite(7, HIGH); //IR led
//delay(250);
/* Get a new sensor event */
//sensors_event_t event;
//tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
//if (event.light)
//{
// Serial.print(event.light); Serial.println(" lux");
//}
//else
//{
/* If event.light = 0 lux the sensor is probably saturated
and no reliable data could be generated! */
// Serial.println("Sensor overload");
//}
//digitalWrite(6, LOW); //IR led
//digitalWrite(7, LOW); //IR led
//delay(250);
//tsl.getLuminosity (&broadband, &infrared);
//Serial.print("broadband= "); Serial.println(broadband);
//digitalWrite(4, LOW); //red led
//digitalWrite(5, LOW); //red led
//digitalWrite(6, HIGH); //IR led
//digitalWrite(7, HIGH); //IR led
//delay(250);
//tsl.getLuminosity (&broadband, &infrared);
//Serial.print("infrared= "); Serial.println(infrared);
////turn off LEDs
//digitalWrite(6, LOW); //IR led
//digitalWrite(7, LOW); //IR led
// read the state of the pushbutton value:
buttonState = digitalRead(2);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// measure the IR value
oled.clear();
oled.println("taking IR measurements");
infrared = average4reads(1);
oled.clear();
oled.print("Average= "); oled.println(infrared);
int darkNumber = readIntFromEEPROM(2);
int lightNumber = readIntFromEEPROM(0);
oled.print("Italian (Egg30)= "); oled.println(darkNumber);
oled.print("Cinnamon(Egg70)= "); oled.println(lightNumber);
float eggtron = ((infrared-darkNumber)*10.0 / (lightNumber - darkNumber))*4.0+30.0;
oled.println("EGGTRON scale value");
oled.set2X();
oled.print(" ");
oled.println(eggtron);
oled.set1X();
if (eggtron < 35.0) {
oled.println("Italian 30-35");
oled.println("Shiny, past 2nd crack");
}
if ((eggtron >= 35.0) && (eggtron <45)) {
oled.println("French 35-45 ");
oled.println("Some oil 2nd crack");
}
if ((eggtron >= 45.0) && (eggtron <55)) {
oled.println("City+ Vienna 45-55");
oled.println("begin 2nd crack");
}
if ((eggtron >= 55.0) && (eggtron <60)) {
oled.println("City medium 55-60");
oled.println("medium brown");
}
if ((eggtron >= 60.0) && (eggtron <70)) {
oled.println("American med/li 60-70");
oled.println("end 1st crack");
}
if (eggtron >= 70.0) {
oled.println("Cinnamon > 70");
oled.println("begin 1st crack");
}
}
delay(100);
}