Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15514

Issue showing temperature on TFT Display

$
0
0

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.
 */

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

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 15514

Trending Articles