Issue showing temperature on TFT Display

Hello everyone,

I'm new to Arduino and want to show the temperature of a DHT11 temperature sensor on my Elegoo TFT 2.8 zoll display. I'm using an Arduino UNO R3, Arduino IDE as desktop app and windows 11.
I don't use the shield and I connected the data pin of the sensor with the digital pin 2.
(The code is also for the measurement of the voltage of a batterie but I haven't got this sensor yet. )
The values of the temperature are displayed correct in the serial monitor but not on my TFT Display. There it always shows "0.00". (green)

Can you help me optimize my code to display the actual values of the temperature on the TFT display? Thank you very much.

This is my code so far. I used a mix of the elegoo tft examples and the example for the DHT11 Sensor.

#include <pin_magic.h>
#include <registers.h>
#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>

#include <pin_magic.h>
#include <registers.h>

// IMPORTANT: Elegoo_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Elegoo_TFTLCD.h FOR SETUP.

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library

//Start Definition Temperaturmessung
#include <dht_nonblocking.h> // Bibliothelk für den Temperatursensor
#define DHT_SENSOR_TYPE DHT_TYPE_11 //Sensortyp definieren
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
    float temperature;
  float humidity;
//Ende Definition Temperaturmessung


// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
//   D0 connects to digital pin 8  (Notice these are
//   D1 connects to digital pin 9   NOT in order!)
//   D2 connects to digital pin 2
//   D3 connects to digital pin 3
//   D4 connects to digital pin 4
//   D5 connects to digital pin 5
//   D6 connects to digital pin 6
//   D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

 Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
//Elegoo_TFTLCD tft;

//----------------------------Spannungsüberwachung Lipo --------------------------------------
/*
 * Spannungsüberwachung einer Lipozelle
 * Lipozelle zwischen A0 und GND angeschlossen
 */
 const byte uBatPin = A0;
 float uBat;
 byte reichweite;
//---------------------------------------------------------------------------------------------

//----------------------------Temperaturüberwachung  ------------------------------------------
//const byte regler = 2; //Der Sensor soll am analogen Pin A1 angeschlossen werden. Wir nennen den Pin ab jetzt "regler"
//int sensorwertRegler;
//int temperaturRegler = 1; //Unter der Variablen "temperaturRegler" wird später der Temperaturwert abgespeichert.
//Deklaration einer Klasse

//---------------------------------------------------------------------------------------------
void setup(void) {
  Serial.begin(9600);
  //Serial.println(F("TFT LCD test"));
#ifdef USE_Elegoo_SHIELD_PINOUT
  Serial.println(F("Using Elegoo 2.8\" TFT Arduino Shield Pinout"));
#else
  Serial.println(F("Using Elegoo 2.8\" TFT Breakout Board Pinout"));
#endif

  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());

  tft.reset();

  uint16_t identifier = tft.readID();
     identifier=0x9341;
  if(identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if(identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if(identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_Elegoo_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    return;
  }

  tft.begin(identifier);

  Serial.println(F("Done!"));
  tft.setRotation(3);
  tft.fillScreen(BLACK); // Zur Vermeidung des Ziffern überschreibens nötig!
  tft.setCursor(0, 0);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Innentemp.");
  tft.println("");
  tft.println("Uhrzeit");
  tft.println("");
  tft.println("");
  tft.println("");
  tft.println("Reichweite");
  tft.println("");
  tft.setCursor(300, 0);
  tft.setTextColor(WHITE);
  tft.println("C");
  tft.setCursor(200, 145);
  tft.setTextColor(WHITE);
  tft.println("KM");
  //----------------------------Spannungsüberwachung Lipo --------------------------------------
  tft.setTextColor(GREEN);
  tft.setCursor(200, 200);
  tft.println("V");
  tft.setCursor(0, 200);
  tft.println("Spannung");

}

void loop(void) {
    Batterie();
    Temperatur();
    static uint32_t startzeit;
   
    if(millis()-startzeit>1000)
    {
      
     ausgabeTemperatur();
     ausgabeSpannung();
     startzeit = millis();
    }



  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
     if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature,1);
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
     tft.setTextColor( GREEN, BLACK); // Zur Vermeidung des Ziffern überschreibens nötig!
  tft.setCursor(200, 0);
  tft.println(temperature);
  }
  }
//----------------------------Temperaturüberwachung  ------------------------------------------
  void ausgabeTemperatur()
  {

  tft.setTextColor( GREEN, BLACK); // Zur Vermeidung des Ziffern überschreibens nötig!
  tft.setCursor(200, 0);
  tft.println(temperature);
   Serial.print ("Innentemperatur: ");
  Serial.println (temperature);
  } 

//---------------------------------------------------------------------------------------------
  void ausgabeSpannung()
  {
  //tft.setTextColor(WHITE);
  tft.setTextColor(WHITE, BLACK); // Zur Vermeidung des Ziffern überschreibens nötig!
  tft.setCursor(250, 145);
  tft.println(reichweite); 
  tft.setCursor(240, 200);
  tft.println(uBat);
  tft.setCursor(280, 200);
  Serial.print ("reichweite  ");
  Serial.println (reichweite);
  Serial.print("uBat= ");
  Serial.println(uBat);
  }


//----------------------------Spannungsüberwachung Lipo --------------------------------------
  void Batterie()
  {
  uBat = analogRead(uBatPin)*4.2/1024;// 4.2ch die gemessene Versorgungsspannung ersetzen
  
  if(uBat>4.1) reichweite = 100;
  if(uBat<4.1 && uBat>4.0) reichweite =75;
  if(uBat<4.0 && uBat>3.9) reichweite =50;
  if(uBat<3.9 && uBat>3.8) reichweite =25;
  if(uBat<3.8 && uBat>3.6) reichweite =0;
 }

//----------------------------Temperaturüberwachung  ------------------------------------------
void Temperatur()
{
//temperature=digitalRead(DHT_SENSOR_PIN); //Auslesen des Sensorwertes.
//temperaturRegler= map(sensorwertRegler, 0, 410, -50, 150); //Umwandeln des Sensorwertes mit Hilfe des "map" Befehls.

 //Serial.begin( 9600);
}
/* Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.*/
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}
/*
 * Main program loop.
 */

//---------------------------------------------------------------------------------------------

I can't see exactly who is the culprit, but I see many "strange" things here, kinda mixed and blended code...

First, the temperature is being printed (on both serial and tft) on more than one place...
See the loop:

void loop(void) {
    Batterie();
    Temperatur();
    static uint32_t startzeit;
   
    if(millis()-startzeit>1000)
    {
     ausgabeTemperatur();
     ausgabeSpannung();
     startzeit = millis();
    }
  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature,1);
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
    tft.setTextColor( GREEN, BLACK); // Zur Vermeidung des Ziffern überschreibens nötig!
    tft.setCursor(200, 0);
    tft.println(temperature);
  }
}

every second you print it on the ausgabeTemperatur() function, and that "measure_environment" function reads the "temperature" variable every three seconds (despite the comment stating "four seconds"). What's the deal to print every second something you read every 3 seconds? Why not read and update the values using a single timer (e.g. 1 per second)?

And what "serial output" you're referring to, from the loop if() or ausgabeTemperatur() function?
I can't assemble a test structure (even on simulators...) so please show us the serial output corresponding to this issue, just to keep track of the flow from your code...

Hello thank you for your response!

I tried to optimice the time for the measurement and the time for the print out and now it is working!

This is my updated code, but I think it is possible to optimice it a little bit more as you stated that it is strange with the several print outs. Also 55% of the ram is already occupied. (The sensor also measures humidity and shows this in the serial monitor.)

#include <pin_magic.h>
#include <registers.h>
#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>


#include <pin_magic.h>
#include <registers.h>

// IMPORTANT: Elegoo_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Elegoo_TFTLCD.h FOR SETUP.

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library

//Start Definition Temperaturmessung
#include <dht_nonblocking.h> // Bibliothelk für den Temperatursensor
#define DHT_SENSOR_TYPE DHT_TYPE_11 //Sensortyp definieren
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
    float temperature;
  float humidity;
