Home and IoTA Room Thermometer on an LCD
A DHT11 sensor measures the temperature and humidity, and a 16×2 display shows both, updating every two seconds.
Home and IoTA sensor checks the soil moisture. When the soil dries out, a relay switches on a pump and the plant waters itself.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
Push the soil moisture sensor into the soil. Connect its VCC to 5V, GND to the board's GND and its analogue output (AO) to A0.
Run the pump through the relay with its own power supply. Never connect the pump straight to a board pin. The relay's IN goes to D7.
Upload the code and open the Serial Monitor (9600). Read the values for wet and dry soil (dry soil gives the higher number) and put a number between them in DRY in the code. If the relay works the other way round (the pump is always on), swap RELAY_ON and RELAY_OFF. On an ESP32 connect the sensor to 3V3 and GPIO 34 and the relay's IN to GPIO 26, and set SOIL_PIN = 34 and RELAY_PIN = 26 in the code. An ESP32 reads from 0 to 4095, so measure DRY again. If the relay won't switch on a 3.3 V signal, use a logic level shifter.
// Automatic plant watering: a soil moisture sensor on A0, a relay module's IN on D7.
// When the soil is dry the pump runs for a few seconds, then the code waits
// 10 minutes for the water to soak in before it measures again.
const int SOIL_PIN = A0; // the sensor's analogue output (AO)
const int RELAY_PIN = 7; // the relay module's IN
const int RELAY_ON = LOW; // most relay modules switch on with LOW;
const int RELAY_OFF = HIGH; // if yours is the other way round, swap these two
int DRY = 450; // above this the soil is dry: set it from your own readings
const unsigned long PUMP_MS = 3000; // how long the pump runs each time
const unsigned long SOAK_MS = 10UL * 60 * 1000; // 10 minutes for the water to soak in
const unsigned long CHECK_MS = 2000; // how often to measure otherwise
// The average of ten readings, so one noisy value doesn't start the pump
int readSoil() {
long sum = 0;
for (int i = 0; i < 10; i++) {
sum += analogRead(SOIL_PIN);
delay(10);
}
return sum / 10;
}
void setup() {
digitalWrite(RELAY_PIN, RELAY_OFF); // set the pin before it becomes an output, so the pump doesn't kick at start
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Soil reading (higher = drier):");
}
void loop() {
int soil = readSoil();
Serial.println(soil);
if (soil > DRY) {
Serial.println("Dry: watering");
digitalWrite(RELAY_PIN, RELAY_ON);
delay(PUMP_MS);
digitalWrite(RELAY_PIN, RELAY_OFF);
Serial.println("Waiting 10 minutes for the water to soak in");
delay(SOAK_MS);
} else {
delay(CHECK_MS);
}
}Test the pump in a glass of water first, then on the plant. After watering the code waits 10 minutes for the water to soak in. If the plant gets too little or too much water, change PUMP_MS.
Home and IoTA DHT11 sensor measures the temperature and humidity, and a 16×2 display shows both, updating every two seconds.
Home and IoTAn ESP32 measures the room's temperature and humidity and serves a small web page you open in your phone's browser on the home Wi-Fi.
Home and IoTA PIR sensor notices a warm body moving. As soon as someone walks into the room, a siren sounds and a red light flashes.