Print to TFT display temperature sensor DS18b20 readings, not to serial monitor.

Hi,

I have been trying to find how to print the temperature readings from 2 DS18b20 sensors to a TFT display. I am using an Adafruit ST7735 160 x 128 display.
I have used the code below compiled from a few places on the net which works fine with the serial monitor.
So far I have figured out how to print the static text and change font, colour, position and size. I have not figured out how to obtain and display the temperature reading to the TFT display from the DS18b20 sensors.

#include <SPI.h>
#include <TFT.h>

// Libraries for DS1820 and Onewire
#include <OneWire.h>
#include <DallasTemperature.h>


//You can use any (4 or) 5 pins  
//#define sclk 4 
//#define mosi 5 
#define cs 6 // pin definition for the Uno
#define dc 7 // pin definition for the Uno
#define rst 8  // pin definition for the Uno
// you can also connect this to the Arduino reset

float myTemp0;
float myHighTemp0;
float myLowTemp0 = 50;
float myTemp1;
float myHighTemp1;
float myLowTemp1 = 50;

OneWire oneWire(2); // Setup onewire instance to communicate with devices.
DallasTemperature sensors(&oneWire);

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char sensorPrintout[4];

void setup()
{
   // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();
  // clear the screen with a black background
  TFTscreen.background(0, 0, 255);
    // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(0,0,0);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("Temperatures :\n ",10,0);
  // set the font color to white
  TFTscreen.stroke(50,50,50);
  // set the font size
  TFTscreen.setTextSize(1);
  // write the text to the top left corner of the screen
  TFTscreen.text("Fridge Temperature :\n ",10,20);
  // write the text to the top left corner of the screen
  TFTscreen.text("Pot One Temperature :\n ",10,30);
  // ste the font size very large for the loop
  TFTscreen.setTextSize(1); 
  
  
 sensors.begin(); 
 Serial.begin(9600);
}

void loop()
{
  readtemp(); // Read the temperature.
  serialprint(); // Write the results to the serial monitor.
  
}
  
void readtemp() // call sensors.requestTemperatures() to issue
                // a global temp request to all devices on the bus.
{
  sensors.requestTemperatures(); // Send the command to get temperatures.
  myTemp0 = (sensors.getTempCByIndex(0));
  myTemp1 = (sensors.getTempCByIndex(1));
  

  TFTscreen.stroke(255,255,255);
  TFTscreen.text(sensorPrintout, 0, 20);
  // Set High or Low Temp.
  if (myTemp0 < myLowTemp0) 
  {
    myLowTemp0 = myTemp0;
  }
  if (myTemp0 > myHighTemp0)
  {
    myHighTemp0 = myTemp0;
  }
 if (myTemp1 < myLowTemp1) 
  {
    myLowTemp1 = myTemp1;
  }
  if (myTemp1 > myHighTemp1)
  {
    myHighTemp1 = myTemp1;
  }
}

void serialprint()
{
Serial.print("Current Fridge Temp 0: ");  
Serial.print(myTemp0);
Serial.print("C");
Serial.print(" Lowest Temp 0: ");
Serial.print(myLowTemp0);
Serial.print("C");
Serial.print(" Highest Temp 0: ");
Serial.print(myHighTemp0);
Serial.print("C");
Serial.print("\t\t");
Serial.print(" Current Pot Plant Temp 1: ");  
Serial.print(myTemp1);
Serial.print("C");
Serial.print(" Lowest Temp 1: ");
Serial.print(myLowTemp1);
Serial.print("C");
Serial.print(" Highest Temp 1: ");
Serial.print(myHighTemp1);
Serial.println("C");
delay(2500);
}

The screen is wired on a breadboard and is working fine as far as I can see according to the code above.

My main issue presently is getting the temperature value to display. I seem to be able to position where I want it to display, just not get the dynamic data from the sensors to show on the TFT.

I would like a hint as I have spent a long time looking through the net without getting that hint for the next step.

Regards,

Wayne

Isn't there an example included with TFT.h? I imagine all you need is to set the cursor and then use

TFTScreen.print(MyTemp);

but I can only guess at what those number are for.

Thanks,

Where in the posted code might I try that? I just tried at the end of the serial print section without success which will come down to my lack of knowledge here.

Regards,

Wayne

That sounds a reasonable place to put it but you would be better off starting with a separate "hello world" type programme exclusive for this purpose.

Thank you again for taking the time to reply.

