Esp32 adc in micropython

analog = ADC(Pin(32), atten=ADC.ATTN_11DB)

Is this command still required with a new Esp32-c6? I want to read a sensor that outputs up to 5 volts. I have made a voltage divider so it outputs 0 to 3.3 v.

Yes, the voltage divider is the right approach. Since your ESP32-C6 ADC input is limited, bringing the sensor output down to 0–3.3 V is important. I’d still set the ADC attenuation if your MicroPython build supports it, and use read_uv() for a more accurate voltage reading.

1 Like

Hi PCB, Steve
Yes, voltage dividers are handy little networks But there are a few shortcomings you should be aware of.

If you are using larger value resistors to keep the source current to a minimum the ADC input resistance needs to be VERY HIGH. In most cases this should be the case but not always. The reason being is that this resistance is effectively in parallel with the low side divider resistor and will influence the calculated output. This input R could vary so you cannot compensate for it.
The usual solution is to make the divider resistance low enough so the input R is so high in comparison it can be ignored in a practical sense. Usually about 10kΩ total resistance is adequate.

Another solution is to interface a rail to rail OpAmp configured as a unity gain voltage follower to present a high R to the voltage divider and a lowR to the ADC input. This has the problem of not going quite to 0V. If excursion to 0V is required the OpAmp has to have the negative supply connection taken to a negative voltage source. Not exactly an ideal situation and can get messy.

On the other side of the divider is the sensor SOURCE RESISTANCE. If the total divider resistance is too small this source resistance will influence the divider itself. It is effectively in series with the total divider R and will form a voltage divider with the total divider R so you will be feeding your “voltage divider” with another “voltage divider” made up of the total “voltage divider” R and the source R.

Another thing that will influence total divider R value is the current capability of your sensor output which must be kept below a Max value. This can only be done with total divider value R.

If the source R is pretty low and the ADC input R is very high they can in practice be pretty much ignored. But there are situations where this will not be the case and the above must be considered.

I realise this sounds very confusing and it is. But it is something that is very real and should be kept in mind as all this can influence expected results and explain what appears to be incorrect measurements.

Note that in no way am I condemning the use of voltage dividers. Just point out that sometimes selecting the R values needs looking at a bit more closely than just selecting the highest or lowest available.

I personally think that the use of a 3.3V operating system is making the use of things like resistive dividers more difficult. There is not a lot of head room. 5V has not got a lot either but has that bit more. You only have a few tolerances going the wrong way and most of your 3.3V could be gone in no time.
Cheers Bob

PS: Another thing to keep in mind is when using high value resistors is the 10MΩ input resistance of any DMM measuring instrument itself. This is effectively in parallel with the DUT and if the DUT (divider resistor ??) is high enough the reading error will be significant This in fact will be the ACTUAL voltage present WHILE THE DMM IS CONNECTED but when disconnected and normal operation restored will be entirely different.

2 Likes