Using a YL-38 and NodeMCU v1.0 I am trying to achieve what the first link is doing, take a soil moisture reading. I've done this on an arduino, but the conversion from an arduino nano to a nodemcu is proving difficult.

To my understanding, the nodemcu A0 pin operates from 0v-3.3v but the YL-38 analog output operates from 0-4.1v. So first off, is my understanding correct?

I struggle with hardware, so looking around I found these resources:

How to set up a simple level shifter: this setup seemed pretty much like what i wanted, but the 5v input was off

This thread was verbatim what I wanted but I am terrible with circuit math, so I couldn't solve or understand the provided formulas.

Attempt 0: Initially had sensor analog out connected to a0 on nodemcu. Saturated or dry the mcu read 1023. Thought this must be because the signal out is above the A0 mapping range.

Attempt 1: I figured I'd try out the level shifter. Wired it all up as indicated. Now my readings were 760 saturated and 765 dry.

``````runHygrometer(index);
delay(2000); //allow hygrometer to wake up
``````

As shown, my code does no scaling, It's a direct read (DEBUG is true here). So what gives? When using an arduino the range of readings was typically from 20 dry to 180 saturated (DEBUG false, so raw 1003 dry and 843 saturated)

To my understanding, the NodeMCU A0 pin operates from 0v-3.3v

Incorrect. The ESP8266's ADC can only measure between 0V and 1V.

You need to reduce your output voltage down to that range. How you do that very much depends on the output impedance of the YL-38.

The LM393 that the YL-38 is based around looks to have a maximum output current of 16mA. That should be enough to happily drive a simple voltage divider. A 4.7kΩ and 1kΩ divider would give you 0 to 0.877v out from a 0-5V input.

simulate this circuit – Schematic created using CircuitLab

An alternative would be to attach an external ADC running at 5V to the NodeMCU. This would give you multiple channels of analogue input with a 0-5V range. I use the MCP3208 running over SPI (I actually bit-bang it on the ESP8266 rather than using SPI, since I haven't yet got SPI to work properly with it). Ideally, you want a proper logic level translator for the SCK and MOSI signals to boost them to 5V. It all gets a little complex, though.

It has come to light that the NodeMCU includes a voltage divider already. For some reason they chose to use massive resistors, which give far too high an impedance for any reasonable ADC input to accurately work with. I would suggest, if you can, removing those resistors from the board and replacing them with 4.7k and 1k resistors to form your own voltage divider. Otherwise you're going to have to use a pair of op-amps in inverting mode, one with a fractional gain, and one with unity gain (the second is to "undo" the inverting of the first one) to reduce the signal to 0-3.3V.

TBH I'd be inclined to use an external ADC anyway for this.

• I thought I read that A0 has a voltage divider and feeds that signal into the ESP8266 ADC? I'm referring to the 3rd to last page on NodeMCU doc listed above. randomnerdtutorials.com/… "Currently, TOUT (Pin6) has a 10-bit precision and its input voltage range is 0 to 3.3V when TOUT is connected to an external circuit." – Doug Clark Jul 31 '18 at 13:42
• Ugh. So it does. That's nasty. You want to remove those resistors and replace them with 4.7K and 1K. The resistors used are at least 10x too big for any normal ADC input. Far too high an impedance. You can't use another voltage divider along side it, it just won't work. Either that or you'd have to use an op-amp with a sub-unity gain (actually you'd need a pair of inverting op-amps, one with a fractional gain and one with unity gain) to buffer your signal with a low impedance output. Or use an external ADC... – Majenko Jul 31 '18 at 13:46
• So i should look into using an external ADC to convert 5v to 3.3v signals? Would that be the simplest solution? – Doug Clark Jul 31 '18 at 15:27
• It's the route I'd probably take, yes. In fact, right in front of me I have a D1 Mini Pro with an MCP3208 attached to it to do some current and voltage measurements... – Majenko Jul 31 '18 at 15:28
• I'm really short on IO pins, so im not sure if the MCP3002 will work for me. But can i skip ADC EX and its voltage divider and use your 4.7k/1k external ADC straight to the ESP8266's tout pin (which i think is labled just ADC in the doc)? – Doug Clark Jul 31 '18 at 16:05