KMR 1.8 SPI TFT Display Print to TFT display Baro, Temp, Humidity And Altitude

Good Day To All,

Need help on my current project, I have done a Weather station project and have been able to Display all the reading nicely on the serial Print.(as per attached pic). Currrently im trying to print the available data to my newly bought 1.8 tft display. I have managed to wired the stuff as per

http://henrysbench.capnfatz.com/henrys-bench/arduino-displays/1-8tft-v1-0-st7736-arduino-tutorial-1-hello-world/

The screen flikers and i cant seem to get the right code to display the value on my tft SCREEN.

I would appreciate if there is any guru out there who can help me on the matter. It would be very great.

/*
  Pin Configuration
  Rain Drop      A0
  Soil Humidity  A1
  SDA            A4
  SCL            A5
  DHT22 Data     D7
*/
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>


// For the breakout, you can use any 2 or 3 pins
// These pins will also work for the 1.8" TFT shield
#define TFT_CS     10
#define TFT_RST    9  // you can also connect this to the Arduino reset
                      // in which case, set this #define pin to 0!
#define TFT_DC     8

// Option 1 (recommended): must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC, TFT_RST);

// Option 2: use any pins but a little slower!
#define TFT_SCLK 13   // set these to be whatever pins you like!
#define TFT_MOSI 11   // set these to be whatever pins you like!
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);


float p = 3.1415926;
int result [5];           //Sensor result array
#include <Wire.h>         //Including wire library
#include <SFE_BMP180.h>   //Including BMP180 library
#define ALTITUDE  50      //Altitude Taman Melawati KL
SFE_BMP180 pressure;      //Creating an object
#include <DHT.h>          //Including DHT22 library
#define DHTPIN 7          //Define DHT22 digital pin
#define DHTTYPE DHT22     //Define DHT TYPE which is DHT11
DHT dht(DHTPIN, DHTTYPE); //Execute DHT22 library

void setup() {
  Serial.begin(9600);       //Starting serial communication
  Serial.println("Program started");
  //Analog setup
  pinMode(A0, INPUT);       //Soil
  pinMode(A1, INPUT);       //Rain
  //BMP180 Setup
  if (pressure.begin())     //If initialization was successful, continue
  { Serial.println("BMP180 Functions Success");
  }
  else                       //Else, stop code forever
  {
    Serial.println("BMP180 Sensor Fail ");
    while (1);
  }
  //DHT22 setup
  dht.begin();
}

