How to use a rain sensor with an Arduino
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
Wire the comparator board’s VCC to 5 V (or better, to a spare pin), GND to GND and AO to A0. Read it with analogRead(): a dry plate reads 1023, and the wetter it gets the lower it reads. The DO pin goes LOW once the plate is wetter than the board’s trimmer allows.
The circuit above is a rain sensor on A0 of an Uno, printing the reading five times a second. Press Run and it prints "A0 = 1023 (5000 mV)". Drag the slider to wet the plate: at 10 percent it prints "A0 = 908 (4437 mV)", at 50 percent "A0 = 547 (2673 mV)" and soaked "A0 = 341 (1666 mV)".
Below are the parts, the wiring, a sketch that sorts the reading into dry, light rain and heavy rain, and the one wiring trick that keeps a real plate from eating itself within weeks.
What you need
- An Arduino Uno
- A rain sensor: the plate and its comparator board (FC-37, YL-83 or similar)
- Jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| Plate | The comparator board’s two pins | Either way around |
| Board VCC | Pin 7 (or 5 V) | From a pin, so the plate is only powered while you read it |
| Board GND | GND | |
| Board AO | A0 | Lower means wetter |
| Board DO | Pin 2 | LOW when wet enough |
How the plate works
The plate is two combs of copper with their teeth between each other. Nothing joins them, so dry, it is an open circuit. A drop of water across two teeth is a resistor, and more drops are more resistors side by side, so the plate’s resistance falls as it gets wetter.
The small board puts the plate under a 10k pull-up. Dry, nothing pulls AO down and it reads the full supply. Wet, the plate pulls it lower. An LM393 comparator watches AO against the trimmer and pulls DO to ground, lighting its lamp, when AO drops below. With the trimmer in the middle, DO went low between 50 and 60 percent wet.
A rain gauge that sorts the reading
This sketch powers the board from pin 7, waits 10 ms, reads AO and DO, and switches the power off again. Then it prints the reading with a word for it. We stepped the plate from dry to soaked on a simulated Uno. Dry it printed "1023 dry, DO high". At 10 percent it printed "908 dry, DO high", at 30 percent "695 light rain, DO high", at 50 percent "547 heavy rain, DO high" and at 60 percent "491 heavy rain, DO low".
The bands, over 900 for dry and over 600 for light rain, are a starting point and not a rule. The reading depends on how salty the water is as much as how much there is, so a finger, tap water and rain all read differently. Leave your sensor out in a real shower and pick the numbers from what it prints.
const int RAIN_POWER = 7;
const int RAIN_AO = A0;
const int RAIN_DO = 2;
void setup() {
pinMode(RAIN_POWER, OUTPUT);
pinMode(RAIN_DO, INPUT);
Serial.begin(9600);
}
void loop() {
// Power the plate only while reading it.
digitalWrite(RAIN_POWER, HIGH);
delay(10);
int wet = analogRead(RAIN_AO);
bool raining = digitalRead(RAIN_DO) == LOW;
digitalWrite(RAIN_POWER, LOW);
Serial.print(wet);
if (wet > 900) {
Serial.print(" dry");
} else if (wet > 600) {
Serial.print(" light rain");
} else {
Serial.print(" heavy rain");
}
Serial.println(raining ? ", DO low" : ", DO high");
delay(1000);
}Why you should power it from a pin
A DC current through wet copper is electrolysis. Leave the plate powered all the time and the traces dissolve: a real one left out in the rain corrodes in weeks. Power it only for the few milliseconds it takes to read, a few times a minute, and the same plate lasts for years.
The board draws only a few milliamps, well inside what one pin can give, so a digital pin makes a fine power switch. In the run above, the board on pin 7 read the same 1023 dry and 908 at 10 percent as the board on 5 V in the circuit at the top. If you use DO as well, read it while the power is still on, as the sketch does.
Rain sensor or water level sensor?
They look alike, but they are used differently. The rain plate lies flat and answers "is it wet, and roughly how wet". A water level sensor is a long strip you stand in a cup, and it answers "how deep". The plant monitor project uses both, beside a soil moisture probe, and each one has its own tutorial linked below.
What the simulation leaves out
The reading follows a smooth curve with how wet the plate is. A real plate reads whatever the water’s salt and the drops’ positions give it, and it takes a while to dry off, so it is slow to say the rain has stopped. Corrosion is not modeled either: here the plate is fine however you power it, which is exactly why the advice above is worth reading before you build a real one.
Common mistakes
Powering the plate all the time. It corrodes. Power the board from a pin and switch it on only to read.
Reading it backwards. Lower means wetter on AO, and DO goes LOW when it is wet.
Reading DO after switching the power off. With no power the board drives nothing, so read DO and AO first, then switch off.
Copying someone else’s thresholds. Water is not all the same. Test your sensor in real rain.
Putting the comparator board outside too. Only the plate should get wet. Keep the small board and the Arduino under cover.
Questions
How do I connect a rain sensor to an Arduino?
The plate goes to the two pins on the small board. The board’s VCC goes to 5 V or a spare pin, GND to GND, AO to an analog pin such as A0 and DO to a digital pin if you want it.
What does a rain sensor read when it is dry?
About 1023 on a 5 V Uno, the full scale, because the dry plate does not pull AO down at all. In the simulator 10 percent wet read 908, half wet 547 and soaked 341.
Why does my rain sensor stop working after a few weeks?
The plate is corroding. A constant current through wet traces dissolves them. Power the sensor from a digital pin, switch it on only to take a reading, and it will last much longer.
What is the DO pin on a rain sensor for?
It is a yes or no from the board’s comparator: LOW when the plate is wetter than the trimmer is set for. Turn the trimmer to choose how wet counts as rain, and read it with digitalRead().
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.