I think I have addressed the hello world part in the section of code posted earlier as follows.

{
   // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();
  // clear the screen with a black background
  TFTscreen.background(0, 0, 255);
    // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(0,0,0);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("Temperatures :\n ",10,0);
  // set the font color to white
  TFTscreen.stroke(50,50,50);
  // set the font size
  TFTscreen.setTextSize(1);
  // write the text to the top left corner of the screen
  TFTscreen.text("Fridge Temperature :\n ",10,20);
  // write the text to the top left corner of the screen
  TFTscreen.text("Pot One Temperature :\n ",10,30);
  // ste the font size very large for the loop
  TFTscreen.setTextSize(1);

Here I have changed the locations, font and colours.

The part I am struggling with is how to get the sensor code, which I assume is already in a format to be exported due to the following code which should get the temperature in Celsius from what I can gleen from the web.

 sensors.requestTemperatures(); // Send the command to get temperatures.
  myTemp0 = (sensors.getTempCByIndex(0));
  myTemp1 = (sensors.getTempCByIndex(1));

By chance or simply good understanding would it be possible to elaborate some more on how I can overcome my problem?

Regards,

Wayne

Not from me, I'm afraid. I have just today started with my TFT screen that I have had for a couple of years and never got round to using. I thought you were using the same library but I now realise it is different. I have UTFT and I don't know anything about it anyway.

One thing though, while your code is incomplete, and I use different code for the DS18B20, I don't think there is anything wrong with the way you are getting it, and if your serial prints deliver what you expect, it should be kosher for anything, and it's just a matter of getting the format right for the screen.

If your library is by Henning Karlsen, note that he has updated with a universal library, hence UTFT. Included therein is a comprehensive manual that I just seen for the first time.

I now understand what you are doing with all those numbers a bit better, and there is more to this than I ever thought.

My 5110s are pixel addressable in x and line addressable in y, with separate cursor and print commands, but these screens are pixel addressed in both x and y, and it is all in one command. Further, there is a specific command for printing floats

The DS18B20 returns a float to two decimal places so print MyTemp1 = 56.78 one might speculate with

myTFT.printNumF(MyTemp1,2,20,50);

the 2 is the number of frac digits and the 20,50 is the coordinates of upper left corner of the start of printing.

I don't know if this is typical.

Hello,i'm modifed your code,this time is print to screen temperature.

Code:
#include <SPI.h>
#include <TFT.h>

// Libraries for DS1820 and Onewire
#include <OneWire.h>
#include <DallasTemperature.h>

//You can use any (4 or) 5 pins
//#define sclk 4
//#define mosi 5
#define cs 6 // pin definition for the Uno 6 Due 10
#define dc 7 // pin definition for the Uno 7 Due 9
#define rst 8 // pin definition for the Uno 8 Due 8
// you can also connect this to the Arduino reset

float myTemp0;
float myHighTemp0;
float myLowTemp0 = 50;
float myTemp1;
float myHighTemp1;
float myLowTemp1 = 50;

OneWire oneWire(3); // Setup onewire instance to communicate with devices.Uno 2 imposible other
DallasTemperature sensors(&oneWire);

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char sensorPrintout[8]; //[4]moded her for print out full numbers of dallas ds18b20

void setup()
{
// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();
// clear the screen with a black background
TFTscreen.background(0, 0, 0);
// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255,255,255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Temperatures :\n ",0,0);
// set the font color to white
TFTscreen.stroke(250,250,250);
// set the font size
TFTscreen.setTextSize(1);
// write the text to the top left corner of the screen
TFTscreen.text("Fridge Temperature :\n ",10,20);
// write the text to the top left corner of the screen
TFTscreen.text("Pot One Temperature :\n ",10,30);
// ste the font size very large for the loop
TFTscreen.setTextSize(1);

sensors.begin();
Serial.begin(9600);
}

void loop()
{
readtemp(); // Read the temperature.
serialprint(); // Write the results to the serial monitor.
//her my modification to get temperaturo on TFT display from one sensor
String myTemp0 = String(sensors.getTempCByIndex(0));
myTemp0.toCharArray(sensorPrintout, 8);
TFTscreen.stroke(250,250,250);
// set the font size
TFTscreen.setTextSize(1);
// write the text to line of text Fridge temperature
TFTscreen.text(sensorPrintout,130,20);
delay(10000);
// erase the text you just wrote
TFTscreen.stroke(0, 0, 0);
TFTscreen.text(sensorPrintout, 130, 20);
}

void readtemp() // call sensors.requestTemperatures() to issue
// a global temp request to all devices on the bus.
{
sensors.requestTemperatures(); // Send the command to get temperatures.
myTemp0 = (sensors.getTempCByIndex(0));
myTemp1 = (sensors.getTempCByIndex(1));

// Set High or Low Temp.
if (myTemp0 < myLowTemp0)
{
myLowTemp0 = myTemp0;
}
if (myTemp0 > myHighTemp0)
{
myHighTemp0 = myTemp0;
}
if (myTemp1 < myLowTemp1)
{
myLowTemp1 = myTemp1;
}
if (myTemp1 > myHighTemp1)
{
myHighTemp1 = myTemp1;
}
}

void serialprint()
{
Serial.print("Current Fridge Temp 0: ");
Serial.print(myTemp0);
Serial.print("C");
Serial.print(" Lowest Temp 0: ");
Serial.print(myLowTemp0);
Serial.print("C");
Serial.print(" Highest Temp 0: ");
Serial.print(myHighTemp0);
Serial.print("C");
Serial.print("\t\t");
Serial.print(" Current Pot Plant Temp 1: ");
Serial.print(myTemp1);
Serial.print("C");
Serial.print(" Lowest Temp 1: ");
Serial.print(myLowTemp1);
Serial.print("C");
Serial.print(" Highest Temp 1: ");
Serial.print(myHighTemp1);
Serial.println("C");
delay(2500);
}

Thanks! Was really helpful.

void loop()
{
 readtemp(); // Read the temperature.
 serialprint(); // Write the results to the serial monitor.
 //her my modification to get temperaturo on TFT display from one sensor
 String myTemp0 = String(sensors.getTempCByIndex(0));
 
 myTemp0.toCharArray(sensorPrintout, 8);
 TFTscreen.stroke(250,250,250);
 // set the font size
 TFTscreen.setTextSize(1);
 // write the text to line of text Fridge temperature
 TFTscreen.text(sensorPrintout,130,20);

 String myTemp1 = String(sensors.getTempCByIndex(1));
 myTemp1.toCharArray(sensorPrintout, 8);
 TFTscreen.stroke(250,250,250);
 // set the font size
 TFTscreen.setTextSize(1);
 // write the text to line of text Fridge temperature
 TFTscreen.text(sensorPrintout,130,30);
 delay(500);
 // erase the text you just wrote
 TFTscreen.stroke(0, 0, 0);
 TFTscreen.text(sensorPrintout, 130, 20);
 // erase the text you just wrote
 TFTscreen.stroke(0, 0, 0);
 TFTscreen.text(sensorPrintout, 130, 30);
}

This will show both temperatures on the display !

weird, I tried my own code, and this one and it wont display the temp. showing -128.00
if the tft is removed still -128.00
if the tft code is only removed it will display temp value on serial monitor.

the code I remove is from setup and loop (its all stock code) confusing?

full code:

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>

// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char sensorPrintout[8];

// Data wire is plugged into port 12 on the Arduino
#define ONE_WIRE_BUS 12

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup(void)
{

// Start up the library
sensors.begin();

// start serial port
Serial.begin(9600);

// Put this line at the beginning of every sketch that uses the GLCD:
//TFTscreen.begin();

// clear the screen with a black background
TFTscreen.background(0, 0, 0);

// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255, 255, 255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Temp Value :\n ", 0, 0);
// ste the font size very large for the loop
TFTscreen.setTextSize(3);

// clear the screen with a black background
TFTscreen.background(0, 0, 0);

// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255, 255, 255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Temp Value :\n ", 0, 0);
// ste the font size very large for the loop
TFTscreen.setTextSize(3);

}

void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));

// Read the value of the temp sensor
String sensorVal = String(sensors.getTempCByIndex(0));

// convert the reading to a char array
sensorVal.toCharArray(sensorPrintout, 8);

// set the font color
TFTscreen.stroke(255, 255, 255);
// print the sensor value
TFTscreen.text(sensorPrintout, 0, 20);
// wait for a moment
delay(250);
// erase the text you just wrote
TFTscreen.stroke(0, 0, 0);
TFTscreen.text(sensorPrintout, 0, 20);

}

thanks, any ideas?
tft is KMR-1.8 SPI 128*160 TFT (green-blue)
ebay DS18B20 on wire\sealed