Arduino Capacitive Soil Moisture Sensor (DIY) with ESP32

Many publications exist about moisture sensors for applications, e.g. plant watering systems. Making a DIY Sensor is fun and cost-saving.

Dec 21, 2018

37721 views

7 respects

Components and supplies

1

Moisture Level Indicator

1

Capacitor 100 nF

1

Geekworm NodeMCU-32S ESP32S Lua

1

capacitive Soil Moisture Sensor

1

Resistor 1M ohm

1

1N4007 – High Voltage, High Current Rated Diode

1

Resistor 10k ohm

1

Resistor 221 ohm

1

Arduino Mega 2560

Tools and machines

1

Soldering iron (generic)

1

common workshop tools

Apps and platforms

1

Arduino IDE

Project description

Code

Soil_Moisture_Sensor.ino

arduino

Downloadable files

Fritzing diagram Soil Moisture Sensor

Breadboard set up for measuring soil moisture with a DIY capacitive sensor

Fritzing diagram Soil Moisture Sensor

Fritzing diagram Soil Moisture Sensor

Breadboard set up for measuring soil moisture with a DIY capacitive sensor

Fritzing diagram Soil Moisture Sensor

Comments

Only logged in users can leave comments

ian27

2 years ago

Hi pedro! I am a student and I just wanna ask if you can explain the schematic/circuit diagram which can help me in my upcoming defense on school, hoping for your reply, thank you!

Anonymous user

2 years ago

Dear Pedro52, (Nameste!) I have tested your project for single sensor and it working fine. In my project i have to use 4 sensors, so i have few question. 1.) Should I use 4 channels, and 4 analog read pins, each for 1 sensor. 2.) Should I use only one common channel for every sensor and 4 analog read pins. 3.) Can it be done unsing only one common R/C network or 4 R/C network for each sensor. What is the right configuraion, Please help. Thank you so much.

Pedro52

2 years ago

Dear Pardeep I have a pending project with the same need for 4 separate sensors. However I am currently busy with some other demanding things. To answer your question, according to information you need to allocate separate PWM channels for each separate sensor. And you need 4 separate analog read pins. An ESP 32 has 14. should be sufficient :) . For every sensor also a separate RC network will be needed, I don't believe you can avoid that. Please let me know how you are proceeding, would be great when you get it working for 4 sensors. Best Regards Pedro52

Pedro52

2 years ago

Dear Pardeep Thank you for your message, nice that have 2 separate sensors working. Interesting to hear that only the ADC1 pins work with analogRead. Any there are still 6 of those. Best regards and good luck with your project

Anonymous user

2 years ago

Thank you very much. now I am using 2 sensors which has a common PMW channel and each sensor has separate analog read and its working fine for me. Also i want to share major problem with esp32, analogRead only works for ADC1 pins if using it online, ADC2 inputs cannot be used.

Anonymous user

2 years ago

hello pedro! i really appreciate your tutorial. I want to make one of this because i've tested commercial capacitive sensor (based on 555) and they aren't quite accurate. So, before doing this, I've one question: can the lenght, size and distance between the two rods influence the reads? I haven't the fondue forks so searching for something else. Really thx.

Pedro52

2 years ago

