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