Having issues displaying sensor values on display

Hey everybody! So I been at my little project for a bit now, I thought I finally got it all done, but I was really wrong. (I'm using TFT ips display, 2inch")

I have three sensors hooked up (Light Sensor, Moisture sensor and tempature/Humidity sensor)

FYI temp and Humid sensor are getting shipped ATM.

But for the life of me, I can't figure out how to utilize the whole 2inch display, it's stuck in the top left corner. So I'm just wondering if anybody could nudge me to the right direction on how to do that? :slight_smile:

//#include <DFRobot_DHT11.h>
#include <TFT.h>  
#include <SPI.h>
//#include <Wire.h>
#define cs   10
#define dc   9
#define rst  -1
//#define DHTPIN 7
#define LRPIN A0
#define led 4
#define buz 6
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
//DFRobot_DHT11 DHT;

float humidity,temperature,LDR;


String temp,hum,soil_moist,ligh_sensor;

int soil_moisture,SM;

int light_sensor,LS;



char T[5],H[5],moist[3],sensor[2];

void display_data_serial()
 {
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" deg C");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %RH");
  Serial.print("soil moisture: ");
  Serial.print("SM");
  Serial.println('%');  
  Serial.print("Light Amount: ");
  Serial.print("LS");
  Serial.println('%');
  }
void setup() 
{
  
  
  TFTscreen.begin();    
  TFTscreen.background(128,128,128);
  TFTscreen.fill(128,128,128);
  Serial.begin(9600);
  Serial.println("temperature, humidity and soil moisture monitoring program");
  TFTscreen.setTextSize(2);  
  TFTscreen.stroke(255,0,255);  
  TFTscreen.text("Tem: ", 0, 10);
  TFTscreen.text(" *C", 115, 15);
  TFTscreen.stroke(0,255,255);  
  TFTscreen.text("Hum: ", 0, 60);
  TFTscreen.text(" %RH", 113, 60);
  TFTscreen.stroke(255,255,0);  
  TFTscreen.text("Moist: ", 0,100);
  TFTscreen.text("%", 110, 95);
  TFTscreen.stroke(255,255,255);
  TFTscreen.text("LS: ",0, 110);
  TFTscreen.text("%", 140, 110);
  TFTscreen.setTextSize(3);  
  pinMode(led,OUTPUT);
  pinMode(buz,OUTPUT);
}

void loop() 
{
 // DHT.read(DHTPIN);
 // humidity = DHT.humidity;
//  temperature = DHT.temperature;
  //digitalWrite(led,1);
  //digitalWrite(buz,1);
  light_sensor = analogRead(A0);
  soil_moisture = analogRead(A1);
  SM = map(soil_moisture,10,1000,100,0);
  LS = map(light_sensor,10,1000,100,0);
  display_data_serial();
  hum = String(humidity);
  temp = String(temperature);
  soil_moist = String(SM);  
  hum.toCharArray(H,5);
  temp.toCharArray(T,5);
  soil_moist.toCharArray(moist,3);  
  ligh_sensor.toCharArray(sensor, 2);
  TFTscreen.stroke(0,0,200);  
  TFTscreen.text(T,50,5);
  TFTscreen.stroke(0,200,0);  
  TFTscreen.text(H,50,55);
  TFTscreen.stroke(200,0,0);  
  TFTscreen.text(moist,70,95);
  TFTscreen.text(sensor,80,95);
  delay(200);
  digitalWrite(led,0);
  digitalWrite(buz,0);
  delay(1800);
  TFTscreen.stroke(0,0,0); 
  TFTscreen.text(T,50,5);
  TFTscreen.text(H,50,55);
  TFTscreen.text(moist,70,95); 
TFTscreen.text(sensor, 80, 95);
  
delay(200);
}

The 70 and 95 numbers here are coordinates on the screen. Isn't it simply a matter of adjusting them?

I couldn't help noticing how over-complicated your code is to achieve simple things. It's often a problem for beginners to figure out when they could make code much shorter and easier to understand. For example:

You start out by reading the analog pin, then using map() to change the range of the readings and assign the result to a second variable. The first variable never gets used again until the next reading is taken.

You could simplify this by either re-using the same variable:

  soil_moisture = analogRead(A1);
  soil_moisture = map(soil_moisture,10,1000,100,0);

or by combining both lines:

  soil_moisture = map(analogRead(A1),10,1000,100,0);

Next, you convert the value into a String, and then convert that to a char array (a C-string). You probably don't understand the difference between a String and a char array at this point, but I can tell you that these are unnecessary steps that over complicated the code.

You finish up using 4 variables all containing the same value in different formats!
soil_moisture
SM
soil_moist
moist

I suggest having a single char array, a buffer, and re-using it for each value you want to display on the screen:

char buffer[20]; // a global variable

  ...
  sprintf(buffer, "%3d", soil_moisture);
  TFTscreen.text(buffer,70,95)

Hey! Thanks for the pointers. Still learning, so I appreciate it!! I know it's definitely a messy code, and certainly makes things more complicated then it needs to be, buttt you live and you learn :relaxed:

Also, I tried adjusting those numbers(Coordinates) but no luck with that sadly

Odd...

The other odd thing in my mind is those two button icons, the triangle and the pause/equals symbol. I don't see anything in your code that draws them, or anything that limits the text to that top left area of the screen.

Where did you install or download that TFT library from?

1 Like

as long as somebody is a beginner the most condensed code is complicated to understand because there is so much going on in a single line.

@ascendingc
post the exact type of your display
I'm very sure that there are demo-codes that show how to use them.

best regards Stefan

1 Like

Absolutely true. But we are nowhere near the most condensed code here. There is a balance to be found. Having multiple variables holding the same value at different stages of formatting can easily lead to confusion and mistakes.

Hey! Yeah that pause button/Triangle symbol is part of a test library, I believe from: https://github.com/adafruit/Adafruit-ST7735-Library/blob/master/Adafruit_ST7789.cpp

just to test the screen/Draw lines to it. And for some reason it's still there which is confusing cause it shouldn't be.

Also, my display is ST7899 TFT IPS Display 2".

And the library is this one: https://github.com/arduino-libraries/TFT

So you have changed libraries at some point. Perhaps that Adafruit library has left the screen in some strange configuration. If so, I would imagine that disconnecting the power to the screen would reset it.

oh yeah, that reset it.

currently doing some research to see if i can expand that box, but not going to well.

On that page, it says

For more information about this library please visit us at

And on the page linked there, it says

Note: this library was retired and is no longer maintained.

So perhaps you should be considering another library.

shucks. I'll attempt to redo this all using the Adafruit ST7789 libary, since the "Example Libary" from it worked flawlessly.

1 Like

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