{"id":1821,"date":"2019-10-14T21:00:59","date_gmt":"2019-10-14T13:00:59","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1821"},"modified":"2020-12-12T22:50:50","modified_gmt":"2020-12-12T14:50:50","slug":"how-to-read-soil-moisture-level-with-raspberry-pi-and-a-yl-69-fc-28-moisture-sensor","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-read-soil-moisture-level-with-raspberry-pi-and-a-yl-69-fc-28-moisture-sensor\/","title":{"rendered":"How to read soil moisture level with Raspberry Pi and a YL-69\/FC-28 moisture sensor"},"content":{"rendered":"<p>When I did a <a href=\"https:\/\/www.techcoil.com\/blog\/review-of-the-raspberry-pi-4-model-b\/\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 4 Model B review<\/a>, I got myself a Raspberry Pi 4 Model B. Therefore, the current Raspberry Pi 3 Model B+ that I have been using for validating <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\/\" rel=\"noopener\" target=\"_blank\">Raspberry Pi ideas<\/a> became available for another <a href=\"https:\/\/www.techcoil.com\/glossary\/internet-of-things\/\" rel=\"noopener\" target=\"_blank\">IoT<\/a> project in the house.<\/p>\n<p>So <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\/\" rel=\"noopener\" target=\"_blank\">what should I do with my Raspberry Pi<\/a>?<\/p>\n<p>Since I have always wanted to build my own <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=indoor+herb+garden&ref=nb_sb_noss&linkCode=ll2&tag=clivsperswebs-20&linkId=4745e1884d3627434b5f2261b862bc7a&language=en_US\" rel=\"noopener\" target=\"_blank\">indoor herb garden<\/a>, I plan to use it to help my plants grow well.<\/p>\n<p>When I bought the <a href=\"https:\/\/www.amazon.com\/Relassy-Spectrum-Gooseneck-Replaceable-Professional\/dp\/B07C68N7PC\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=10198ffcf15bbe10364d57231902d1da&language=en_US\" rel=\"noopener\" target=\"_blank\">LED Grow Lights<\/a>, I am able to <a href=\"https:\/\/www.youtube.com\/watch?v=4sdWO1C4KOg\" rel=\"noopener\" target=\"_blank\">give my plants consistent lighting<\/a>. Given that consistent lighting is solved, I can use my Raspberry Pi 3 to monitor soil moisture so that I can provide water my plants better.<\/p>\n<p>Given these points, this is a proof of concept that I did to read soil moisture level with my Raspberry Pi and a YL-69\/FC-28 moisture sensor.<\/p>\n<h2>Parts used in the soil moisture monitoring prototype<\/h2>\n<p>Since I had already <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-raspbian-buster-for-your-raspberry-pi-4-model-b\/\" rel=\"noopener\" target=\"_blank\">setup Raspbian Buster to run my Raspberry Pi 4 Model B<\/a>, I built this soil monitoring proof of concept with the following parts:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.amazon.com\/seeed-studio-Raspberry-Computer-Model\/dp\/B07WBZM4K9\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=b6322a731737dbd3ad9fb56715f7923d&language=en_US\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 4 Model B<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/SanDisk-128GB-microSDXC-Memory-Adapter\/dp\/B073JYC4XM\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=d15fe377bb14e919cb71ac714063ca11&language=en_US\" rel=\"noopener\" target=\"_blank\">SanDisk 128GB Ultra microSDXC UHS-I Memory Card<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll2&tag=clivsperswebs-20&linkId=ce5551c1e1fb9b638d1dd018640bd87b&language=en_US\" rel=\"noopener\" target=\"_blank\">Solderless Prototype Breadboard<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/EDGELEC-Breadboard-Optional-Assorted-Multicolored\/dp\/B07GD2BWPY\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=ed1cd66ecc676e5369d92c76673bff51&language=en_US\" rel=\"noopener\" target=\"_blank\">Breadboard Jumper Wires<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/Microchip-MCP3008-I-10-Bit-ADC-Pack\/dp\/B01HGCSGXM\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=cabe336d1363c9835a0f31469c60a926&language=en_US\" rel=\"noopener\" target=\"_blank\">MCP3008 I\/P ADC chip<\/a><\/li>\n<\/ul>\n<p>What we will be covering in this article should work for a version of Raspberry Pi with 40 GPIO pins, including my free <a href=\"https:\/\/www.amazon.com\/ELEMENT-Element14-Raspberry-Pi-Motherboard\/dp\/B07P4LSDYV\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=ea24460e52886084725d99e91faed852&language=en_US\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 3 Model B+<\/a> or a <a href=\"https:\/\/www.techcoil.com\/blog\/why-buy-the-raspberry-pi-zero-w\/\" target=\"_blank\" rel=\"noopener\">Raspberry Pi Zero W<\/a>.<\/p>\n<h2>Taking a closer look at the YL-69\/FC-28 moisture sensor<\/h2>\n<p>If you look at the M393 Comparator Module that comes with your moisture sensor, you will find 2 pins at the top and 4 pins at the bottom:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/M393-Comparator-Module-that-came-along-with-FC-28-moisture-sensor.jpg\" class=\"aligncenter\" alt=\"M393 Comparator Module that came along with FC-28 moisture sensor\"><\/p>\n<p>We will connect the 2 pins at the top to the soil moisture sensor:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/FC-28-moisture-sensor-connected-to-the-M393-comparator-module.jpg\" class=\"aligncenter\" alt=\"FC-28 moisture sensor connected to the M393 comparator module\"><\/p>\n<p>The 4 pins at the bottom will be connected either, directly or indirectly, to the Raspberry Pi.<\/p>\n<ul>\n<li>AO<\/li>\n<li>DO<\/li>\n<li>GND<\/li>\n<li>VCC<\/li>\n<\/ul>\n<h3>The VCC and GND pins<\/h3>\n<p>The <strong>VCC<\/strong> pin will need to be connected to a power source of 3.3V while the <strong>GND<\/strong> pin to the ground.<\/p>\n<h3>Values that can be read from the DO pin<\/h3>\n<p>The <strong>DO<\/strong> pin can be connected to one of Raspberry Pi GPIO pins directly. However, the value that can be read is either a 0 or a 1. In addition to that, the sensitivity for this pin to output a 0 can be manipulated by turning the screw on the blue potentiometer.<\/p>\n<p>For example, when you turn the screw clock-wise to its limit, the pin will always return a 0. This is what will be returned when the soil moisture sensor is immersed in a glass of water. In addition to that, the LED labeled <strong>DO-LED<\/strong> will light up:<br \/>\n<img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/M393-Comparator-Module-with-2-pins-at-the-top-connected-VCC-GND-and-DO-pin-connected-and-DO-LED-lighted-up.jpg\" class=\"aligncenter size-full wp-image-1825\" alt=\"M393 Comparator Module with 2 pins at the top connected, VCC GND and DO pin connected and DO-LED lighted up\"><\/p>\n<h3>Values that can be read from the AO pin<\/h3>\n<p>In order for our Raspberry Pi to read from the <strong>AO<\/strong> pin, we need to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-mcp3008-i-p-chip-to-help-your-raspberry-pi-read-digital-data-from-analog-sensors\/\" rel=\"noopener\" target=\"_blank\">use an analog to digital converter like the MCP3008 chip<\/a>. When connected to an analog to digital converter, our Raspberry Pi can get a wider range of values from the <strong>AO<\/strong> pin. Therefore, readings from this pin are more useful as compared to readings from the <strong>DO<\/strong> pin.<\/p>\n<h2>Reading from the DO pin of the soil moisture sensor with the Raspberry Pi<\/h2>\n<p>If you only want to be notified when your soil moisture had reached a threshold that you had set with the potentiometer, then you can read the <strong>DO<\/strong> pin directly from your Raspberry Pi.<\/p>\n<h3>Connecting the DO pin of the soil moisture sensor directly to your Raspberry Pi<\/h3>\n<p>The following diagram depicts how you can wire your soil moisture sensor directly to your Raspberry Pi:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/FC-28-moisture-sensor-directly-connected-to-Raspberry-Pi-Model-3.gif\" class=\"aligncenter\" alt=\"FC-28 moisture sensor directly connected to Raspberry Pi Model 3\"><\/p>\n<p>In this example, we had connected the <strong>DO<\/strong> pin to <strong>GPIO4<\/strong> pin of our Raspberry Pi.<\/p>\n<p>Therefore, we can <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-python-3-virtual-environments-to-run-python-3-applications-on-your-raspberry-pi\/\" rel=\"noopener\" target=\"_blank\">use a Python 3 virtual environment<\/a> (with <a href=\"https:\/\/pypi.org\/project\/RPi.GPIO\/\" rel=\"noopener\" target=\"_blank\">RPi.GPIO<\/a> and <a href=\"https:\/\/gpiozero.readthedocs.io\/en\/stable\/\" rel=\"noopener\" target=\"_blank\">gpiozero<\/a> dependencies installed) to run the following Python script to probe if we need to water the soil:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom gpiozero import DigitalInputDevice\r\nimport time\r\n\r\nd0_input = DigitalInputDevice(4)\r\n\r\nwhile True:\r\nif (not d0_input.value):\r\n    print('Moisture threshold reached!!!')\r\nelse:\r\n    print('You need to water your plant')\r\n    time.sleep(2)\r\n<\/pre>\n<h2>Reading the soil moisture sensor through a MCP3008 chip<\/h2>\n<p>Since the AO pin can give a wider spectrum of readings, we will be able get a percentage of how wet our soil is.<\/p>\n<h3>Connecting the soil moisture sensor to the Raspberry Pi through a MCP3008 chip<\/h3>\n<p>As I had mentioned earlier, we will need a Analog to Digital convertor like the MCP3008 chip to read the analog pin (AO) of our soil moisture sensor.<\/p>\n<p>Given that, this is how we can connecting the soil moisture sensor to the Raspberry Pi through a MCP3008 chip:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/FC-28-moisture-sensor-connected-to-Raspberry-Pi-Model-3B-via-MCP3008-chip.gif\" class=\"aligncenter\" alt=\"FC-28 moisture sensor connected to Raspberry Pi Model 3B via MCP3008 chip\"><\/p>\n<p>In this setup, we had connected AO pin of our soil moisture sensor to the CH0 pin of the MCP3008 pin.<\/p>\n<p>Therefore, we can use the same Python script that from <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-mcp3008-i-p-chip-to-help-your-raspberry-pi-read-digital-data-from-analog-sensors\/\" rel=\"noopener\" target=\"_blank\">how to use the MCP3008-I\/P chip to help your Raspberry Pi read digital data from analog sensors<\/a> to probe the soil moisture sensor periodically:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport busio\r\nimport digitalio\r\nimport board\r\nimport time\r\nimport adafruit_mcp3xxx.mcp3008 as MCP\r\nfrom adafruit_mcp3xxx.analog_in import AnalogIn\r\n\r\n# create the spi bus\r\nspi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)\r\n\r\n# create the cs (chip select)\r\ncs = digitalio.DigitalInOut(board.CE0)\r\n\r\n# create the mcp object\r\nmcp = MCP.MCP3008(spi, cs)\r\n\r\n# create an analog input channel on pin 0\r\nchan = AnalogIn(mcp, MCP.P0)\r\n\r\nwhile True:\r\n    print('Raw ADC Value: ', chan.value)\r\n    print('ADC Voltage: ' + str(chan.voltage) + 'V')\r\n    time.sleep(2)\r\n<\/pre>\n<h3>How to interpret the voltage level as level of moisture in your soil<\/h3>\n<p>When I ran the Python script without immersing the soil moisture sensor into a cup of water, I got a voltage reading of close to <strong>3.3V<\/strong>. On the other hand, when I immersed the soil moisture sensor into a cup of water, I got a voltage reading of around <strong>1.3V<\/strong>.<\/p>\n<p>Given these points, we can conclude that the voltage reading from the soil moisture sensor will be <strong>lower for higher moisture levels<\/strong>. Therefore when we detect a value of close to 3.3V, we better be giving our plant some water.<\/p>\n<h2>Summary<\/h2>\n<p>By building this proof of concept, I was able to verify that:<\/p>\n<ul>\n<li>A digital output can be read directly with a Raspberry Pi. However, possible values is either a 0 or 1. When a value of 0 was read from the DO pin, we know that the moisture threshold had been met. Such a threshold can be adjusted by turning the screw on the blue potentiometer.<\/li>\n<li>An analog output can be read with a Raspberry Pi via an analog to digital converter like the MCP3008 chip. Since the range of values is wider, we will have a sense of how wet the soil is. When the <strong>AO<\/strong> pin return a <strong>higher value<\/strong>, we will know that the <strong>soil moisture level is low<\/strong>.<\/li>\n<li>When we use a MCP3008 chip in our Raspberry Pi, the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_CircuitPython_MCP3xxx\" rel=\"noopener\" target=\"_blank\">Adafruit CircuitPython library for MCP300x SPI ADC<\/a> can be used to read the analog data from the YL-69\/FC-28 soil moisture sensor.<\/li>\n<\/ul>\n<p>Given these points, I had gathered what I need for building a soil monitoring system for my indoor herb garden.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-tn\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-tn&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-tn&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1821&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When I did a <a href=\"https:\/\/www.techcoil.com\/blog\/review-of-the-raspberry-pi-4-model-b\/\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 4 Model B review<\/a>, I got myself a Raspberry Pi 4 Model B. Therefore, the current Raspberry Pi 3 Model B+ that I have been using for validating <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\/\" rel=\"noopener\" target=\"_blank\">Raspberry Pi ideas<\/a> became available for another <a href=\"https:\/\/www.techcoil.com\/glossary\/internet-of-things\/\" rel=\"noopener\" target=\"_blank\">IoT<\/a> project in the house.<\/p>\n<p>So <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\/\" rel=\"noopener\" target=\"_blank\">what should I do with my Raspberry Pi<\/a>?<\/p>\n<p>Since I have always wanted to build my own <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=indoor+herb+garden&#038;ref=nb_sb_noss&#038;linkCode=ll2&#038;tag=clivsperswebs-20&#038;linkId=4745e1884d3627434b5f2261b862bc7a&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">indoor herb garden<\/a>, I plan to use it to help my plants grow well.<\/p>\n<p>When I bought the <a href=\"https:\/\/www.amazon.com\/Relassy-Spectrum-Gooseneck-Replaceable-Professional\/dp\/B07C68N7PC\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=10198ffcf15bbe10364d57231902d1da&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">LED Grow Lights<\/a>, I am able to <a href=\"https:\/\/www.youtube.com\/watch?v=4sdWO1C4KOg\" rel=\"noopener\" target=\"_blank\">give my plants consistent lighting<\/a>. Given that consistent lighting is solved, I can use my Raspberry Pi 3 to monitor soil moisture so that I can provide water my plants better.<\/p>\n<p>Given these points, this is a proof of concept that I did to read soil moisture level with my Raspberry Pi and a YL-69\/FC-28 moisture sensor.<\/p>\n","protected":false},"author":1,"featured_media":1828,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[691,484,689,226,233,240,412,692],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/FC-28-moisture-sensor-connected-to-Raspberry-Pi-Model-3B-via-MCP3008-chip.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-tn","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1821"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1821"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1821\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1828"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}