Do I need a voltage divider, ADC or something else?

So I know you will probably groan when I say, I am quite new to this (despite being quite old) and then again when I tell you I am building a greenhouse monitoring system; but please bear with me.

I am on my umpteenth incarnation of this project having originally built it work with a nano using the basic of 1 DHT11, 1 Soil Moisture Probe and a gas sensor. I had it working satisfactorily (relatively sensible readings from these rather unstable moisture modules) but decided it would be nice to monitor the status when I am not here.

I juggled things around, got rid of the MQ135 sensor and added more soil moisture probes. Wanting to monitor it remotely I swapped the board from a Nano to a NodeMCU WiFi Lua Lolin V3 ESP8266 ESP-12F.
I spent some time working out how to connect it WiFi, build the obligatory Blynk app to turn an LED on and off via my smartphone etc. Learned a lot along the way.

Anyway; I have my NodeMCU set up to read the Temp & Humidity through a DHT11 Module (DHT22 on the way) as well as the moisture modules. These 3 moisture modules are connected to the one NodeMCU analogue pin (A0) using relays switching each moisture sensor module on for 15s (allowing time for them to settle before reading) and preventing signals colliding on the A0 pin etc.
Sensor 1 is in air, sensor 2 is in a damp cloth, sensor 3 is fully immersed in water up to the black lacquer.
Now for the life of me, I can not get a sensible reading from the moisture modules. Sensor readings are as follows

Serial Output:

Connecting to Riverside 4G 2.4Ghz
15:15:22.302 -> [4913] Connected to WiFi
15:15:22.302 -> [4913] IP: xxx.168.0.xxx
15:15:22.337 -> [4913]
15:15:22.337 -> ___ __ __
15:15:22.372 -> / _ )/ /_ _____ / /__
15:15:22.406 -> / _ / / // / _ / '/
15:15:22.440 -> /
//_, /////_
15:15:22.474 -> /
__/ v0.6.1 on NodeMCU
15:15:22.509 ->
15:15:22.509 -> [4990] Connecting to blynk-cloud.com:80
15:15:22.544 -> [5154] Ready (ping: 76ms).
15:15:22.612 -> Connecting to
15:15:22.612 -> Riverside 4G 2.4Ghz
15:15:22.785 -> ........
15:15:25.063 -> WiFi connected
15:15:26.099 -> 49.00 %
15:15:26.203 -> 22.60 C
15:15:26.203 -> Reading Sensor 1
15:15:41.178 -> 430.00
15:15:42.201 -> Reading Sensor 2
15:15:57.172 -> 430.00
15:15:58.199 -> Reading Sensor 3
15:16:13.221 -> 429.00
15:16:13.221 -> ----------------------------------------

Using a multimeter to measure the voltages on the A0 pin I see 1.256v on each if just using the bare output from the module. Now I am aware that the A0 analogue input can only cope with 0 - 1.1V so I tried putting a voltage divider in which reduced the maximum voltage to just under 1v - but the reading still don't make sense - even adjusting the calculation used I get no discernible difference between air and fully immersed sensors. Measuring the voltage with the divider in position, I got Sensor 1 (Dry) 940mv, Sensor 2 (Damp) 942mv and Sensor 3 (Immersed) 936mv.

So does anyone have any pearls of wisdom they could share as to what I can do to make it so I can use the NodeMCU with 3 moisture sensors giving readings I can translate into sensible % figures? Should I look at using a voltage divider again but somehow set up or calculated differently? What about and ADC pumping the info into digital pins on the NodeMCU? Would I be better off looking at going back the Nano and pumping info into the Node MCU via SUART or similar? Or should I chuck it all in the bin and go to the pub? Oooh wait - Lockdown - that's not an option anymore!

I have included a Fritzing image of my approximate layout - again I apologise as I am no Rembrandt but hopefully it will give a general idea of what I've done.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

