Arduino Weather Station Project - Hydreon Rain Sensor

Hooking Up the Hydreon RG-11 Rain Sensor (Part 4)


Observation: The Hydreon sensor is not as accurate as a tipping bucket rain gauge. It tends to read high in comparison. However this sensor will detect rain before a tipping bucket gauge.

Once we have confirmed that the DS18B20 and BME280 sensors are working okay we then hookup the Hydreon RG-11 rain sensor. This sensor requires 12V DC to operate. When it rains a relay is tripped when a amount of rain is detected. This is set via dip switches inside the sensor. The Arduino is configured to detect this relay opening and closing.

Hookup Arduino to BME280 and DS18B20

Hookup

The sensor is wired up as shown in the diagram below.

The rain sensor appears to the Arduino as a switch and is wired up the same as wiring up a switch to the Arduino. The RG-11 has either a NO (Normally Open) or NC (Normally Closed) output.

Hydreon RG-11 Hookup Circuit

We have connected to the RG-11 using the NO (Normally Open) outputs. When there is no rain detected the digital input on the micro controller (Arduino) will be pulled to 5V via the 10K resistor. If rain is detected then the RG-ll NO output will be closed and the digital input will go to ground.

We connect the sensor to digital pin 3. To detect the relay opening and closing when rain is detected we have to use a digital inout that supports interrupts.

Only digital pins 2 and 3 support interrupts. Pin 2 is used by the anemometer. When rain is detected it will generate a interrupt that we handle in the sketch. We use this increment the bucket count and the amount of rain that has fallen.

Code

As far as the Arduino is concerned the rain sensor appears as a switch. When rain is detected the input is pulled to ground before returning to 5V. So we need to detect this event happening. One way of doing this is in the sketch loop where we check the state of the digital input connected to the sensor. However a better way to do this is to use a hardware interrupt and handler to detect this pulse.

Only digital pins 2 and 3 support interrupts on the Arduino Uno series. We are using pin 3 (pin 2 is for the anemometer). In the sketch we used a method called attachInterrupt (line 41) to attach an interrupt handler routine to trigger when this interrupt is generated by a falling pulse. The handler function is called rgisr. At the bottom of the sketch we define the function called rgisr with the code to be executed by the interrupt.

Interrupt handler code should be kept as short as possible. In this case we do a little bit of debouncing of the signal before we update the tipCount. That is all. The rest is handled in the loop.

In the sketch loop function we update the rainfall count starting at line 50. When we are updating variables that are impacted by interrupt handler routines we need to disable interrupts. Here we check if the tip count has increased. If so we then recalculate the rainfall total. Once done we reactive the interrupt handlers.

In this sketch we have activated the data transmit led which we will use when we are transmitting data to the web server. We have mounted the power and transmit led so they are visible outside the controller box. It's useful for debugging and it annoys the rats at night time. The data led is defined by TX_Pin and is connected to digital pin 8. In this sketch we just toggle the led every 2 seconds when we output data to the console.

All new code has been highlighted.

Library

There is no library required for the rain sensor

Software Sketch

Basic Weather Station Rain Sensor Sketch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "cactus_io_DS18B20.h"
#include "cactus_io_BME280_I2C.h"

#define Bucket_Size 0.01 // bucket size to trigger tip count
#define RG11_Pin 3 // digital pin RG11 connected to
#define TX_Pin 8 // used to indicate web data tx
#define DS18B20_Pin 9 // DS18B20 Signal pin on digital 9

volatile unsigned long tipCount; // bucket tip counter used in interrupt routine
volatile unsigned long contactTime; // Timer to manage any contact bounce in interrupt routine
volatile float totalRainfall; // total amount of rainfall detected
bool txState; // current led state for tx rx led

// Create DS18B20, BME280 object
DS18B20 ds(DS18B20_Pin); // on digital pin 9
BME280_I2C bme; // I2C using address 0x77

void setup() {

txState = HIGH;
tipCount = 0;
totalRainfall = 0;

ds.readSensor();

Serial.begin(9600);
Serial.println("cactus.io | Weather Station DS18B20, BME280, RG11 Sensor Test");
Serial.println("DS Temp\t\tBME Temp\tHumdity\t\tPressure\tRainfall");

if (!bme.begin()) {
Serial.println("Could not find BME280 sensor, check wiring!");
while (1);
}

pinMode(TX_Pin, OUTPUT);
pinMode(RG11_Pin, INPUT);
attachInterrupt(digitalPinToInterrupt(RG11_Pin), isr_rg, FALLING);
sei();// Enable Interrupts
}

void loop() {

txState = !txState; // toggle the led state

ds.readSensor();
bme.readSensor();

digitalWrite(TX_Pin,txState);

Serial.print(ds.getTemperature_C()); Serial.print(" *C\t");
Serial.print(bme.getTemperature_C()); Serial.print(" *C\t");
Serial.print(bme.getHumidity()); Serial.print(" %\t\t");
Serial.print(bme.getPressure_MB()); Serial.print(" mb\t");
Serial.print(totalRainfall); Serial.println(" mm");

// Add a 2 second delay.
delay(2000); //just here to slow down the output.
digitalWrite(TX_Pin,txState);
}

// Interrupt handler routine that is triggered when the rg-11 detects rain
void isr_rg() {

if((millis() - contactTime) > 15 ) { // debounce of sensor signal
tipCount++;
totalRainfall = tipCount * Bucket_Size;
contactTime = millis();
}
}
// end of rg-11 rain detection interrupt handler

Sketch Console Output

The sketch displays data from all of the sensors we have connected so far. To simulate rain we tipped some water from a cup on the sensor.

Connect Arduino to DS18B20, BME280, RG11 Sensor