Hi, I have an Arduino Nano connected to a DHT11 temperature sensor and a 1.8" TFT display. I'm trying to display the temperature detected in Fahrenheit and have it be displayed onto the TFT's screen. But when I upload the sketch it just keeps displaying the number 824 were the temperature is suppose to be (btw I've copied some pieces from other sketches).
#include <idDHT11.h>
int idDHT11pin = 2; //Digital pin for comunications
int idDHT11intNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above)
//declaration
void dht11_wrapper(); // must be declared before the lib initialization
// Lib instantiate
idDHT11 DHT11(idDHT11pin,idDHT11intNumber,dht11_wrapper);
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8
// pin definition for the Leonardo
// #define cs 7
// #define dc 0
// #define rst 1
TFT TFTscreen = TFT(cs, dc, rst);
char sensorPrintout[4];
void setup() {
// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();
TFTscreen.background(0, 0, 0);
TFTscreen.stroke(255,255,255);
TFTscreen.setTextSize(1);
TFTscreen.text("Temperature Now :\n ",0,0);
TFTscreen.setTextSize(3);
Serial.begin(9600);
Serial.println("idDHT11 Example program");
Serial.print("LIB version: ");
Serial.println(IDDHT11LIB_VERSION);
Serial.println("---------------");
}
void dht11_wrapper() {
DHT11.isrCallback();
}
void loop()
{
Serial.print("\nRetrieving information from sensor: ");
Serial.print("Read sensor: ");
//delay(100);
DHT11.acquire();
while (DHT11.acquiring())
;
int result = DHT11.getStatus();
switch (result)
{
case IDDHTLIB_OK:
Serial.println("OK");
break;
case IDDHTLIB_ERROR_CHECKSUM:
Serial.println("Error\n\r\tChecksum error");
break;
case IDDHTLIB_ERROR_ISR_TIMEOUT:
Serial.println("Error\n\r\tISR Time out error");
break;
case IDDHTLIB_ERROR_RESPONSE_TIMEOUT:
Serial.println("Error\n\r\tResponse time out error");
break;
case IDDHTLIB_ERROR_DATA_TIMEOUT:
Serial.println("Error\n\r\tData time out error");
break;
case IDDHTLIB_ERROR_ACQUIRING:
Serial.println("Error\n\r\tAcquiring");
break;
case IDDHTLIB_ERROR_DELTA:
Serial.println("Error\n\r\tDelta time to small");
break;
case IDDHTLIB_ERROR_NOTSTARTED:
Serial.println("Error\n\r\tNot started");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Temperature (oF): ");
Serial.println(DHT11.getFahrenheit(), 2);
delay(20);
// Read the value of the sensor on A0
String yo = String('DHT11.getFahrenheit(), 2');
yo.toCharArray(sensorPrintout, 4);
TFTscreen.stroke(255,255,255);
TFTscreen.text(sensorPrintout, 0, 20);
delay(2500);
TFTscreen.stroke(0,0,0);
TFTscreen.text(0, 0, 20);
}
Hmm, I think you were referring to the quotations, so I've changed them to double quotations but now the TFT just displays the letters DHT where the temperature is supposed to be.
KenF I honestly wouldn't know since I am a noob at this, but I will try to modify that 2.
Update: I've tried removing the 2 from the serial.print line and it come out with an error. I also tried deleting the 2 in the string line and I'm still just displaying the letters DHT where the temperature is supposed to be.
I've been working with the DHT11 for my current project. I've since changed over to a different chip for temperature (as the dht11 only gives temperature to the nearest whole degree centigrade. I'll see if I can find the code I WAS using.
Honestly I would be happy If it would even display the nearest whole degree centigrade. I don't think 824 is correct, and when I look at the serial monitor the sensor is displaying reasonable values.
Update: I've tried removing the 2 from the serial.print line and it come out with an error. I also tried deleting the 2 in the string line and I'm still just displaying the letters DHT where the temperature is supposed to be.
dht11 DHT11;
//in the following line replace 53 with the pin you're using
int chk=DHT11.read(53);
if (chk==DHTLIB_OK)
{//get humidity
int hum=DHT11.humidity;
//get temperature in centigrade
int temp=DHT11.temperature;
//if you want it in farenheight..
temp=32+temp*9/5;
Serial.println(temp,DEC);
}
Hmm, I applied the line of code you provided and the serial monitor continues to put out reasonable values but the display continues to show the number 824.
Well my serial monitor display this:
Retrieving information from sensor: Read sensor: OK
Temperature (oF): 77.00
Here is my current code ( I think this is what you were asking for)
#include <idDHT11.h>
int idDHT11pin = 2; //Digital pin for comunications
int idDHT11intNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above)
//declaration
void dht11_wrapper(); // must be declared before the lib initialization
// Lib instantiate
idDHT11 DHT11(idDHT11pin,idDHT11intNumber,dht11_wrapper);
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8
// pin definition for the Leonardo
// #define cs 7
// #define dc 0
// #define rst 1
// 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, 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(1);
// write the text to the top left corner of the screen
TFTscreen.text("Temperature Now :\n ",0,0);
// ste the font size very large for the loop
TFTscreen.setTextSize(3);
Serial.begin(9600);
Serial.println("idDHT11 Example program");
Serial.print("LIB version: ");
Serial.println(IDDHT11LIB_VERSION);
Serial.println("---------------");
}
void dht11_wrapper() {
DHT11.isrCallback();
}
void loop()
{
Serial.print("\nRetrieving information from sensor: ");
Serial.print("Read sensor: ");
//delay(100);
DHT11.acquire();
while (DHT11.acquiring())
;
int result = DHT11.getStatus();
switch (result)
{
case IDDHTLIB_OK:
Serial.println("OK");
break;
case IDDHTLIB_ERROR_CHECKSUM:
Serial.println("Error\n\r\tChecksum error");
break;
case IDDHTLIB_ERROR_ISR_TIMEOUT:
Serial.println("Error\n\r\tISR Time out error");
break;
case IDDHTLIB_ERROR_RESPONSE_TIMEOUT:
Serial.println("Error\n\r\tResponse time out error");
break;
case IDDHTLIB_ERROR_DATA_TIMEOUT:
Serial.println("Error\n\r\tData time out error");
break;
case IDDHTLIB_ERROR_ACQUIRING:
Serial.println("Error\n\r\tAcquiring");
break;
case IDDHTLIB_ERROR_DELTA:
Serial.println("Error\n\r\tDelta time to small");
break;
case IDDHTLIB_ERROR_NOTSTARTED:
Serial.println("Error\n\r\tNot started");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Temperature (oF): ");
Serial.println(DHT11.getFahrenheit());
delay(20);
// Read the value of the sensor on A0
String yo = String('DHT11.getFahrenheit(), 2');
// convert the reading to a char array
yo.toCharArray(sensorPrintout, 4);
// set the font color
TFTscreen.stroke(255,255,255);
// print the sensor value
TFTscreen.text(sensorPrintout, 0, 20);
// wait for a moment
delay(2500);
// erase the text you just wrote
TFTscreen.stroke(0,0,0);
TFTscreen.text(0, 0, 20);
}
Robtillart has published a much easier library to use.. Try it instead...
I might point out that the DHT22 is a much better device to use.
Although a bit more expensivehttp://www.ebay.com/itm/DHT22-AM2302-Digital-Temperature-and-Humidity-Sensor-/120770206870