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 the intensity of the present light using the sensor.


Initialization

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

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

#define ANALOG_PIN
#define DIGITAL_PIN

SimpleLightSensor sensor(ANALOG_PIN);

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

SimpleRainSensor sensor()

Creates SimplerainSensor object

Returns value: none

Function parameters:

TypeNameDescription
uint16_tpinAnalog pin number for data communication

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"

// Connecting diagram
// Breakout Arduino

// |-------------|
// D0------------9
// A0------------A0
// GND-----------GND
// VCC-----------5V

// Define your pins
#define ANALOG_PIN 14
#define DIGITAL_PIN 15

// Declare the sensor object and specify the pin to which the sensor is connected
SimpleRainSensor sensor(ANALOG_PIN);

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

// Initialize the sensor
sensor.begin();
sensor.setADCWidth(12);
// You can use a potentiometer on the breakout board to set the threshold to sense rain.
pinMode(DIGITAL_PIN, INPUT_PULLUP);
}

void loop()
{
Serial.print("Raw value of sensor: "); // Print information message
Serial.print(sensor.getValue()); // Prints percent value of rain sensor
Serial.print("% ");
Serial.println(sensor.getRawValue()); // Prints raw value of rain sensor

Serial.print("Resistance of sensor: "); // Print information message
Serial.print(sensor.getResistance()); // Prints resistance of rain sensor
Serial.println(" Ohms."); // Print information message
if (digitalRead(DIGITAL_PIN))
{
Serial.println("Rain is not detected");
}
else
{
Serial.println("Rain is detected");
}
Serial.println();

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

sensor.setADCWidth()

Sets the ADC resolution for used MCU.

Returns value: None

Function parameters:

TypeNameDescription
uint8_tresolutionADC resolution

sensor.getValue()

Returns the percent value of rain sensor.

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

sensor.getRawValue()

Returns the raw ADC value.

Returns value: Returns integer representation of rain value.

sensor.getResistance()

Returns the calculated resistance.

Returns value: Returns float representation of 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_native.ino

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