Dear all,
I am trying to construct a light sensor using TSL2561 coupled in Arduino MEGA with LCD touch screen. I am using the code below. Unfortunately, when I expose the sensor to a slightly more intense light, the LCD shows strange characters and does not return to normal, see the photo. On the other hand, the serial monitor shows variations and normal numbers that are expected for the test situation.
Can anyone help me with this?
#include <TSL2561.h>
#include <UTFT.h>
#include <URTouch.h>
UTFT myGLCD(ILI9341_16,38,39,40,41); //Parameters should be adjusted to your Display/Schield model
URTouch myTouch( 6, 5, 4, 3, 2);
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
int x, y;
char currentPage, selectedUnit;
TSL2561 tsl(TSL2561_ADDR_FLOAT);
void setup() {
Serial.begin(9600);
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
// Defining TSL2561
if (tsl.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No sensor?");
while (1);
}
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
tsl.setGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
//tsl.setGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations)
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
//tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light)
//tsl.setTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light)
}
void drawLightSensor() {
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.drawLine(0,40,319,40);
myGLCD.setFont(BigFont);
myGLCD.setColor(255, 255, 255);
myGLCD.print("Luximeter", CENTER , 50);
myGLCD.setColor(255, 255, 255);
myGLCD.print("Vis/Ir Ratio", CENTER, 110);
myGLCD.setColor(255, 255, 255);
}
void loop() {
// Simple data read example. Just read the infrared, fullspecrtrum diode
// or 'visible' (difference between the two) channels.
// This can take 13-402 milliseconds! Uncomment whichever of the following you want to read
//uint16_t x = tsl.getLuminosity(TSL2561_VISIBLE);
//uint16_t x = tsl.getLuminosity(TSL2561_FULLSPECTRUM);
//uint16_t x = tsl.getLuminosity(TSL2561_INFRARED);
Serial.println(x, DEC);
// More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
// That way you can do whatever math and comparisons you want!
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
float lux;
float visible;
float ratio;
ir = lum >> 16;
full = lum & 0xFFFF;
//lux = tsl.calculateLux(full, ir);
visible = (full-ir);
ratio = visible/ir;
Serial.print("IR: "); Serial.print(ir); Serial.print("\t\t");
Serial.print("Full: "); Serial.print(full); Serial.print("\t");
Serial.print("Visible: "); Serial.print(full - ir); Serial.print("\t");
Serial.print("Lux: "); Serial.println(lux);
Serial.print("ratio"); Serial.println(ratio);
delay(100);
//Prints Luximeter
myGLCD.setColor(255,200,000);
myGLCD.drawRoundRect (5, 75, 310, 100);
myGLCD.setColor(0,255,0);
myGLCD.setFont(BigFont);
myGLCD.printNumF(visible,0,30, 80);
myGLCD.print("Lux", 230, 80);
//Prints
myGLCD.setColor(255,200,000);
myGLCD.drawRoundRect (5, 130, 310, 155);
myGLCD.setColor(0,255,0);
myGLCD.setFont(BigFont);
myGLCD.printNumF(ratio,0,CENTER, 135);
delay(500);
}
Thanks for any help,
Cesar