Dear Claudio Thank you very much for your reaction. I have chosen the fondue forks because I had them available, but they also be bought a really affordable prices in cheap stores like e.g. Action ( I don't know if they are present in your country); Someone suggested to use stainless steel welding rods. Should work as well. The length and distance certainly has an influence, if you use the same type of connector as I did you get the same distance between the pens. In any case you have to calibrate to define the dry and wet figures in the software. Hope you succeed, if so let me know. Best Regards

Anonymous user

2 years ago

Hi there, Very interesting project, thank you for sharing. I built it and got it to work without any problems. Now want to send the moisture data to a remote unit via ESP Now but as soon as I set WiFi.mode(WIFI_STA); it no longer reads the values from the sensor. Have you come across something like this?

Anonymous user

2 years ago

Hi your project is very useful for me as I am working on a similar project (humidity sensor on ESP12 transmitting MQTT info by Wifi and self powered) and because the Pwm solution is an interesting approach. My question is ; are you sure to use a capacitive sensor ? did you protect the metal (fork is a good idea !) with paint or varnish ?

Pedro52

2 years ago

Dear Denis Thank you for your message , I did not paint or varnish the metal because it is allready chromium plated.

Anonymous user

2 years ago

Awesome! really loved it, I adapted it to Arduino mega and it worked nicely, I used an old library to control the frequency of the pwm and some stainless steel TIG welding rod for electrodes. That's the library I used if someone need it: https://code.google.com/archive/p/arduino-pwm-frequency-library/downloads Thanks for this really nice project.

Anonymous user

2 years ago

Hi MadSurveyor, Could you share with us how did you adopted for Mega? I1m trying to use on Arduino Uno/Arduino IDE, but its giving this error: "'ledcSetup' was not declared in this scope". Using Adafruit_Neopixel or not, same error message. Thank you so much.

Pedro52

2 years ago

Thank You!, Good to hear that it can be done on a Mega as well, very useful tip. The stainless steel welding rod is an excellent idea

Anonymous user

2 years ago

Hi, great post. I think I found a mistake in your circuit diagram: The P04 is actually connected on the other side of D1. At least it is like that in the "circuit diagram (made with Fritzing)". It also makes more sense, since by that the sensor pin measures the equalized voltage. I found a nice visualization of this circuit at https://thecavepearlproject.org/2020/10/27/hacking-a-capacitive-soil-moisture-sensor-for-frequency-output/ (Schematic before conversion).

Pedro52

2 years ago

Dear jkb You are very right, the Fritzing diagram is correct, the electrical diagram is wrong the P04 should be connected to the kathode of the diode. Thank you very much for the link. Pedro52

Anonymous user

2 years ago

Excellent innovation!!! Isn't his is a resistive sensor? The readings are a measure of the resistance due to the current flowing between the rods. Readings from a capacitor sensor are a measure of the capacitance between the rods.

Anonymous user

2 years ago

Thank you for your great ideas. I had already bought a moisture sensor made in China but it rusted in a few weeks. I used fondue forks as well with ESP32 and upload the moisture value to an MQTT broker, that way I can check the value on my cellphone wherever I am (library is Adafruit MQTT). ESP32 is put into deep sleep for 1 hour and runs on 4 AA batteries. The same Arduino sketch also reads the water level in a water tank used for water drip irrigation system and similarly pushes it to MQTT broker. I'm planning on addnig a solar system and check voltage values as well.

Anonymous user

2 years ago

Hi, Pedro! 'Goedenavond'! I'm brazilian and starting Arduino's studies. Very nice device this one you create. Good job. Congratulations. I was using Capacitive Moisture Sensor v1.2 boughten on China market, but it's "freezing" it's readings after some days, like if the soil was very wet. I suppose that this issue may occur because of electrical induction surges caused by far lightings on outdoor usages. I've noted that all 3 sensors had changed its 1M ohm capacitor's values. So, I'm intending to make one sensor like this you create, but I need to run it on Arduino Uno clone, made in China. Is it possible? May I change any parameters on PWM pin (Uno has PWM on 3, 5, 6, 9, 10 e 11 pins)? 'Duizendmaal dank'!

Anonymous user

2 years ago

'Dank ye' Pedro52! I have some Esp32 modules here, but my main project had made with Arduino, so I think easier to try anothers read to use sensors before. I bought someones called Crowtail, a reddish capacitive moisture soil sensors, founded on AliExpress. Do you know it? Do you mind it should have good quality? Second test could be this wheatstone brigde, that I don't know it till now, neither how does it works. So, let's continue to research. As soon I find a solution, I'll share it with you. I really sure that we can help one to another, working like partners in near future. Please, forgive me any english mistakes. Have a nice day n 'tot ziens'!

Anonymous user

2 years ago

Dear Pedro, I read that Arduino can generate maximum of 62500Hz on pins 5, 6 and about a half on pins 3, 9, 10, 11. But the first problem found is ledcSetup comand or function not recognized on Arduino IDE during compilation process ("was not declared in this scope" message). Do you mind what solution or alternative way to generate this signal? Does tone or other sound function also could work? Can I input and read 5v with this concept? I made a irrigation system composed of water flow sensor, 4 sectors with hygrometers, solenoid valves and a RTC to schedule irrigations times. Script working n all others components working nice. So, I really need to have these moisture soil sensors working perfectly, in order to save water. Its crucial for this project. I hope that this device you developed may solve 'freezing' readings of capacitive ones, because your device doen't stay on every time, wich may avoid static or induction surges because of far lightings. May I wrong? So, if you should interest, we could be commercial partners in near future. My son is moving to live in Amsterdan, where will work for Danone from march. He is also called Pedro. So, what about to share and work together in this project? Best reggards. Fernando

Pedro52

2 years ago

Dear Fernando to answer to your questions: - the ledcSetup function is typical for ESP32 and does not work on an Uno - 62,5 Khz is still far to low for this application - you can still use the hardware concept of the sensor but instead apply it as a resistive sensor. - the best solution in such case would be to put the sensor in a wheatstone bridge circuit, which is excited with 5V pulses at intervals only when you want to take a measurement, thus avoiding corrosion. - a little experimentation with the resistors is needed and viewing the arduino plotter to see the effects of various moisture levels Hope this helps you Best regards Pedro52

Pedro52

2 years ago

Dear Fernando Ook Goedenavond, Thank you for your nice message, it is encourage me to go on producing interesting projects. I am afraid that the Arduino Uno does not achieve frequencies higher than 25 kHz, whereas in my project on an ESP32 I am using 600 kHz. To be honest I have not tried it at 25 kHz, but please feel free to try and check on the Arduino plotter if you can discriminate different levels. Groeten Pedro52

Anonymous user

3 years ago

Hi there, Very interesting project, thank you for sharing. I built it and got it to work without any problems. Now want to send the moisture data to a remote unit via ESP Now but as soon as I set WiFi.mode(WIFI_STA); it no longer reads the values from the sensor. Have you come across something like this?

Anonymous user

4 years ago

Hi, great post. I think I found a mistake in your circuit diagram: The P04 is actually connected on the other side of D1. At least it is like that in the "circuit diagram (made with Fritzing)". It also makes more sense, since by that the sensor pin measures the equalized voltage. I found a nice visualization of this circuit at https://thecavepearlproject.org/2020/10/27/hacking-a-capacitive-soil-moisture-sensor-for-frequency-output/ (Schematic before conversion).

Pedro52

2 years ago

Dear jkb You are very right, the Fritzing diagram is correct, the electrical diagram is wrong the P04 should be connected to the kathode of the diode. Thank you very much for the link. Pedro52

Pedro52

2 years ago

Dear jkb You are very right, the Fritzing diagram is correct. I have meanwhile corrected the electrical diagram in the tutorial. Thank you for the above link Best Regards

Anonymous user

4 years ago

hello pedro! i really appreciate your tutorial. I want to make one of this because i've tested commercial capacitive sensor (based on 555) and they aren't quite accurate. So, before doing this, I've one question: can the lenght, size and distance between the two rods influence the reads? I haven't the fondue forks so searching for something else. Really thx.

Pedro52

2 years ago

Dear Claudio Thank you very much for your reaction. I have chosen the fondue forks because I had them available, but they also be bought a really affordable prices in cheap stores like e.g. Action ( I don't know if they are present in your country); Someone suggested to use stainless steel welding rods. Should work as well. The length and distance certainly has an influence, if you use the same type of connector as I did you get the same distance between the pens. In any case you have to calibrate to define the dry and wet figures in the software. Hope you succeed, if so let me know. Best Regards

Anonymous user

5 years ago

Dear Pedro52, (Nameste!) I have tested your project for single sensor and it working fine. In my project i have to use 4 sensors, so i have few question. 1.) Should I use 4 channels, and 4 analog read pins, each for 1 sensor. 2.) Should I use only one common channel for every sensor and 4 analog read pins. 3.) Can it be done unsing only one common R/C network or 4 R/C network for each sensor. What is the right configuraion, Please help. Thank you so much.

