#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <DHT.h>
// Define pins for each DHT sensor
#define DHTPIN1 0 // Digital pin connected to the first DHT sensor
#define DHTPIN2 5 // Digital pin connected to the second DHT sensor
#define DHTPIN3 17 // Digital pin connected to the third DHT sensor
// Define DHT sensor types
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Initialize DHT sensors
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);
// Define ILI9341 display pins
#define TFT_CS 15
#define TFT_RST 4
#define TFT_DC 2
// Initialize ILI9341 display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx multiple sensors with ILI9341 display test!"));
// Initialize DHT sensors
dht1.begin();
dht2.begin();
dht3.begin();
// Initialize the display
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(0, 0);
tft.println(F("DHTxx Sensor Readings:"));
}
void loop() {
delay(2000);
// Clear the previous readings
tft.fillRect(0, 30, 320, 210, ILI9341_BLACK);
// Read and display data from the first DHT sensor
readAndDisplayDHTData(dht1, 1, 30);
// Read and display data from the second DHT sensor
readAndDisplayDHTData(dht2, 2, 80);
// Read and display data from the third DHT sensor
readAndDisplayDHTData(dht3, 3, 130);
}
void readAndDisplayDHTData(DHT& dht, int sensorNumber, int yPos) {
float t = dht.readTemperature();
if (isnan(t)) {
Serial.print(F("Failed to read from DHT sensor "));
Serial.print(sensorNumber);
Serial.println(F("!"));
tft.setCursor(0, yPos);
tft.print(F("Sensor "));
tft.print(sensorNumber);
tft.println(F(": Error"));
return;
}
Serial.print(F("Sensor "));
Serial.print(sensorNumber);
Serial.print(F(" - Temperature: "));
Serial.print(t);
Serial.println(F("°C"));
tft.setCursor(0, yPos);
tft.print(F("Sensor "));
tft.print(sensorNumber);
tft.print(F(": "));
tft.print(t);
tft.println(F("C"));
}