The rain sensor detects water that falls on the metal circuit strips on its sensor boards. The sensor board acts as a variable resistor that will change from 100k ohms when wet to 2M ohms when dry. This change in resistence will cause a change in the outout voltage, which we can read using an anlog pin on the Arduino. There is also a digital pin which is pulled HIGH when the resistence falls below a threshold value set by the sensitivity dial (which is another variable resistor)
int RainPin = 0;
int RainDigitalPin = 2;
int RainValue;
boolean IsItRaining = false;
String strRaining;
void setup() {
Serial.begin(9600);
pinMode(RainDigitalPin,INPUT);
}
void loop() {
RainValue = analogRead(RainPin);
IsItRaining = !(digitalRead(RainDigitalPin));
if(IsItRaining){
strRaining = "YES";
}
else{
strRaining = "NO";
}
Serial.print("Raining?: ");
Serial.print(strRaining);
Serial.print("\t Moisture Level: ");
Serial.println(RainValue);
delay(200);
}
On an Arduino, analog input voltages between 0 and the operating voltage(5V for an Uno) are mapped into into integer values between 0 and 1023. So the values returned by an analogRead
have a resolution of 4.9 mV per unit.