Pedro52

2 years ago

Dear Pardeep I have a pending project with the same need for 4 separate sensors. However I am currently busy with some other demanding things. To answer your question, according to information you need to allocate separate PWM channels for each separate sensor. And you need 4 separate analog read pins. An ESP 32 has 14. should be sufficient :) . For every sensor also a separate RC network will be needed, I don't believe you can avoid that. Please let me know how you are proceeding, would be great when you get it working for 4 sensors. Best Regards Pedro52

Pedro52

2 years ago

Dear Pardeep Thank you for your message, nice that have 2 separate sensors working. Interesting to hear that only the ADC1 pins work with analogRead. Any there are still 6 of those. Best regards and good luck with your project

Anonymous user

2 years ago

Thank you very much. now I am using 2 sensors which has a common PMW channel and each sensor has separate analog read and its working fine for me. Also i want to share major problem with esp32, analogRead only works for ADC1 pins if using it online, ADC2 inputs cannot be used.

Anonymous user

5 years ago

Hi, Pedro! 'Goedenavond'! I'm brazilian and starting Arduino's studies. Very nice device this one you create. Good job. Congratulations. I was using Capacitive Moisture Sensor v1.2 boughten on China market, but it's "freezing" it's readings after some days, like if the soil was very wet. I suppose that this issue may occur because of electrical induction surges caused by far lightings on outdoor usages. I've noted that all 3 sensors had changed its 1M ohm capacitor's values. So, I'm intending to make one sensor like this you create, but I need to run it on Arduino Uno clone, made in China. Is it possible? May I change any parameters on PWM pin (Uno has PWM on 3, 5, 6, 9, 10 e 11 pins)? 'Duizendmaal dank'!

