Skip to content

Instantly share code, notes, and snippets.

@topshed
Last active May 5, 2019 18:03
Show Gist options
  • Save topshed/098a06583ca7792064c2ee5798fa0216 to your computer and use it in GitHub Desktop.
Save topshed/098a06583ca7792064c2ee5798fa0216 to your computer and use it in GitHub Desktop.

Build a rain alarm

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)

Imgur

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment