Can I measure how long it rains?

Hi,
I am a beginner for programming, I want to ask if it is possible for me to measure how long it rains with the rain sensor (FC-37)? if there are compatible sensors for this question or other suitable algorithms are welcome.

Moved your topic to a mores suitable section.

You may want to READ THIS for future reference.
It will help you get the best out of the forum.

Bob.

Assuming you're happy that if the sensor's wet it's raining, and dry then it's not, then that's quite an easy task. (I wonder how long they take to dry, once the rain has stopped, to tell you it's still raining when it's not?)

It's pretty much the technique explained in the state change detect tutorial.

If it was dry and is now wet, start the clock. If it was wet and it's now dry, stop the clock, subtract.

(If it was dry and is now dry, do nothing. It it was wet and is now wet, do nothing.)

The code below is tested with a potentiometer, since I don't have your actual sensor.

It starts the clock at a dry to wet transition, and stops at wet to dry. It displays how long that shower was, and also keeps a running total since power up.

YMMV.

Don't forget to set the right pins and choose a suitable wet vs dry threshold in these lines:

int rainPin = A0;
int dryLED = 6;
int wetLED = 7;
// you can adjust the threshold value
int thresholdValue = 500;
//  https://forum.arduino.cc/index.php?topic=649167
//  how long has rain sensor been wet?
//  this code gives length of last rain spell, and runnig total since power on
//  21 nov 2019

// based on https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/rain_sensor_arduino.ino
//  (but it's just a simple analog read)
// incorporates state change detect ala https://www.arduino.cc/en/Tutorial/StateChangeDetection

/* sample output
Rain started at 4651
  Rain stopped at 10652
    It rained for 6001ms
    Total rain since power on 6001ms
Rain started at 15678
  Rain stopped at 25681
    It rained for 10003ms
    Total rain since power on 16004ms
Rain started at 29709
  Rain stopped at 35211
    It rained for 5502ms
    Total rain since power on 21506ms
*/

int rainPin = A0;
int dryLED = 6;
int wetLED = 7;
// you can adjust the threshold value
int thresholdValue = 500; //wet if under
int sensorValue;
bool itIsWet; //gets set correctly in setup()
bool itWasWet = false; //so that if we start wet, it immediately starts timing
unsigned long rainStartedAt;
unsigned long rainStoppedAt;
unsigned long itRainedFor; //this time
unsigned long itRainedForTotal;  //runnung total since power on

void setup()
{
  Serial.begin(9600);
  Serial.println(".... how long has it been raining? ....");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  // initialise the rain leds and sensor input
  pinMode(rainPin, INPUT); //redundant for an analog input
  pinMode(dryLED, OUTPUT);
  pinMode(wetLED, OUTPUT);
  digitalWrite(dryLED, LOW);
  digitalWrite(wetLED, LOW);
  //initialise the status
  sensorValue = analogRead(rainPin);
  
  Serial.println("");
  if (sensorValue < thresholdValue)
  {
    itIsWet = true;
    Serial.print("It's already raining, so assuming ");
  }
  else
  {
    itIsWet = false;
  }
} //setup

void loop()
{
  readTheSensor();
  timeTheRain();
  delay(500);
} //loop

void readTheSensor()
{
  sensorValue = analogRead(rainPin);
  if (sensorValue < thresholdValue)
  {
    itIsWet = true;
    digitalWrite(wetLED, HIGH);
    digitalWrite(dryLED, LOW);
  }
  else
  {
    itIsWet = false;
    digitalWrite(wetLED, LOW);
    digitalWrite(dryLED, HIGH);
  }
}//readTheSensor

void timeTheRain()
{
  if (itIsWet && !itWasWet) //is wet now, was dry before
  {
    rainStartedAt = millis();
    Serial.print("Rain started at ");
    Serial.println(rainStartedAt);
  }

  if (!itIsWet && itWasWet) //is dry now, was wet before
  {
    rainStoppedAt = millis();
    Serial.print("  Rain stopped at ");
    Serial.println(rainStoppedAt);
    itRainedFor = rainStoppedAt - rainStartedAt;
    Serial.print("    It rained for ");
    Serial.print(itRainedFor); Serial.println("ms");
    itRainedForTotal = itRainedForTotal + itRainedFor;
    Serial.print("    Total rain since power on ");
    Serial.print(itRainedForTotal); Serial.println("ms");
  }
  itWasWet = itIsWet;
}//timeTheRain