//Ende Definition Temperaturmessung


// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
//   D0 connects to digital pin 8  (Notice these are
//   D1 connects to digital pin 9   NOT in order!)
//   D2 connects to digital pin 2
//   D3 connects to digital pin 3
//   D4 connects to digital pin 4
//   D5 connects to digital pin 5
//   D6 connects to digital pin 6
//   D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

 Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
//Elegoo_TFTLCD tft;

//----------------------------Spannungsüberwachung Lipo --------------------------------------
/*
 * Spannungsüberwachung einer Lipozelle
 * Lipozelle zwischen A0 und GND angeschlossen
 */
 const byte uBatPin = A0;
 float uBat;
 byte reichweite;
//---------------------------------------------------------------------------------------------

//----------------------------Temperaturüberwachung  ------------------------------------------
//const byte regler = 2; //Der Sensor soll am analogen Pin A1 angeschlossen werden. Wir nennen den Pin ab jetzt "regler"
//int sensorwertRegler;
//int temperaturRegler = 1; //Unter der Variablen "temperaturRegler" wird später der Temperaturwert abgespeichert.
//Deklaration einer Klasse

//---------------------------------------------------------------------------------------------
void setup(void) {
  Serial.begin(9600);
  //Serial.println(F("TFT LCD test"));
#ifdef USE_Elegoo_SHIELD_PINOUT
  Serial.println(F("Using Elegoo 2.8\" TFT Arduino Shield Pinout"));
#else
  Serial.println(F("Using Elegoo 2.8\" TFT Breakout Board Pinout"));
#endif

  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());

  tft.reset();

  uint16_t identifier = tft.readID();
     identifier=0x9341;
  if(identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if(identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if(identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_Elegoo_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    return;
  }

  tft.begin(identifier);

  Serial.println(F("Done!"));
  tft.setRotation(3);
  tft.fillScreen(BLACK); // Zur Vermeidung des Ziffern überschreibens nötig!
  tft.setCursor(0, 0);
  tft.setTextColor(WHITE);
  tft.setTextSize(2.5);
  tft.println("Innentemp.");
  tft.println("");
  tft.println("Uhrzeit");
  tft.println("");
  tft.println("");
  tft.println("");
  tft.println("Reichweite");
  tft.println("");
  tft.setCursor(300, 0);
  tft.setTextColor(WHITE);
  tft.println("C");
  tft.setCursor(200, 145);
  tft.setTextColor(WHITE);
  tft.println("KM");
  //----------------------------Spannungsüberwachung Lipo --------------------------------------
  tft.setTextColor(GREEN);
  tft.setCursor(200, 200);
  tft.println("V");
  tft.setCursor(0, 200);
  tft.println("Spannung");

}

void loop(void) {
    Batterie();
    Temperatur();
    static uint32_t startzeit;
   
    if(millis()-startzeit>2000)
    {
      
     ausgabeTemperatur();
     ausgabeSpannung();
     startzeit = millis();
    }



  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
     if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature,1);
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
  //   tft.setTextColor( GREEN, BLACK); // Zur Vermeidung des Ziffern überschreibens nötig!
 // tft.setCursor(200, 0);
 // tft.println(temperature,1);
  }
  }
//----------------------------Temperaturüberwachung  ------------------------------------------
  void ausgabeTemperatur()
  {
  tft.setTextColor( GREEN, BLACK ); // Zur Vermeidung des Ziffern überschreibens nötig!
  tft.setCursor(150, 0);
  tft.println(temperature);
   Serial.print ("Innentemperatur: ");
  Serial.println (temperature);
  } 

//---------------------------------------------------------------------------------------------
  void ausgabeSpannung()
  {
  //tft.setTextColor(WHITE);
  tft.setTextColor(WHITE, BLACK); // Zur Vermeidung des Ziffern überschreibens nötig!
  tft.setCursor(250, 145);
  tft.println(reichweite); 
  tft.setCursor(240, 200);
  tft.println(uBat);
  tft.setCursor(280, 200);
  Serial.print ("reichweite  ");
  Serial.println (reichweite);
  Serial.print("uBat= ");
  Serial.println(uBat);
  }


//----------------------------Spannungsüberwachung Lipo --------------------------------------
  void Batterie()
  {
  uBat = analogRead(uBatPin)*4.2/1024;// 4.2ch die gemessene Versorgungsspannung ersetzen
  
  if(uBat>4.1) reichweite = 100;
  if(uBat<4.1 && uBat>4.0) reichweite =75;
  if(uBat<4.0 && uBat>3.9) reichweite =50;
  if(uBat<3.9 && uBat>3.8) reichweite =25;
  if(uBat<3.8 && uBat>3.6) reichweite =0;
 }

//----------------------------Temperaturüberwachung  ------------------------------------------
void Temperatur()
{
//temperature=digitalRead(DHT_SENSOR_PIN); //Auslesen des Sensorwertes.
//temperaturRegler= map(sensorwertRegler, 0, 410, -50, 150); //Umwandeln des Sensorwertes mit Hilfe des "map" Befehls.

 //Serial.begin( 9600);
}
/* Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.*/
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}
/*
 * Main program loop.
 */

//---------------------------------------------------------------------------------------------



This is the serial output:

Using Elegoo 2.8" TFT Breakout Board Pinout
TFT size is 240x320
Found ILI9341 LCD driver
Done!
Innentemperatur: 0.00
reichweite  100
uBat= 4.20
Innentemperatur: 0.00
reichweite  100
uBat= 4.20
T = 21.0 deg. C, H = 13.0%
Innentemperatur: 21.00
reichweite  100
uBat= 4.20
Innentemperatur: 21.00
reichweite  100
uBat= 4.20
T = 20.0 deg. C, H = 15.0%
Innentemperatur: 20.00
reichweite  100
uBat= 4.20
Innentemperatur: 20.00
reichweite  100
uBat= 4.20
T = 20.0 deg. C, H = 15.0%
Innentemperatur: 20.00


Well, if the program now works (besides, I see now the display output format is pretty changed in size...), about the RAM there's nothing special to do, I think most of the memory is used by the included libraries.
To optmize the code I'd have to go deep in the flow and usage so at the moment I can't say much. But the first thing is to apply a good code indentation because it isn't easily readable at the moment. Start by pressing Ctrl-T from IDE, it will do the job for you, and keep that syntax.
Then you could remove some unnecessary functions like "Temperatur()" (it's empty) and all old and commented out statements. Together with any multiple consecutive empty lines to avoid the code from being unnecessarily lenghty: if you want/need to separate portions of code for visual reasons, a single empty line (or a single line of comment) is enough.
Lastly, I think you should review the meaning/purpose of the functions you defined. I can't speak german so I can't fully understand what you had in mind, anyway once you define what a function does you must keep it consistent with the rest of the code. For instance, if "ausgabeTemperatur" function should print on the LCD the temperature value, I think you should call it only if the values are changed/read back, there's no need to do it periodically, and make sure you removed any other LCD output (you can keep the Serial prints to locally show the values for debug purposes, but only when you read new values e.g. remove unnecessary duplicates elsewhere).

With those things in mind, I'd change the loop this way as an example (it obviously needs some other changes on the called functions), see how it becomes more readable and simpler:

...
// LCD display update interval
#define UPDATE_INTERVAL 2000
...
void loop() 
{
  static uint32_t startzeit;

  if(millis()-startzeit > UPDATE_INTERVAL) 
  {
    startzeit = millis();

    // Update the values
    dht_sensor.measure( temperature, humidity )
    Batterie();
    
    // Debug printouts
    DebugOutput();
    
    // show the updated values
    ausgabeTemperatur();
    ausgabeSpannung();
  }
}

// Debug output: show the values via Serial for debugging
void DebugOutput() {
  Serial.print( "T = " );
  Serial.print( temperature,1);
  Serial.print( " deg. C, H = " );
  Serial.print( humidity, 1 );
  Serial.println( "%" );
  Serial.print ("reichweite  ");
  Serial.println (reichweite);
  Serial.print("uBat= ");
  Serial.println(uBat);
}
...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.