{"id":1924,"date":"2020-05-10T10:44:22","date_gmt":"2020-05-10T17:44:22","guid":{"rendered":"https:\/\/partofthething.com\/thoughts\/?p=1924"},"modified":"2024-05-25T18:30:07","modified_gmt":"2024-05-26T01:30:07","slug":"connecting-a-hydreon-infrared-rain-sensor-to-a-esp8266-or-arduino-or-raspberry-pi","status":"publish","type":"post","link":"https:\/\/partofthething.com\/thoughts\/connecting-a-hydreon-infrared-rain-sensor-to-a-esp8266-or-arduino-or-raspberry-pi\/","title":{"rendered":"Connecting a Hydreon Infrared Rain Sensor to a ESP8266 (or Arduino or Raspberry Pi)"},"content":{"rendered":"\n<p>I&#8217;ve been working on a <a href=\"https:\/\/partofthething.com\/thoughts\/weather-and-air-quality-monitoring-station-with-esp8266-and-home-assistant\/\">home-brew weather station<\/a> and was looking into rain sensors when I discovered that you can get infrared (IR) rain detectors. <a href=\"https:\/\/rainsensors.com\/\">A company in Minnesota sells one called the Hydreon RG-11<\/a>. They shoot pulses of IR light around a plastic dome and monitor them on the other end. When rain hits the dome, the refraction changes and the pulses received are perturbed. This is nice because it&#8217;s very simple and has no moving parts. I figured I&#8217;d be able to find a way to read it into my weather station. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_082423.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_082423-1024x576.jpg\" alt=\"\" class=\"wp-image-1925\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_082423-1024x576.jpg 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_082423-300x169.jpg 300w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_082423-768x432.jpg 768w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_082423-1536x864.jpg 1536w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_082423.jpg 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The RG-11 rain sensor with a NodeMCU for scale. (It&#8217;s actually bigger than I thought)<\/figcaption><\/figure>\n\n\n\n<!--more-->\n\n\n\n<p>There are a <a href=\"https:\/\/rainsensors.com\/rain-gauge-modes\/\">variety of modes<\/a> you can use. I wanted early indication of when it started raining (so I could get an alert from Home Assistant to close the windows). But I also wanted some measure of the actual rain intensity. Thus, I decided that a small <a href=\"https:\/\/en.wikipedia.org\/wiki\/Rain_gauge#Tipping_bucket_rain_gauge\">&#8220;tipping bucket&#8221;<\/a> mode would work best for me. In this mode, the sensor just closes a relay whenever it thinks a certain amount of rain has fallen. Here&#8217;s a quick demo of it clicking:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Quick demo of Hydreon RG11 infrared rain sensor\" width=\"660\" height=\"371\" src=\"https:\/\/www.youtube.com\/embed\/BxVbIZfC324?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><figcaption class=\"wp-element-caption\">Short demo pouring water on the sensor and seeing it click<\/figcaption><\/figure>\n\n\n\n<p>You can set the sensor to click at various sizes. Light, moderate, heavy, and violent rain rates <a href=\"https:\/\/en.wikipedia.org\/wiki\/Rain#Intensity\">are defined as follows:<\/a><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Rain intensity<\/th><th>mm\/hr<\/th><th>in\/hr<\/th><th>0.01&#8243; clicks\/hr<\/th><th>0.001&#8243; clicks\/hr<\/th><th>0.0001&#8243; clicks\/hr<\/th><\/tr><\/thead><tbody><tr><td>Light<\/td><td>&lt;2.5<\/td><td>0.098<\/td><td>9.8<\/td><td>98<\/td><td>980<\/td><\/tr><tr><td>Moderate<\/td><td>&lt;7.6<\/td><td>0.3<\/td><td>30<\/td><td>300<\/td><td>3000<\/td><\/tr><tr><td>Heavy<\/td><td>&lt;50<\/td><td>2.0<\/td><td>200<\/td><td>2000<\/td><td>20000<\/td><\/tr><tr><td>Violent<\/td><td>&gt;50<\/td><td>&gt;2.0<\/td><td>&gt;200<\/td><td>&gt;2000<\/td><td>&gt;20000<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">How many clicks you can expect in different rain conditions with the various tipping bucket mode settings.<\/figcaption><\/figure>\n\n\n\n<p>In testing, I got a zillion clicks out of my little squeeze bottle on the most sensitive setting, so I put it on 0.001&#8243;\/click. But then I put it out in the misting rain of Seattle and it only clicked like once. So I did the math above and realized that in Seattle, the 0.0001&#8243;\/click setting is probably the most useful one.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"453\" height=\"350\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-14.png\" alt=\"\" class=\"wp-image-1947\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-14.png 453w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-14-300x232.png 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><figcaption class=\"wp-element-caption\">Top rainy days in Seattle, from <a href=\"https:\/\/www.seattleweatherblog.com\/rain-stats\/\">Seattleweatherblog<\/a>, data from Sea-Tac. <\/figcaption><\/figure>\n<\/div>\n\n\n<p>So, averaged over the rainiest day in Seattle history, I would see one click every 1.7 seconds. Surely there were periods of extra intensity, and the sensor will peak out clicking like crazy in those times. We will see if that becomes a problem.<\/p>\n\n\n\n<p>Here are some basic counts that have been sent to my Home Assistant controller. I set it up to give a nice &#8220;drop&#8221; sound when it goes above zero to help me remember to close the window. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"464\" height=\"378\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/Screenshot-from-2020-05-13-08-48-31.png\" alt=\"\" class=\"wp-image-1959\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/Screenshot-from-2020-05-13-08-48-31.png 464w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/Screenshot-from-2020-05-13-08-48-31-300x244.png 300w\" sizes=\"auto, (max-width: 464px) 100vw, 464px\" \/><figcaption class=\"wp-element-caption\">Readings from the sensor when there very low drizzle going on<\/figcaption><\/figure>\n<\/div>\n\n\n<p>I temporarily mounted it with non-waterproof, non-UV resistant cat5 cable and a indoor mounting bracket. Once COVID lets up (or I get a mail-order), I&#8217;ll replace these with waterproof stuff and stainless hardware. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_3385.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"682\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_3385-1024x682.jpg\" alt=\" RG-11 rain sensor mounted temporarily on outside of house\" class=\"wp-image-1952\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_3385-1024x682.jpg 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_3385-300x200.jpg 300w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_3385-768x512.jpg 768w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_3385-1536x1024.jpg 1536w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_3385.jpg 2000w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Temporary non-waterproof mounting of my RG-11 rain sensor<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Making a 12V to 3.3V voltage divider<\/h2>\n\n\n\n<p>The sensor needs 12V to operate, but ESP8266 and Raspberry Pis can only handle up to 3.3V on their GPIO digital input pins. I could run a separate 3.3V data line to the relay for signaling, but I decided to be more economical and just use the power line as the signal line as well. Besides, 12V signals will travel over longer distances on cables than 3.3V ones. Of course I have to step 12V down to 3.3V before going into the GPIO. Since the GPIO signal is very low current, this is appropriate. <\/p>\n\n\n\n<p class=\"has-yellow-background-color has-background\"><strong>UPDATE<\/strong>: I was convinced to just run 3.3V in this case rather than a voltage divider because if the ground fails or something else goes wrong, the ESP would see the full 12V and be fried, so it&#8217;s just not that safe. But the voltage divider info is still here&#8230;<\/p>\n\n\n\n<p>We know the <a href=\"http:\/\/henrysbench.capnfatz.com\/henrys-bench\/arduino-projects-tips-and-more\/esp8266ex-gpio-high-and-low-input-thresholds\/\">threshold for a HIGH signal in ESP8266 is about 2.5 V<\/a> so we need a <a href=\"http:\/\/www.ohmslawcalculator.com\/voltage-divider-calculator\">voltage divider<\/a> that can go from 12 to somewhere between 2.5 V and 3.3 V. I dumped out my collection of resistors and made a spreadsheet with the options:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_081815.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_081815-1024x576.jpg\" alt=\"\" class=\"wp-image-1926\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_081815-1024x576.jpg 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_081815-300x169.jpg 300w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_081815-768x432.jpg 768w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_081815-1536x864.jpg 1536w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/IMG_20200510_081815.jpg 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Which voltage divider to use to step 12V down to 3.3V?<br><\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-11.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"384\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-11-1024x384.png\" alt=\"\" class=\"wp-image-1927\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-11-1024x384.png 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-11-300x113.png 300w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-11-768x288.png 768w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/image-11.png 1164w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">My spreadsheet matrix of resistors showing the voltage I would get in a voltage divider starting from 12V. I had to dig out my dad&#8217;s old resistors from Radio Shack in the 1980s before I found a 3.3 k\u03a9 10k\u03a9 combo that got it down into the 3.3V logic range. <\/figcaption><\/figure>\n\n\n\n<p>Here&#8217;s a quick video showing the output pulses from the voltage divider on my scope:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Stepping a 12V signal from a rain sensor down to 3.3V to be safe for GPIO\" width=\"660\" height=\"371\" src=\"https:\/\/www.youtube.com\/embed\/jXzt7maOl4g?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>I will be running outdoor CAT6 cable to it once it&#8217;s mounted. I will only need three of the cables: 12V in, ground, and 12V pulses out for signaling the tipping bucket. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The code<\/h3>\n\n\n\n<p>And that&#8217;s about it. To wire it up to the ESP8266 or Raspberry Pi you just use the same interrupt code that you&#8217;d use for, <a href=\"https:\/\/www.instructables.com\/id\/PIR-Motion-Detector-With-Arduino-Operated-at-Lowes\/\">like, a PIR motion sensor<\/a> or anything. In the code, you just count the number of pulses and divide by a running clock to get a read-out of precipitation per hour, or whatever. <\/p>\n\n\n\n<p>Here are some code snippets to read the pulses in a ESP8266. I also have a software timer that sends reports over MQTT every 30 seconds and resets the count. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define RAIN_PULSE D3\n\nlong rainPulseCount = 0L;\nvolatile bool rainFlag = false;\n\nvoid setup()\n{\n    Serial.begin(115200);\n    \/\/ clipping relay grounds the pin, causing it to go down\n    pinMode(RAIN_PULSE, INPUT_PULLUP); \n    attachInterrupt(digitalPinToInterrupt(RAIN_PULSE), rain_ISR, FALLING);\n}\n\nICACHE_RAM_ATTR void rain_ISR() {\n  rainFlag = true;\n}\n\nvoid loop() \n{\n    if (rainFlag) {\n        \/\/ Just bump the rain counter. Each bump is 0.01\" of rain.\n        \/\/ Will reset after reporting\n        Serial.println(\"RAIN!\");\n        rainPulseCount++;\n        rainFlag = false;\n    }\n}<\/code><\/pre>\n\n\n\n<p>Here&#8217;s how I wired up the tipping bucket mode. The relay output was just connected to a digital IO pin on the ESP.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"1024\" data-id=\"2504\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/20230523_190733-768x1024.jpg\" alt=\"\" class=\"wp-image-2504\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/20230523_190733-768x1024.jpg 768w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/20230523_190733-225x300.jpg 225w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/20230523_190733-1152x1536.jpg 1152w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/20230523_190733.jpg 1536w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<\/figure>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a home-brew weather station and was looking into rain sensors when I discovered that you can get infrared (IR) rain detectors. A company in Minnesota sells one called the Hydreon RG-11. They shoot pulses of IR light around a plastic dome and monitor them on the other end. When rain hits &hellip; <a href=\"https:\/\/partofthething.com\/thoughts\/connecting-a-hydreon-infrared-rain-sensor-to-a-esp8266-or-arduino-or-raspberry-pi\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Connecting a Hydreon Infrared Rain Sensor to a ESP8266 (or Arduino or Raspberry Pi)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1929,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"federated","footnotes":""},"categories":[75],"tags":[],"class_list":["post-1924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-home-automation"],"_links":{"self":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/comments?post=1924"}],"version-history":[{"count":13,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1924\/revisions"}],"predecessor-version":[{"id":2506,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1924\/revisions\/2506"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/media\/1929"}],"wp:attachment":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/media?parent=1924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/categories?post=1924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/tags?post=1924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}