Learn to build a basic Arduino weather station measuring temperature humidity and light level using DHT11 sensor and photoresistor with serial output.
#include <DHT.h> // Library for DHT sensor
#define DHTPIN 2 // Digital pin connected to the DHT11 sensor
#define DHTTYPE DHT11 // Type of DHT sensor
#define LDRPIN A0 // Analog pin connected to the photoresistor
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor
void setup() {
Serial.begin(9600); // Initialize serial communication
dht.begin(); // Start the DHT sensor
// No special configuration for LDR, it is an analog input
}
void loop() {
// Read temperature and humidity from DHT11
float umidita = dht.readHumidity();
float temperatura = dht.readTemperature(); // Celsius
// Check if the reading is valid
if (isnan(umidita) || isnan(temperatura)) {
Serial.println("Error reading from DHT11 sensor");
} else {
Serial.print("Humidity: ");
Serial.print(umidita);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperatura);
Serial.println(" *C");
}
// Read light level from photoresistor
int valoreLDR = analogRead(LDRPIN); // Value from 0 (dark) to 1023 (bright light)
Serial.print("Light level (LDR): ");
Serial.println(valoreLDR);
Serial.println("-----------------------------");
delay(2000); // Wait 2 seconds before next reading
}Arduino Uno (x1)
DHT11 sensor (temperature and humidity) (x1)
Photoresistor (LDR) (x1)
10kΩ resistor (for LDR) (x1)
Male-to-male jumper wires
Breadboard (optional)
CONNECTIONS: DHT11 VCC -> 5V Arduino
CONNECTIONS: DHT11 GND -> GND Arduino
CONNECTIONS: DHT11 DATA -> D2 Arduino
CONNECTIONS: LDR connected in series with 10kΩ resistor between 5V and GND; junction point of LDR-resistor connected to Arduino analog input A0