← All projects
Sensors Beginner

A Rain Sensor That Won't Rot in a Month: Powered From a GPIO Only While Measuring

Cheap FC-37 and YL-83 rain sensors corrode within weeks when they are powered all the time. We power the module from an ESP32 pin for 10 ms every few seconds, read AO and DO, and get a sensor that lasts for years.

Difficulty
Beginner
Time
30 min
Category
Sensors
Updated

About the project

A rain sensor is a board with two interleaved combs of tracks plus an LM393 comparator module. Water bridges the tracks, the resistance drops, and the output voltage changes. The problem is that a constant current through a wet board means electrolysis: the copper dissolves, and after a few weeks outdoors the tracks are simply gone. The fix is easy: power the sensor only for the moment of a measurement.

What you need

  • ESP32 DevKit
  • An FC-37 / YL-83 rain sensor (the plate plus a module with AO and DO)
  • Wires; for outdoors, a sealed box for the module

Wiring

Module ESP32
VCC GPIO26 — powered from the pin
GND GND
AO GPIO34 (ADC1)
DO GPIO27

The module draws 5–8 mA, which an ESP32 pin (up to 20 mA) supplies easily. For sensors that need more, use a transistor or a MOSFET.

Outputs

  • AO: a dry plate gives almost VCC, and the more water, the lower it goes. That tells drizzle from a downpour.
  • DO: LOW when wet (the level is past the threshold set by the trimmer).

Code

// A rain sensor that lasts for years: power it from a GPIO only while measuring
const int RAIN_POWER = 26;   // the module's VCC
const int RAIN_AO = 34;
const int RAIN_DO = 27;      // LOW = wet (threshold set by the trimmer)

void setup() {
  Serial.begin(115200);
  pinMode(RAIN_POWER, OUTPUT);
  digitalWrite(RAIN_POWER, LOW);
  pinMode(RAIN_DO, INPUT);
}

void loop() {
  digitalWrite(RAIN_POWER, HIGH);
  delay(10);                         // let the comparator settle
  int raw = analogRead(RAIN_AO);
  bool wet = digitalRead(RAIN_DO) == LOW;
  digitalWrite(RAIN_POWER, LOW);     // no current through the plate between readings
  int water = map(raw, 4095, 0, 0, 100);
  Serial.printf("Water %3d %%  %s\n", water, wet ? "RAIN" : "dry");
  delay(2000);
}

In the simulator change "Water on the plate". Try it the other way round too: connect the module's VCC to 3V3 and leave the plate wet — after half a minute the simulator reminds you about corrosion.

Good to know

  • The plate goes outside, the module into a box. The comparator fears moisture more than the tracks do. The cable between them can be 5 m long.
  • Tilt it 30–45°. Water pools on a flat plate, and the "rain" goes on for an hour after it has stopped.
  • Alternating current protects even better: some projects swap the polarity with two GPIOs. For most uses short power pulses are enough.
  • The same circuit suits resistive soil moisture probes — they have the same corrosion problem.

Parts for this project

Everything on the simulator schematic — into the cart with one click.

Usually also needed: