Soil moisture value reading

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
ajocius
Posts: 29
Joined: Mon Feb 19, 2018 6:31 am

Soil moisture value reading

Post by ajocius » Sun Mar 04, 2018 10:41 pm

I have connected soil moisture sensor to the third pin from top on the left side (https://goo.gl/images/XUPYFj) marked as digital pin 36 on ESP32 (it is also A0 analog pin).
Soil moisture sensor has digital and analog data output (https://www.aliexpress.com/item/Free-sh ... 85387.html).

I have used simple code to just read value, test sensor:

Code: Select all

import machine
adc = machine.ADC(machine.Pin(36))
print(adc.read())
This was constantly giving my output of 4095, while sensor was dry, hanging in the air and also submerged in water glass. Tried tonnectint to digital or analog output of the sensor, same value of 4095 is returned. I understood something is not functioning as I should not be receiving same value.

I couldn't find the cause, so i thought of using another pin on ESP32. Changed code first to use Pin(39), which is one below on the board . Just ran the code to see what value will it give prior I move physical wire from Pin36 to Pin39 to see the value prior I connect sensor there. Was expecting either error code or zero value. To my surprise I got value of around 2000 returned. So i did several attempts to read value while touching sensor with fingers, or putting sensor into water, this time value was changing. It was around 700 in water (not total sensor was submerged), value come back to around 2000 once I pulled it out of the water.

What is going on here? Why can't I read value on Pin36, like intended?

ajocius
Posts: 29
Joined: Mon Feb 19, 2018 6:31 am

Re: Soil moisture value reading

Post by ajocius » Thu Mar 08, 2018 8:55 am

I suspect it is a question of putting resistors as analog input should be limited to 1V, while sensor is connected to 3,3V. At least that is what I got from reading multiple posts in various forums. However I struggle to get it to work right. I followed other post on this forum that was about pulse sensor connection to analog PIN. Here it is:
You need two resistors in series. I suggest 2.2KΩ connected between analog I/P and 0V. Then use a 4.7KΩ resistor between analog I/P and the pulse sensor output. A voltage of 3.3V on the pulse sensor will produce
3.3 * 2.2 / (4.7 + 2.2) = 1.05V on the analog input.
from this post:
viewtopic.php?t=3727

I measured voltage at analog input, it was 0,42V. Soil moisture reading was around 1400 when dry, around 1000 when submerged in water. Something does not still feel right, although it is step forward since my first post in my opinion.

I lack basic understanding how this works to understand what else should I (can I) change. I am not sure what that value of 1400 or 1000 that analog input reads mean. Is it mA value or some kind of voltage representation? What is expected range? See that other posts (mostly Arduino IDE based) are referring to range 0-1000.

I would appreciate if someone can help to read soil moisture values.

User avatar
pythoncoder
Posts: 2904
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Soil moisture value reading

Post by pythoncoder » Fri Mar 09, 2018 9:34 am

The ADC's produce outputs in the range 0 to 4095. As configured by MicroPython, 0 corresponds to 0V and 4095 corresponds to 1.0V. So a voltage of 0.3V would produce a reading of around 0.3*4095 = 1228.

You need to arrange the circuit so that voltages applied to the pin are constrained to that range. I would produce a 1V supply by connecting a 100Ω resistor to 0V and a 220Ω resistor between it and 3.3V. Check that the voltage at the junction is close to 1V. I would wire one side of the sensor to 0V and the other side both to the ADC and to your 1V supply via a resistor. The value of the resistor is going to be a matter of trial and error. For best sensitivity you want to see about 0.5V at the ADC input for a "typical" level of moisture. Not soaking wet or bone dry, but what you'd normally use.

Mapping the voltages read by the ADC onto some established index of soil moisture may well be something of a black art. Unless we happen to have an expert amongst us you might find more information on an agricultural forum ;) If you just want a threshold for when to water then you may need to rely on your plantsman's expertise. Mine is pretty minimal...
Peter Hinch

ajocius
Posts: 29
Joined: Mon Feb 19, 2018 6:31 am

Re: Soil moisture value reading

Post by ajocius » Sat Mar 10, 2018 9:53 pm

Thanks Pythoncoder! Actualy I was quoting you in my above post, where you suggested what resistors to use to get down to 1V on ADC. You are suggesting different resistor combo now. Am I right thinking that both resistor combinations would work?
3.3 * 2.2 / (4.7 + 2.2) = 1.05V
3.3 * 0.1 / (0.1+0.22) = 1.03V

I would also like to check if I understand correctly connection of resistors (sorry, this might be obvious to some, but not me)
My sensor has 3 pins (actually 4, but digital output is nor relevant in this threat): GND, VCC, A_Out
Lets call analog on ESP32 A_in

Do I connect 100 ohm resistor between A_in and 3.3 V power supply, and 330 ohm resistor between A_Out on sensor and A_in on ESP32?

I am not sure i understood suggested connection and since i do not understand the logic of that connection, then I am afraid to connect it wrong.

User avatar
dhylands
Posts: 2843
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Soil moisture value reading

Post by dhylands » Sun Mar 11, 2018 12:12 am

Here's a schematic:
Image
The input voltage comes from your sensor and the output voltage goes to the ADC. The bottom line in the schematic is often connected to ground.

Vout = Vin * R2 / (R1 + R2)

Using smaller values for R1 and R2 causes more current to be consumed. Some ADCs need a low impedance input, some don't.

User avatar
pythoncoder
Posts: 2904
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Soil moisture value reading

Post by pythoncoder » Sun Mar 11, 2018 11:17 am

Either will work but the reason I suggested this
Image
is that you only need to adjust one resistor (R3) to get the scaling factor you need.
Peter Hinch

ajocius
Posts: 29
Joined: Mon Feb 19, 2018 6:31 am

Re: Soil moisture value reading

Post by ajocius » Sun Mar 11, 2018 8:04 pm

thank you. I see the formula in dhylands post, which converts 3,3V to 1V if 100 Ohm and 220 Ohm are used. I thought of testing it live. I assumed that divided voltage i would connect to sensor Ground and VCC inputs and then wire Analog output on sensor directly to analog input on ESP32. Is it correct?

The pythoncoder posted scheme with one more resistor (R3=2,2 kOhm). What is the role of it and how does it work with R1 and R2 given voltage division formula posted by dhyland?

How do you guys visualize connection schemes? I could try to make scheme myself with all connections (including ESP32 and sensor) it would be easier to verify.

After I manage to connect one sensor properly, my follow question will be about connecting more than one sensor. What is important to consider then.

loboris
Posts: 266
Joined: Fri Oct 02, 2015 6:19 pm

Re: Soil moisture value reading

Post by loboris » Sun Mar 11, 2018 8:32 pm

R3 in pythoncoder's schematics is not needed and changing its value will not influence the voltage set by the voltage divider (R1&R2), assuming the ADC input is high impedance.

ajocius
Posts: 29
Joined: Mon Feb 19, 2018 6:31 am

Re: Soil moisture value reading

Post by ajocius » Mon Mar 12, 2018 8:14 am

ok, so maybe pythoncoder suggested that 2,2 kOhm resistor to reduce inpendance as it is needed for ESP32 input pins? I am not sure what impendance is considered high in this case.

User avatar
pythoncoder
Posts: 2904
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Soil moisture value reading

Post by pythoncoder » Mon Mar 12, 2018 8:45 am

My intention is that the soil moisture device is connected across J1, with the ADC reading the voltage at J1 pin 1.

The voltage at the junction of the three resistors should be about 1.0V. The voltage divider R1/R2 ensures that the voltage across the sensor (and hence on the ADC) can never exceed 1V.

The voltage divider comprising R3 and the sensor defines the voltage presented to the ADC. The value of R3 should be chosen so that, at the ideal target level of moisture, the voltage at the ADC is somewhere around 0.5V. This will maximise the sensitivity around the target level of moisture.

Elementary network theory shows you can achieve the same outcome with just two resistors. The benefit of the scheme I suggested is that only one resistor (R3) needs to be select-on-test. With the two resistor circuit you have to adjust the values of both resistors to meet the two criteria: that the ADC input never exceeds 1.0V and that, at the target moisture value, the ADC input is about 0.5V.
Peter Hinch

Post Reply