Projects / Green roof moisture sensors

Green roof moisture sensors

Intermediate3 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. ESP32SHT3124.6°C

    Calibrate the probes

    Note the reading in dry air and in a glass of water for each probe and put them in DRY and WET.

  2. ESP32SHT3124.6°C

    Wire the sensors

    Connect the three probes and the SHT31.

  3. ESP32SHT3124.6°C

    Open the page

    Upload, read the IP address on the Serial Monitor and open it in a browser.

  4. ESP32CAPACITIsoil 40%

    Place the probes

    Put them at three depths in the tray, with the blade fully in soil.

  5. ESP32SHT31IP6524.7°C

    Seal the box

    Mount the electronics in the IP65 box with cable glands.

  6. ESP32SHT3124.7°C

    Watch a rain day

    Compare the three readings before and after rain.

In short

How do you build a green roof moisture sensors?

Sensors in a green roof tray show how much water the soil holds and when it needs watering, on a small web page. You need ESP32 DevKit V1, Capacitive Soil Moisture Module × 3, SHT31 temperature and humidity sensor and Waterproof enclosure IP65 (junction box). It takes 6 steps and about 180 minutes.

Level
Intermediate
Time
About 180 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Moisture sensor 1 AOUT, VCC, GNDESP32 GPIO34, 3V3, GNDShallow probe (near the surface)
Moisture sensor 2 AOUT, VCC, GNDESP32 GPIO35, 3V3, GNDMiddle probe
Moisture sensor 3 AOUT, VCC, GNDESP32 GPIO32, 3V3, GNDDeep probe
SHT31 SDA, SCLESP32 GPIO21, GPIO22I2C
SHT31 VCC, GNDESP32 3V3, GNDPower

Code

Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module". Install "Adafruit SHT31 Library". Put your own Wi-Fi details in the placeholders. The page is only for your home network. Use capacitive probes, which do not corrode like the two-prong type, and seal the module's electronics and cable joints with conformal coating or silicone; only the probe's flat blade goes into the soil, never above the printed line. Put the ESP32 in the IP65 box on the roof edge.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <Adafruit_SHT31.h>

const char* WIFI_NAME = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
const byte PINS[3] = { 34, 35, 32 };
// Calibrate: raw reading in dry air and in a glass of water for your own sensors
const int DRY[3] = { 2800, 2800, 2800 }, WET[3] = { 1300, 1300, 1300 };
const int WATER_BELOW = 35;                   // percent at which the tray needs watering
Adafruit_SHT31 sht;
WebServer server(80);

int percent(byte i) {
  long s = 0;
  for (byte k = 0; k < 16; k++) { s += analogRead(PINS[i]); delay(2); }
  int pct = map(s / 16, DRY[i], WET[i], 0, 100);
  return constrain(pct, 0, 100);
}

void page() {
  int a = percent(0), b = percent(1), c = percent(2);
  String html = "<meta http-equiv='refresh' content='30'><body style='font-family:sans-serif'><h2>Green roof</h2>";
  html += "<p>Surface: " + String(a) + "%<br>Middle: " + String(b) + "%<br>Deep: " + String(c) + "%</p>";
  html += "<p>Air " + String(sht.readTemperature(), 1) + " C, " + String(sht.readHumidity(), 0) + "% humidity</p>";
  html += (a < WATER_BELOW && b < WATER_BELOW) ? "<h3>Water the tray</h3>" : "<h3>Moisture is fine</h3>";
  server.send(200, "text/html", html);
}

void setup() {
  Serial.begin(115200);
  analogSetPinAttenuation(34, ADC_11db); analogSetPinAttenuation(35, ADC_11db); analogSetPinAttenuation(32, ADC_11db);
  Wire.begin(21, 22);
  sht.begin(0x44);
  WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println(WiFi.localIP());
  server.on("/", page);
  server.begin();
}

void loop() { server.handleClient(); }

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

192.168.1.42

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Dry airAbout 0 %
Glass of waterAbout 100 %
Soaked trayHigh at the surface first, then deeper
Dry spellThe page says Water the tray

If it does not work

Always 0 or 100

Calibration values are off. Re-measure dry and wet readings.

Page will not load

Check the IP address and that your phone is on the same Wi-Fi.

SHT31 reads nan

Check I2C wiring and the 0x44 address.

Readings jump

Keep wires short and put the blade fully in the soil.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the green roof moisture sensors?

ESP32 DevKit V1, Capacitive Soil Moisture Module × 3, SHT31 temperature and humidity sensor and Waterproof enclosure IP65 (junction box).

How long does the green roof moisture sensors take to build?

About 180 minutes for a intermediate maker, in 6 steps.

How do I know the green roof moisture sensors works?

Dry air: About 0 %. Glass of water: About 100 %. Soaked tray: High at the surface first, then deeper.

Always 0 or 100: what should I check?

Calibration values are off. Re-measure dry and wet readings.

Page will not load: what should I check?

Check the IP address and that your phone is on the same Wi-Fi.

SHT31 reads nan: what should I check?

Check I2C wiring and the 0x44 address.

Readings jump: what should I check?

Keep wires short and put the blade fully in the soil.