corrupted text
-
As you can see from the following photo I have a problem with text display. When I first upload the sketch or do a reset on my lilygo t-disply the text will display fine...for about 30 seconds. Then the temperature degrades into the little boxes you see in the photo. The date and time will generally be fine for several hours but after a while that too degrades. I am using the TFT_eSPI library.
#include <Adafruit_GFX.h> // Core graphics library #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789 #include <SPI.h> #include <WiFi.h> #include <Wire.h> #include "time.h" const char* ssid = "randomtorok-d"; const char* password = "Chipss12"; int counter = 0; int dT= 5000; // pinouts from https://github.com/Xinyuan-LilyGO/TTGO-T-Display #define TFT_MOSI 19 #define TFT_SCLK 18 #define TFT_CS 5 #define TFT_DC 16 #define TFT_RST 23 #define TFT_BL 4 // constructor for data object named tft Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); const char* ntpServer = "pool.ntp.org"; const long gmtOffset_sec = -28800; const int daylightOffset_sec = 3600; float getTemperature(){ #define ADC_VREF_mV 5000.0 // in millivolt #define ADC_RESOLUTION 4095.0 #define PIN_LM35 32 // ESP32 pin GPIO36 (ADC0) connected to LM35 // read the ADC value from the temperature sensor int adcVal = analogRead(PIN_LM35); //int adcVal = 250; // convert the ADC value to voltage in millivolt float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION); // convert the voltage to the temperature in °C float tempC = milliVolt / 10; return tempC; } void setup(void) { Serial.begin(115200); delay(dT) ; Serial.println(); Serial.print("Connecting to network: "); Serial.println(ssid); WiFi.disconnect(true); //disconnect form wifi to set new wifi connection WiFi.begin(ssid, password); //connect to wifi while (WiFi.status() != WL_CONNECTED) { delay(dT); Serial.print("."); counter++; if (counter >= 60) { //after 30 seconds timeout - reset board (on unsucessful connection) ESP.restart(); } // Init and get the time configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address set: "); Serial.println(WiFi.localIP()); //print LAN IP Serial.print(F("Hello! ST77xx TFT Test")); pinMode(TFT_BL, OUTPUT); // TTGO T-Display enable Backlight pin 4 digitalWrite(TFT_BL, HIGH); // T-Display turn on Backlight tft.init(135, 240); // Initialize ST7789 240x135 Serial.println(F("Initialized")); tft.fillScreen(ST77XX_BLACK); } void loop() { if (WiFi.status() == WL_CONNECTED) { //if we are connected to eduroam network counter = 0; //reset counter } else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry WiFi.begin(ssid); } while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots delay(dT); Serial.print("."); counter++; if (counter >= 60) { //30 seconds timeout - reset board ESP.restart(); } } struct tm timeinfo; if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); return; } Serial.println(getTemperature()); tft.setRotation(3); tft.setTextSize(2); tft.setTextColor(ST77XX_YELLOW); tft.setTextWrap(true); tft.setCursor(0, 0); tft.print("IP: "); tft.setCursor(40, 0); tft.print(WiFi.localIP()); tft.setCursor(0, 25); tft.print("Date/Time: "); tft.setCursor(0, 50); tft.print(&timeinfo, "%b %d, %Y %H:%M"); tft.setCursor(0, 75); tft.print("Temperature: "); tft.setCursor(74, 100); tft.setTextSize(3); tft.print(getTemperature()); }
Here's hoping that someone might have a solution to this issue. All and any comments or suggestions are greatly appreciated.
-
@witchdoc Ooof! This is a classic case of not clearing the printed text before the next display update!
The text 'compounds' until it is filled in completely.
Your time display is the clue.
At the top of your print results put in:tft.fillScreen(TFT_BLACK);
or if that causes flickering try
tft.fillRect(h, v, h size, v size, TFT_BLACK);
for example, where needed.
This second one is a pain to get the horizontal, vertical and box dimensions perfect.Try that!
-Terry -
@teastain2 Ok that worked a treat. The temperature is displaying as it should. Now to figure out why it's all over the place. Thermometer on my desk reads 23.5 while my Lilygo is displaying Anywhere from 17 to 20 degrees. And it's changing every loop of the void loop. And I still have to deal with the time display being corrupted.
Gonna look more into millis().
Thanks for the assistance.WD
-
@witchdoc what temperature sensor are you using?
I see an LM-35, but they are analog, and the ESP32 is a terrible ADC, with linearity and stability issues, because its value changes with the ESP32 core temp and internal voltage fluctuations! -
@teastain2 Thanks for that information. How about the DS18B20? Would it work better? Looking now for the datasheet on the DS.
-
@witchdoc DS18B20 is the best low-end temp sensor I’ve seen.
It is digital, with a simple serial interface.
You need two libraries to run it, but its use is well documented.
Cheers,I have a test sketch for it running on LilyGO if you are interested, I will post it here.
-
@teastain2 I'd be interested to see how you did it. I've got it running on my T-Display. My multimeter with a thermocouple on it reads 22 degrees. My cheap 3 dollar Chinese thermometer reads 23.4 and my DS18B20 is reading 23.87. At the moment the temperature is flashing on and off as the loop, loops. And I've yet to deal with the time display getting corrupted.
-
@witchdoc
Prints to serial as I do not have a TTGO!#include <OneWire.h> #include <DallasTemperature.h> OneWire oneWire(1); //pin 1 or your choice! DallasTemperature sensors(&oneWire); void setup() { Serial.begin(115200); delay(200); Serial.println(" setup DS18B20 "); delay(200); sensors.begin(); } void loop() { sensors.requestTemperatures(); float temperatureC = sensors.getTempCByIndex(0); Serial.print(temperatureC); Serial.println("ºC"); Serial.println(" "); delay(2000); }
-
@teastain2 Thanks for the script. I found a solution to the blinking text. I have another display running on another ESP32 and I realized that it wasn't blinking. It is running a GPS program and displaying the time which always looks crisp and clean so I took a look at that script and realized it was using a different display library. TFT_eSPI. Took a bit of tinkering but I got a display that shows the temperature perfectly.```
#include <TFT_eSPI.h> // Hardware-specific library
#include <OneWire.h>
#include <DallasTemperature.h>TFT_eSPI tft = TFT_eSPI();
// GPIO where the DS18B20 is connected to
const int oneWireBus = 32;float getTemperature(){
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
sensors.begin();
//delay(dT) ;sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
//float temperatureF = sensors.getTempFByIndex(0);
return(temperatureC);
}void setup() {
Serial.begin(115200);
//delay(dT) ;
pinMode(TFT_BL, OUTPUT); // TTGO T-Display enable Backlight pin 4
digitalWrite(TFT_BL, HIGH); // T-Display turn on Backlight
tft.init(); // Initialize ST7789 240x135
Serial.println(F("Initialized"));
tft.fillScreen(TFT_BLACK);
}void loop() {
tft.setRotation(1);
tft.setTextColor(TFT_GREENYELLOW,TFT_BLACK);
tft.setCursor(0, 0);
tft.print("Temperature: ");
tft.setCursor(74, 50);
tft.setTextSize(3);
tft.print(getTemperature());
} -
@witchdoc Well it looks like I spoke to soon. I converted my thermometer sketch to the tft_espi library and it's doing the same thing that the adafruit library does. Initially it is very clear but within the first second the temperature reading begins to corrupt as does the time display. Not sure why since the test sketch with the espi library was so perfect.
-
@witchdoc Can you post your sketch here and I will have a look?
The key is that new text is not being erased, it is just building up like a rubber stamp, changing the time and over printing each time. It may not be the library, but your code!
-
@teastain2 I figured it out. With the eSPI library, when you specify text color you have to specify a background color. So by entering the following tft.setTextColor(TFT_GREENYELLOW,TFT_BLACK); the display will display a non blinking text.
-
@witchdoc Perfect! Thanks for getting back to me.