int sensorPin = A0;    // select the input pin for the potentiometer
int enable1 = 12;      // enable reading sensor A
int enable2 = 13;      // enable reading sensor B
int enable3 = 15;      // enable reading sensor C

float sensorValue1 = 0.000;  // variable to store the value coming from sensor A
float sensorValue2 = 0.00;  // variable to store the value coming from sensor B
float sensorValue3 = 0.0;  // variable to store the value coming from sensor C

char auth[] = "nothing to see here";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Riverside 4G 2.4Ghz";
char pass[] = "nothing to see here";

#define DHTPIN 2          // What digital pin we're connected to for T and H
#define LED D1            // Led in NodeMCU at pin GPIO16 (D0) for connection LED.

// DHT Type
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
WiFiClient client;


// Send it to Blynk
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {

    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Serial.print(h);
  Serial.println(" %");
  Blynk.virtualWrite(V6, t);
  Serial.print(t);
  Serial.println(" C");
}

void setup()
{
  // Debug console
  Serial.begin(9600); // default was 9600
  delay(10);
  Blynk.begin(auth, ssid, pass); // Temp & hyd
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  
  dht.begin();
  
  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);

  pinMode(enable1, OUTPUT); //Set D5 to OutPut to Switch on Sensor1
  pinMode(enable2, OUTPUT); //Set D6 to OutPut to Switch on Sensor2
  pinMode(enable3, OUTPUT); //Set D7 to OutPut to Switch on Sensor3
  
  pinMode(LED, OUTPUT); // set the digital pin as output for connection LED.
 
  Serial.println("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(LED, LOW);
    delay(150);
    Serial.print(".");
    digitalWrite(LED, HIGH);
    delay(150);
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  
  digitalWrite(LED, HIGH); // turn the LED on.
  delay(1000);         // wait for 1 second.

}
void sensordata()
{// read the value from sensor A:
  digitalWrite(enable1, HIGH); 
  Serial.println("Reading Sensor 1");
  delay(15000);
  //sensorValue1 = (((analogRead(sensorPin)/353) * 100 ));
  sensorValue1 = analogRead(sensorPin);
  Blynk.virtualWrite(V7, sensorValue1);
  Serial.println(sensorValue1);
  digitalWrite(enable1, LOW);
  delay(1000);

 // read the value from sensor B:
  digitalWrite(enable2, HIGH);
  Serial.println("Reading Sensor 2");
  delay(15000); 
  //sensorValue2 = (100 - ( (analogRead(sensorPin)/1023) * 100 ));
  //sensorValue2 = (((analogRead(sensorPin)/353) * 100 ));
  sensorValue2 = analogRead(sensorPin);
  Blynk.virtualWrite(V8, sensorValue2);
  Serial.println(sensorValue2);
  digitalWrite(enable2, LOW);
  delay(1000);

  // read the value from sensor C:
  digitalWrite(enable3, HIGH); 
  Serial.println("Reading Sensor 3");
  delay(15000);
  //sensorValue3 = (100 - ( (analogRead(sensorPin)/1023) * 100 ));
  //sensorValue3 = (((analogRead(sensorPin)/353) * 100 ));
  sensorValue3 = analogRead(sensorPin);
  Blynk.virtualWrite(V9, sensorValue3);
  Serial.println(sensorValue3);
  digitalWrite(enable3, LOW);
  Serial.println("----------------------------------------");
  delay(10000); // delay before starting next set of readings
}


void loop()
{
  Blynk.run(); // T&H
  timer.run(); // T&H
  sensordata();
}

Now I am aware that the A0 analogue input can only cope with 0 - 1.1V

There is a voltage divider on the ADC input that raises the full scale input to 3.3V (see schematic).


How to post an image.

I wonder about connecting all of the soil sensor outputs together. You are putting a voltage on unpowered outputs. Not usually a good thing as you may be back powering the unpowered devices through the output.

So I did a little more reading and found as suggested by GroundFungus that the input on A0 can indeed be up to 3.v and the info I read was probably for a bare ESP.

So I have tinkered a little more and am now running the Moisture Modules at 5v and seeing:
Sensor 1 1.63 to 1.72v - never really settles just keeps climbing slowly (Sensor in Air)
Sensor 2 1.733v - fairly steady (Sensor in damp cloth)
Sensor 3 1.725v - fairly steady (sensor immersed)

The serial readings are:
16:19:12.267 -> ----------------------------------------
16:19:22.319 -> 48.00 %
16:19:22.423 -> 23.00 C
16:19:22.423 -> Reading Sensor 1
16:19:37.409 -> 586.00
16:19:38.402 -> Reading Sensor 2
16:19:53.410 -> 588.00
16:19:54.418 -> Reading Sensor 3
16:20:09.417 -> 581.00

So whilst the readings have shifted up in value with the change from 3.3v to 5.0v they still don't make a lot of sense to me. I can't even devise a formula that mathematically brings the readings into line.

I'm open to any advice.

groundFungus:
I wonder about connecting all of the soil sensor outputs together. You are putting a voltage on unpowered outputs. Not usually a good thing as you may be back powering the unpowered devices through the output.

That is a very good point that I had not considered. I will try putting a diode in and see if that changes anything. CBA tonight so will look at it again tomorrow. Many thanks.

I think I get whats being asked.

I use an ESP32 set up for 12 bits, which gives a range of readings from 0 to 4096. From reading what you wrote if I set 4096 to represent 100% and 0 represents 0%, I can use the readings from the sensors to develop a scale. Dividing 4096 by 100, I get 1% to equal a reading of 40.96; the scale. And so on and so forth.

Here's what I think.

Those PCB modules that come with the soil probes are useless to you. They consume power and take up space for no benefit. You can replace each with a single resistor. If you were using the digital outputs of the PCB modules, then they would have a purpose in life. But if you want the analog outputs, so you can see a % level rather than a simple wet/dry indication, then they do nothing other than provide a single resistor, which you can easily do yourself.

Using 3 relays to switch power to the probes: why did you do that? Was it to power down the sensors to save battery power, or to reduce corrosion of the sensor probes? Or was it simply just to be able to connect 3 to the same analog pin? Whichever, relays were a bad choice. You can't just connect the 3 analog outputs from the sensors together, they will affect each others readings, even when not powered. Switching them off to avoid corrosion is a good idea, but not to save battery power, relays are notoriously wasteful of power. My advice would be to get an ads1115 board. This will provide you with 4 analog inputs and all take up only two digital pins (or take up zero pins, I'll explain later).

PaulRB:
Using 3 relays to switch power to the probes: why did you do that?

Hi PaulRB; The relays were to both reduce corrosion and allow the 3 connections to A0. I had an 8 relay module lying in my parts box and wasn't too worried about power at this point. I hadn't considered the voltage from the working sensor being affected by the others. In mitigation I did try an earlier version of the build with a single moisture module, and I dont think its readings were anything like those I got when using the Nano (which gave a results range from about 300 to 1020ish) - but that earlier build may have been running at 3.3v not that that should make a big difference.

Interesting you mention the ADC board as I ordered a couple off Amazon yesterday after reading a post about increasing the analogue inputs. They are ADS1115 ADC+PGA. Impatience got the better of me and I though that powering each sensor in turn would achieve what I wanted. I will still try a diode to see if that makes any difference as the ADS's wont be here till next week.

I don't suppose you have more detail on the resistor use instead of the PCB Module? I made a much longer probe myself to read deeper into the soil and it gave very similar results as the short "dissolvable" ones that came with the module. If I can use more of my own with a resistor then I'd prefer to do that.

Many thanks for your thoughts and advice.

Hi Mike. The best resistor value to use may need a little trial and error, especially with home made probes, but start with 10K. You don't really want to go below 1K or above 1M. Start by sticking your probe into dry soil, damp soil and saturated soil and measure the resistance between across the probe with your multimeter. The resistor value should be in the same ball park.

To cut power to the probes and prevent corrosion, just power the probes from a digital pin. The current should be so low that a single digital pin can power all 3 probes at once without breaking a sweat. The fixed resistor should be connected between the digital pin providing the power and the analog pin, and the probe between the analog pin and ground. If you do it the other way around, there's a risk of a short circuit of you accidentally drop the probe and it touches an earthed conductor.

More suggestions: might be worth getting one of those capacitive soil moisture sensors to experiment with alongside the resistive off-the-shelf and home-made probes. In theory the capacitive ones should be less prone to corrosion and give more consistent results in different soil compounds and temperatures. Let us know what you find.
capsoil.jpg

Another more expensive but robust option is the waterproof temperature/humidity sensors that contain an sht10/20 chip or similar. You can bury them further down and get soil temp as well has moisture level. The exact same model can be used for the air temp/humidity, and should last longer than the dht11/22 sensors, which are not known for their longevity in outdoor or greenhouse environments.


They are digital, so you won't need analog pins at all. But you would have the problem of them all having the same address on the i2c bus, requiring a bus multiplexer.
i2cmux.jpg

PS. I'm not endorsing any of the above sellers, just using them as example.

capsoil.jpg

i2cmux.jpg

PaulRB:
Here's what I think.

You can't just connect the 3 analog outputs from the sensors together, they will affect each others readings, even when not powered.

groundFungus:
I wonder about connecting all of the soil sensor outputs together. You are putting a voltage on unpowered outputs. Not usually a good thing as you may be back powering the unpowered devices through the output.

Guys you were 100% correct here. I did a quick check and disconnected the signal from all the Sensor Modules to A0 and manually re-made the connection as each relay powered up. The results are significantly different, and more to the point, I understand the results:

09:09:39.493 -> ----------------------------------------
09:09:49.533 -> 68.00 %
09:09:49.636 -> 21.60 C
09:09:49.636 -> Reading Sensor 1 Dry air
09:10:04.606 -> 1024.00
09:10:05.616 -> Reading Sensor 2 Damp Cloth
09:10:20.633 -> 601.00
09:10:21.632 -> Reading Sensor 3 Water
09:10:36.663 -> 882.00
09:10:36.663 -> ----------------------------------------

Now there seems to be something slightly amiss with number 2 or 3 as #2's reading are below that of 3 which it shouldn't be; I would expect 3 to be nearer 400 maybe. But I can calibrate it and adjust the formula to calculate the correct % .... or bin it and use my homemade sensor bypassing the module PCB as per PaulRB's suggestions - PaulRB - would you have a quick schematic for me to review? I think I get what you mean but I'd like to be sure.

All in all, thanks all for your help and suggestions. I value them all and enjoy the way this community helps to broaden the thought patterns of people like myself. Great stuff chaps. Cheers.

You could try this:

You would activate a probe by setting one of the digital pins to OUTPUT/LOW. The other 2 would be set to INPUT to avoid affecting the reading.

Once again, many thanks for the help. I will give that a go and see how it goes.

I have now removed the relays and set up 3 digital pins to take their place. I also added 3 diodes as mentioned previously and the readings are now back where I believe they should be and I have worked out a formula that gives a good result. The probe readings still wander around a bit and I assume that is mostly down to the poor quality probes. The results appear much more stabilised when I use my homemade stainless probe, so I will make some shorter ones (the first is 300mm as I wanted to test over a greater depth).

Once I am happy with all this I will probably start pulling it all apart again and seeing where I can make improvements for the sake of longevity and reliability. I will need to go to 5v for the probes due to the cable length I will need in the Greenhouse so that too may cause a bit of brain ache. I may also start looking at capacitive probes and trying to find a way of getting those deeper into the soil etc.

Again, many thanks for pointing me in the right direction.

M