Different values between serial monitor and LCD with strange characters

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() { 

      // 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.setBackColor(100, 155, 203);
       myGLCD.print("<-", 18, 15);
       myGLCD.setBackColor(0, 0, 0);
       myGLCD.print("Back to Main Menu", 70, 18);
       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);

       //Prints Luximeter
       myGLCD.drawRoundRect (5, 75, 310, 100);
       myGLCD.printNumF(visible,0,30, 80);    
       myGLCD.print("Lux", 230, 80);

       myGLCD.drawRoundRect (5, 130, 310, 155);
       myGLCD.printNumF(ratio,0,CENTER, 135);


Thanks for any help,


Read the UTFT.pdf documentation. Especially printNumF()

Then think about your display.
Personally I find right-aligned numeric values look the best.

If you like centre-aligned values, just add an extra space at each end.
Otherwise you get an ugly view when you overwrite 12.345 with 6.789


Hi David,
I understand your point. I read the documentation you suggested and even before that, I had already tried various configurations to print. The code I showed below uses "0" in the fractional part and this is not supported in "printNumF". However, I have correct this before and I have tryed change for "printNumI" without success. I have also tried to use all types of variables, uint, float, double without success. The problem is always the same. When I increase the light intensity from a certain value, the screen reading never returns to before value. Instead strange characters are added or more digits appear. On the other hand, the reading on the serial monitor is always correct for any choice of variable.
This sounds kind of surreal, as I've done this before with other sensors and I've never had that sort of problem before.
Do you have any other suggestions?



I do trust you are using a proper 5 V power supply for the Mega?

Hi Paul,

Yes I am using a proper power supply. As said before, I have used others sensors before, pressure, temperature, humidity. I am just a beginner in arduino but I am repeating a sequence that has never presented this type of problem before. I initially distrusted TSL2561, but the values shown on the serial monitor are correct.



OK, just checking.. Would not want you to blunder by trying to run it off "Vin" or the "barrel jack".

The JPG shows Vis/Ir ratio displayed as 1.8
The previous value was probably 1.75

Because you have center-aligned, you can see half of the previous 1 and half of the previous 5

If you used the formatting facilities of printNumF() to display width=4 with 2 decimals, you would overwrite the 1.75 with 1.80

And the display would look attractive (to me)

I may have misunderstood. You said the sensors reported correctly to Serial. So there is no problem with power or wiring.


I tried to use printNumI () for the "lux" and the first reading in low light indicates 9 lux. When I increase the brightness to more than 1000 lux it princes the result correctly, but when I reduce the luminosity to 9 lux again it print 56 09. Two extra digits appear on the left side. When I tried to use printNumF for the lux the first reading in low light correctly indicates 9.0 lux, but after increasing and decreasing the intensity it print 9.0.00
In both situations, the variation of intensity results in the increase of two digits, or the left or the right.These new digits stay there forever. They only desapear if a turn of and turn on again the devices.

For another hand to print "vis /ir" ratio, the system shows a strange character as you can see in the attached photo, right side of the number.

These anomalies occur when the light intensity exceeds 1000 lux. Below 900 lux everything works fine.

However, serial monitor shows consistent results for any lighting intensity, more than 4000 lux without presenting any problem.

I've tried other sensors that displayed correct numbers using similar code. However none of the other sensors produced numbers greater than 100. So I'm wondering if there is any problem with the absolute value of the number to be displayed.




I will try positions not in the center as you suggest...
and printNumF() to display width=4 with 2 decimals



Thank you for teaching me ...

Now I understand better what you wanted to say and basically you was right.
It seems that it is not appropriate to center variables directly using "CENTER". This is best suited for centering indicative phrases or numbers that will not change. On the other hand the number of digits on the right side and the size of the number should also be correctly chosen to avoid this type of problem.
Thanks again,


This is the current part of the code corrected:

//Prints Luximeter
myGLCD.drawRoundRect (5, 75, 310, 100);
myGLCD.printNumI(lux,70, 80,5);
myGLCD.print("Lux", 240, 80);

myGLCD.drawRoundRect (5, 130, 310, 155);
myGLCD.printNumF(ratio,2,110, 135);