Pedro52

2 years ago

Dear Fernando to answer to your questions: - the ledcSetup function is typical for ESP32 and does not work on an Uno - 62,5 Khz is still far to low for this application - you can still use the hardware concept of the sensor but instead apply it as a resistive sensor. - the best solution in such case would be to put the sensor in a wheatstone bridge circuit, which is excited with 5V pulses at intervals only when you want to take a measurement, thus avoiding corrosion. - a little experimentation with the resistors is needed and viewing the arduino plotter to see the effects of various moisture levels Hope this helps you Best regards Pedro52

Pedro52

2 years ago

Dear Fernando Ook Goedenavond, Thank you for your nice message, it is encourage me to go on producing interesting projects. I am afraid that the Arduino Uno does not achieve frequencies higher than 25 kHz, whereas in my project on an ESP32 I am using 600 kHz. To be honest I have not tried it at 25 kHz, but please feel free to try and check on the Arduino plotter if you can discriminate different levels. Groeten Pedro52

Anonymous user

2 years ago

Dear Pedro, I read that Arduino can generate maximum of 62500Hz on pins 5, 6 and about a half on pins 3, 9, 10, 11. But the first problem found is ledcSetup comand or function not recognized on Arduino IDE during compilation process ("was not declared in this scope" message). Do you mind what solution or alternative way to generate this signal? Does tone or other sound function also could work? Can I input and read 5v with this concept? I made a irrigation system composed of water flow sensor, 4 sectors with hygrometers, solenoid valves and a RTC to schedule irrigations times. Script working n all others components working nice. So, I really need to have these moisture soil sensors working perfectly, in order to save water. Its crucial for this project. I hope that this device you developed may solve 'freezing' readings of capacitive ones, because your device doen't stay on every time, wich may avoid static or induction surges because of far lightings. May I wrong? So, if you should interest, we could be commercial partners in near future. My son is moving to live in Amsterdan, where will work for Danone from march. He is also called Pedro. So, what about to share and work together in this project? Best reggards. Fernando

Anonymous user

2 years ago

'Dank ye' Pedro52! I have some Esp32 modules here, but my main project had made with Arduino, so I think easier to try anothers read to use sensors before. I bought someones called Crowtail, a reddish capacitive moisture soil sensors, founded on AliExpress. Do you know it? Do you mind it should have good quality? Second test could be this wheatstone brigde, that I don't know it till now, neither how does it works. So, let's continue to research. As soon I find a solution, I'll share it with you. I really sure that we can help one to another, working like partners in near future. Please, forgive me any english mistakes. Have a nice day n 'tot ziens'!

Anonymous user

5 years ago

Thank you for your great ideas. I had already bought a moisture sensor made in China but it rusted in a few weeks. I used fondue forks as well with ESP32 and upload the moisture value to an MQTT broker, that way I can check the value on my cellphone wherever I am (library is Adafruit MQTT). ESP32 is put into deep sleep for 1 hour and runs on 4 AA batteries. The same Arduino sketch also reads the water level in a water tank used for water drip irrigation system and similarly pushes it to MQTT broker. I'm planning on addnig a solar system and check voltage values as well.

Anonymous user

6 years ago

Excellent innovation!!! Isn't his is a resistive sensor? The readings are a measure of the resistance due to the current flowing between the rods. Readings from a capacitor sensor are a measure of the capacitance between the rods.

Anonymous user

6 years ago

Hi your project is very useful for me as I am working on a similar project (humidity sensor on ESP12 transmitting MQTT info by Wifi and self powered) and because the Pwm solution is an interesting approach. My question is ; are you sure to use a capacitive sensor ? did you protect the metal (fork is a good idea !) with paint or varnish ?

Pedro52

2 years ago

Dear Denis Thank you for your message , I did not paint or varnish the metal because it is allready chromium plated.

MadSurveyor

6 years ago

Awesome! really loved it, I adapted it to Arduino mega and it worked nicely, I used an old library to control the frequency of the pwm and some stainless steel TIG welding rod for electrodes. That's the library I used if someone need it: https://code.google.com/archive/p/arduino-pwm-frequency-library/downloads Thanks for this really nice project.

Pedro52

2 years ago

Thank You!, Good to hear that it can be done on a Mega as well, very useful tip. The stainless steel welding rod is an excellent idea

Anonymous user

2 years ago

Hi MadSurveyor, Could you share with us how did you adopted for Mega? I1m trying to use on Arduino Uno/Arduino IDE, but its giving this error: "'ledcSetup' was not declared in this scope". Using Adafruit_Neopixel or not, same error message. Thank you so much.