Hello. I want data from a DHT11 (temperature and humidity) sensor to be printed 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 something wrong with code.
Could anyone please point out what needs to be fixed?
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 1s
float h = dht.readHumidity();
//Read the moisture content in %.
float t = dht.readTemperature();
//Read the temperature in degrees Celsius
Serial.println(t);
Serial.println(h);
Display.setTextAlignment(PA_CENTER);
Display.print(t);
delay(5000);
Display.displayClear();
}
}
Are you using the following MAX7219 driven LED Matrix Display Unit? If yes, please show the value of temperature and humidity that you have got on Serial Monitor.
The following sketch is a test skethc to show a float number (say: -23.57) at the center of the display unit. The sketch uses the SPI Port of UNO R3.
// Including the required Arduino libraries
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CS_PIN 10
// Create a new instance of the MD_Parola class with hardware SPI connection
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
float number = -23.57;
char buffer[20]; // Make sure the buffer is large enough
void setup()
{
myDisplay.begin();
myDisplay.setIntensity(0);
myDisplay.setTextAlignment(PA_CENTER);
myDisplay.displayClear();
delay(2000);
//------------------
// Convert the float to ASCII with 2 decimal places of precision
floatToASCII(number, buffer, 2);
myDisplay.print(buffer);
}
void loop() {}
void floatToASCII(float num, char *buffer, int precision)
{
// Handle the case where the number is negative
if (num < 0)
{
*buffer++ = '-';
num = -num;
}
// Extract the integer and fractional parts
int intPart = (int)num;
float fracPart = num - (float)intPart;
// Convert the integer part to ASCII
int length = snprintf(buffer, 10, "%d", intPart);
buffer += length;
// Add the decimal point
*buffer++ = '.';
// Convert the fractional part to ASCII with the specified precision
for (int i = 0; i < precision; i++)
{
fracPart *= 10;
int digit = (int)fracPart;
*buffer++ = '0' + digit;
fracPart -= digit;
}
// Null-terminate the string
*buffer = '\0';
}
arg1 = The float number: 3.14 arg2 = minimum field width: 4 (there are 4 symbols in the number 3 1 . 4) arg3 = Precison - number of digits after decimal point: 2 arg4 = Buffer (char-type array) into which the ASCII codes of 3 (0x30), . (0x2E), 1 (0x31), 4 (0x34) will be saved. The last element is a null-character and must be inserted manually. The buffer/array declaration is:
char myBuffer[5]; //size must be at least = symbols/digits in float number + 1