void loop() {
  //analog setup
  int A_Rain = analogRead(A0);
  int A_Soil = analogRead(A1);
  A_Rain = map(A_Rain, 800, 1023, 100, 0);
  A_Soil = map(A_Soil, 400, 1023, 100, 0);
  result[0] = A_Soil;
  result[1] = A_Rain;
  //bmp180 setup
  char status;
  double T, P, p0, a;
  status = pressure.startTemperature();
  if (status != 0) {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0) {
      status = pressure.startPressure(3);
      if (status != 0) {
        delay(status);
        status = pressure.getPressure(P, T);
        if (status != 0) {
          p0 = pressure.sealevel(P, ALTITUDE);
          result[2] = p0;

float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println();
Serial.print("Temperature:      ");
Serial.print(t);
Serial.print(" Degrees Celcius ");
Serial.println();
Serial.print("Humidity:         ");
Serial.print(h);
Serial.print("%");
Serial.println();

//--- extra---- you can measure dew point with the temperature and the humidity

double gamma = log(h/100) + ((17.62*t) / (243.5+t));
double dp = 243.5*gamma / (17.62-gamma);


Serial.print("Dew point:        ");
Serial.print(dp);
Serial.print(" Degrees Celcius ");
Serial.println();
       
        }
      }
    }
  }
  result[3] = dht.readHumidity();
  result[4] = dht.readTemperature();
  Serial.println();
  Serial.print("Soil Humidity : ");
  Serial.print(" \t");
  Serial.print(result[0]);
  Serial.print(" %\t");
  Serial.print("Rain Drop Percentage : ");
  Serial.print("\t");
  Serial.print(result[1]);
  Serial.print(" %\t");
  Serial.print("Presssure : ");
  Serial.print("\t");
  Serial.print(result[2]);
  Serial.print(" hPa \t");
  Serial.print("Air Humidity : ");
  Serial.print(result[3]);
  Serial.print(" %\t");
  Serial.print("\t");
  Serial.print("Temperature : ");
  Serial.print(result[4]);
  Serial.println(" C \t");
  Serial.println();
  Serial.print ("BMP 180 Readings");
  Serial.println("");
  Serial.print("Provided Altitude:                ");
  Serial.print(ALTITUDE, 0);
  Serial.print(" meters, ");
  Serial.print(ALTITUDE * 3.28084, 0);
  Serial.println(" feet");
  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      Serial.print("Temperature:                      ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
      
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P, T);
        if (status != 0)
        {
          // Print out the measurement:
          Serial.print("Absolute Pressure:                ");
          Serial.print(P, 2);
          Serial.print(" hPa, ");
          Serial.print(P * 0.0295333727, 2);
          Serial.println(" inHg");

          // The pressure sensor returns abolute pressure, which varies with altitude.
          // To remove the effects of altitude, use the sealevel function and your current altitude.
          // This number is commonly used in weather reports.
          // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
          // Result: p0 = sea-level compensated pressure in mb

          p0 = pressure.sealevel(P, ALTITUDE); // we're at 1655 meters (Boulder, CO)
          Serial.print("Relative (Sea-Level) Pressure:    ");
          Serial.print(p0, 2);
          Serial.print(" hPa, ");
          Serial.print(p0 * 0.0295333727, 2);
          Serial.println(" inHg");

          // On the other hand, if you want to determine your altitude from the pressure reading,
          // use the altitude function along with a baseline pressure (sea-level or other).
          // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
          // Result: a = altitude in m.

          a = pressure.altitude(P, p0);
          Serial.print("Computed Altitude:                ");
          Serial.print(a, 0);
          Serial.print(" meters, ");
          Serial.print(a * 3.28084, 0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
  delay(5000);
}

I am gobsmacked by the “quality” of your internet link. It is nicely presented. At first glance it looks as if it is an authoritative tutorial.

Operating off of 3.3V, the TFT has a resolution of 128 x 160 pixels.

Yes, the controller requires 3.3V logic. It has a LDO voltage regulator on the pcb that provides 3.3V to the controller.

NOTE – You should really use a level shifter (or at least 510 resistors) between the Arduino outputs and display inputs. This will work, but you may cause damage or deterioration over time.

Yes, of course you should use level shifters on every logic pin.

Why should “obeying specification” cause deterioration?

The “tutorial” then shows an appalling wiring scheme that does not even connect the display VCC or GND pins.
It supplies the backlight with 3.3V. This is ok for a small display. You could connect to 5V with a 47R series resistor if you want.

Yes, you can level-shift safely in several ways:

  1. voltage divider e.g. 4k7 / 10k resistors
  2. integrated circuits e.g. 74HC4050
  3. series resistors e.g. 4k7

But quite honestly, it is simpler to use a proper 3.3V MCU in the first place

  1. Uno clone with switchable 3.3V logic e.g. Seeeduino
  2. 3.3V Pro Mini
  3. Zero
  4. Due

David.

david_prentice:
I am gobsmacked by the "quality" of your internet link. It is nicely presented. At first glance it looks as if it is an authoritative tutorial.
Yes, the controller requires 3.3V logic. It has a LDO voltage regulator on the pcb that provides 3.3V to the controller.
Yes, of course you should use level shifters on every logic pin.

Why should "obeying specification" cause deterioration?

The "tutorial" then shows an appalling wiring scheme that does not even connect the display VCC or GND pins.
It supplies the backlight with 3.3V. This is ok for a small display. You could connect to 5V with a 47R series resistor if you want.

Yes, you can level-shift safely in several ways:

  1. voltage divider e.g. 4k7 / 10k resistors
  2. integrated circuits e.g. 74HC4050
  3. series resistors e.g. 4k7

But quite honestly, it is simpler to use a proper 3.3V MCU in the first place

  1. Uno clone with switchable 3.3V logic e.g. Seeeduino
  2. 3.3V Pro Mini
  3. Zero
  4. Due

David.

Thanks david for the reply, really appreciate it. I have a node MCU in hand Lolin V3. My question is:-

a) Is the code that im using is okay for the node MCU. A level shifter is on its way from my ordering.
b) Appreciate if you could share me a link on how to print sensor reading using MCU via the 1.8 TFT.

  • or some example code that is needed to merge with my current coding.
    c) I would love to learn programming language for Arduino. I currently on copy paste and try and error with a bit of editing, I you could share a site where i could learn the programming language of arduino from basic.

I was goint to make my weather station iot able in future but i am currently tinkering with uno 1st. My ultimate project is goint to be an outdoor plant and weather monitoring system with remode watering capability. I would love to share my project once its completed.

Hope you could help. And thank you so much for your help

Your NodeMcu board has 3.3V logic. No need for any level shifters.

Bodmer has a TFT_eSPI library designed for ESP8266 and ESP32 boards. There are lots of examples. You will be impressed by how well your display performs with this library.

Bodmer also has a library for AVR boards. The library link shows how to connect KMR1,8 board to a 5V Uno. Simple series resistors can be all you require. Personally I prefer to just run a 3.3V board without level shifters.

David.

Thanks David.will check out Bodmer tutorial, and see whats the result. Thank you so much.