This circuit is designed to monitor environmental parameters such as temperature, humidity, and soil moisture levels. It utilizes an ESP8266 NodeMCU microcontroller to read data from a DHT11 temperature and humidity sensor and a soil moisture sensor. The readings are displayed on a 16x2 I2C LCD screen. The ESP8266 NodeMCU is responsible for controlling the sensors, processing the data, and updating the display.
/*
* This Arduino Sketch reads temperature and humidity from a DHT11 sensor,
* soil moisture levels from a soil moisture sensor, and displays the
* readings on a 16x2 I2C LCD screen using an ESP8266 NodeMCU.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// LCD I2C address and dimensions
#define LCD_ADDR 0x27
#define LCD_COLS 16
#define LCD_ROWS 2
// DHT11 sensor settings
#define DHTPIN D3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Soil moisture sensor pin
#define SOIL_MOISTURE_PIN A0
// Initialize the LCD
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLS, LCD_ROWS);
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the DHT sensor
dht.begin();
// Initialize the LCD
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Initializing...");
}
void loop() {
// Read temperature and humidity from DHT11
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Read soil moisture level
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
float soilMoisturePercent = map(soilMoistureValue, 0, 1023, 0, 100);
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print readings to the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print(" %");
lcd.setCursor(8, 1);
lcd.print("Soil: ");
lcd.print(soilMoisturePercent);
lcd.print(" %");
// Wait a few seconds between measurements
delay(2000);
}
This code is responsible for initializing the sensors and the LCD screen, reading the sensor data, and displaying the information on the LCD. It includes error handling for failed sensor reads and uses a delay to space out the readings.