I want print data from a DHT11 (temperature and humidity) on a MAX7219 LED display but it only shows 0. Serial monitor, on the other hand, shows both humidity and temperature which means that my circuit works just fine and there's something wrong with code.Could anyone please point out what needs to be fixed to make it work? Thanks.
Here is the code
#include <WiFi.h>#include <NTPClient.h>#include <DHT.h> #include <WiFiUdp.h>#include <MD_Parola.h>#include <MD_MAX72xx.h>#include <SPI.h>#define HARDWARE_TYPE MD_MAX72XX::FC16_HW//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_H#define MAX_DEVICES 4#define CLK_PIN 5 #define DATA_PIN 7 #define CS_PIN 6 MD_Parola Display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);WiFiUDP ntpUDP;NTPClient timeClient(ntpUDP);const char* ssid = "6666";const char* password = "6666";String Time, hour, minute;String Formatted_date;long currentMillis = 0;long previousMillis = 0;int interval = 1000;#define DHTPIN A2 /*Signal pin for DHT11 Sensor*/#define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE);void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("Connecting."); } Serial.println(""); Serial.println("WiFi connected."); timeClient.begin(); timeClient.setTimeOffset(0); Display.begin(); Display.setIntensity(0); Display.displayClear(); dht.begin(); Serial.println("Status\tHumidity (%)\tTemperature (C)");}void loop() { obtainTime();}void obtainTime() { while(!timeClient.update()) { timeClient.forceUpdate(); } currentMillis = millis(); if (currentMillis - previousMillis > interval) {previousMillis = millis();Formatted_date = timeClient.getFormattedDate();Serial.println(Formatted_date);hour = Formatted_date.substring(11, 13);minute = Formatted_date.substring(14, 16);Time = hour + ":" + minute;Serial.println(Time);Display.setTextAlignment(PA_CENTER);Display.print(Time);delay(3000);// The DHT11 returns at most one measurement every 3s float h = dht.readHumidity(); //Read the moisture content in %. float t = dht.readTemperature(); //Read the temperature in degrees CelsiusSerial.println(t);Serial.println(h);Display.setTextAlignment(PA_CENTER);Display.print(t);delay(5000);Display.displayClear();} }- Variable
tis a float. Try temporarily changing this:Display.print(t);toDisplay.print(42);to see if the displayed temperature is something other zero. Also look at the Parola library to see what data type theprint()method can handle. Maybe also try a minimal Parola "Hello World" type sketch to see if a simple sketch behaves as expected. It would be nice if you formatted your code in the IDE before posting so it would be easier to read.6v6gt– 6v6gt2023-09-30 08:32:57 +00:00CommentedSep 30, 2023 at 8:32 - Thank you. Sure, I'll give it a shot. I tried this and it worked. char temp_result[6]; dtostrf(t,2,1,temp_result); Display.setTextAlignment(PA_CENTER); Display.print(temp_result); Not sure what "char temp_result[6]" does though. Any idea?Andrew Coz– Andrew Coz2023-09-30 12:55:43 +00:00CommentedSep 30, 2023 at 12:55
- Ok. So you have converted the float
tto a "c string" using dtostrf() , that is to a null terminated character array, then used that in the print() method for parola and it works. The conclusion is that parola can't handle a float datatype directly. I guess that you can now write your own answer to the original question to close the issue.6v6gt– 6v6gt2023-09-30 13:16:36 +00:00CommentedSep 30, 2023 at 13:16 - Done. Thanks for your help.Andrew Coz– Andrew Coz2023-10-02 05:23:56 +00:00CommentedOct 2, 2023 at 5:23
1 Answer1
It turned out that MD_Parola can't handle floats directly. I fixed the problem by converting the floats t and h into strings using dtostrf()
float h = dht.readHumidity(); float t = dht.readTemperature(); char temp_result[6];dtostrf(t,2,1,temp_result); char hum_result[6];dtostrf(h,2,0,hum_result);Serial.println(t);Serial.println(h);Display.setTextAlignment(PA_CENTER);//Display.print(temp_result);Display.print((String)temp_result+" "+"C");delay(5000);//Display.print(hum_result);Display.print((String)hum_result+" "+"%");delay(5000);Explore related questions
See similar questions with these tags.