Skip to main content

Simple Rain Sensor - Measuring and detecting moisture with regular rain sensor (example)

This page contains a simple example with function documentation on how to detect and measure moisture using the sensor.


Initialization

To use the sensor, first include the required library, create the sensor object (SimpleRainSensor), and initialize it in the setup() function. You can use the return value of begin() to check if everything is connected correctly.

#include "Simple-rain-sensor-easyC-SOLDERED.h"

SimpleRainSensor sensor;

void setup(){
sensor.begin();
}
//...

SimpleRainSensor sensor

Creates SimpleRainSensor object

Returns value: none

sensor.begin()

Initializes the sensor.

Returns value: Returns true if initialization is successful, false otherwise.


Measuring with both digital and analog output

In this library, there are two options for displaying the analog value: for a percentage value, call the getValue() function, and for the raw value, call the getRawValue() function.

#include "Simple-rain-sensor-easyC-SOLDERED.h"

SimpleRainSensor sensor;

#define CALIBRATION_VALUE_ON_DRY 1024
void setup(){
// Initialize the serial communication via UART
Serial.begin(115200);

// Initialize the sensor
// Start I2C communication on default address (0x30)
sensor.begin();
sensor.calibrate(CALIBRATION_VALUE_ON_DRY);

// Set threshold for LED on the breakout by raw value
// sensor.setRawThreshold(400); // or percentage:
sensor.setThreshold(90); // 25.1%
// If the sensor reads a value smaller than this, it means rain is detected

// If you want the LED to turn OFF when rain is detected, use:
// sensor.invertLED(true);
// Usually, it's ON while rain is detected
}

void loop(){
Serial.print("Rain reading from the sensor: ");
Serial.print(sensor.getValue()); // Prints percent value of rain sensor
Serial.print("%, and the raw value: ");
Serial.println(sensor.getRawValue()); // Prints raw value of rain sensor

Serial.print("Resistance of sensor: ");
Serial.print(sensor.getResistance()); // Prints resistance of rain sensor
Serial.println(" Ohms.");

Serial.print("The set threshold is: ");
Serial.print(sensor.getThreshold());
Serial.println("%");

// Detect if it's raining or not
// And print information accordingly
if (sensor.isRaining())
{
Serial.println("Rain is detected");
}
else
{
Serial.println("Rain is not detected");
}
Serial.println();

// Wait a bit before next reading
delay(1000);
}

sensor.getValue()

Returns the percent value of the rain sensor.

Returns value: Returns a float representation of the rain sensor value in percentage.

sensor.getRawValue()

Returns the raw ADC value.

Returns value: Returns an integer representation of the rain sensor value.

sensor.getResistance()

Returns the calculated resistance.

Returns value: Returns a float representation of the rain sensor resistance.

Sensor when rain is not present
Sensor when rain is not present
Serial Monitor output
Serial Monitor output
Sensor when rain is present
Sensor when rain is present
Serial Monitor output
Serial Monitor output

Full example

Try all of the functions mentioned above in the full example below:

Read_values_easyC.ino

Example for using the digital and analog read functions for the simple rain sensor with Qwiic.