Tech Note 143 - ESP32 - Voltage Reading (pre-calculated voltage divider) Examples

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 มิ.ย. 2024
  • How to measure voltages using the ESP32 ADC. Here I have pre-calculated a range of voltage dividers to measure voltages from 0 - 5v, 0 - 6v, 0 - 12v, 0 - 15v, 0 - 24v, 0 - 50v and 0 - 100v.
    Because the input impedance of the ESP32 loads the voltage divider, you have to compensate the readings accordingly and I explain how to do that, including pre-calculated factors so you don't have to work anything out!
    Example code here: github.com/G6EJD/ESP32-ADC-Vo...
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 47

  • @hughbreivik4653
    @hughbreivik4653 5 หลายเดือนก่อน +1

    Really good explanation of practical intracacies of impedance matching for reading ADC values. Thank You!! The info is still just as valid today as when the video was created! Please keep the videos coming!

  • @TYGAMatt
    @TYGAMatt 3 ปีที่แล้ว

    Hi Dave. Any plans on a tech note regarding using dual cores on the ESP32?
    Read a fair bit and watched a few vids but would be nice to get your easy to understand take on it.
    Sorry if this is off topic.

  • @TechnoEveryday
    @TechnoEveryday ปีที่แล้ว

    Interesting and useful

  • @EmbSysDev
    @EmbSysDev 3 ปีที่แล้ว

    Would it improve accuracy to feed the ADC with an opamp buffer, so that the input impedance of the ESP32 is >> than the output impedance of the opamp ?

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว +2

      Good question. Well, once the ESP32 input impedance has been established it does tend to be mostly resistive and constant for any particular device, this is why I’m using the term impedance which is usually a combination of (R+-jX) resistance and capacitance or inductance. So adding a buffer will reduce the load on a voltage divider, allowing the ratio to be more exact, but it won’t affect the readings once recalculated for the input impedance load.
      What you’ve suggested would be a much better add-on when using a DAC output as the ESP32 output impedance is relatively high, so an op-amp would enable a much better output to be obtained that is less susceptible to loading.

  • @toastrecon
    @toastrecon 3 ปีที่แล้ว +2

    Any idea how "stable" the calibration value would be? I mean, if you followed this, and then went on to use your ESP32 for days and weeks (and various power cycles) - would that calibration value still work?

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว +2

      David Clawson, I have not seen any variation in the ADC readings other than a small amount of (expected) noise, but this could be eliminated (statistically) by over sampling, such as a simple take 5 readings, sum them, then divide by 5, this has the effect of smoothing the results. Other variations will be due to the effect of temperature on the resistor values, but as they should both rise or fall in temperature together, unless physically separated, then there will be an element of common mode rejection, meaning the ratio of division will remain constant. In practice I have not seen any appreciable variations.

  • @RodrigoeBeta
    @RodrigoeBeta ปีที่แล้ว

    I'm currently using a load - 40k - adc - 1k - gnd and I'm getting a ridiculous amount of noise. Adding a bypass 100nf electrolytic capacitor very close to ADC input doesn't reduce any of the noise.
    Feeding 2v I get readings that go from 0 to 4095, it is impossible to use. Using an ADS1015 or ADS1115 for each project turns things very expensive. I'm new to electronics, but as I understand, if I follow your resistor values, I'll be able to get rid of most of the noise?
    One of the applications is in an ebike throttle so I'm afraid to use a capacitor and get a laggy response.

    • @G6EJD
      @G6EJD  ปีที่แล้ว

      The time constant for the filter is too short, currently about 1mS, so it won’t filter much. Try increasing both values by a factor of 10, so 400K and 10K (nearest perfected values for the 400K). I suspect the problem with an eScooter control system is high switching currents do give rise to high ambient electrical noise, so you may have to put an RFI suppression chime in series with the ADC measurement circuit to remove any high frequency noise, just about any RFI coil/choke will do, but it’s resistance needs to be low and this should be the case with a good quality choke with a low Q.

  • @alfonso4998
    @alfonso4998 2 ปีที่แล้ว

    Very good video. Could you answer me a question? If we wanted to use the function you explain in the TN069 video to improve accuracy, would we just have to substitute the line "double reading = analogRead(pin);" for "double reading = (float)analogRead(ADC_pin)/4095*calibration_factor"? That is, the polynomial can return a more exact value as long as the voltage read does not exceed 3.3V, right? Thanks.

    • @G6EJD
      @G6EJD  2 ปีที่แล้ว

      Correct.

    • @alfonso4998
      @alfonso4998 2 ปีที่แล้ว

      @@G6EJD Another question if you don't mind. Can't we select R1 (470k) with a lower value? I say this because then R2 would be smaller too and I think it would not affect the ADC input resistance measurement as much. Also, if we want to put the capacitor in to smooth out the noise, couldn't the readings be faster as the time constant is smaller?

    • @G6EJD
      @G6EJD  2 ปีที่แล้ว +1

      Yes all of that, the values I chose were to measure the ESP32 running on a battery and presenting the lowest battery load. Ideally; if power consumption isn’t an issue, drop the values down but maintains the same ratio and add a larger smoothing capacitor, noting that as the capacitor increases so does the RC time constant and the circuit takes longer to stabilise after change, which could lead to inaccurate readings until the capacitor has reached a steady state charge. The ESP32 input impedance is quite high, it varies from device to device, but about 300K ohms.

    • @alfonso4998
      @alfonso4998 2 ปีที่แล้ว

      @@G6EJD If it is possible to put smaller resistors, do we increase the capacitance because a larger capacitor removes noise better? We could increase its value by the same amount as we decrease the value of R2, right?

  • @tobo5757
    @tobo5757 3 ปีที่แล้ว

    Thanks Dave, iam going test it. Happy to see this tech note so soon after the previous one. May I translate this that’s going beter and beter with you. Maybe a bit wrong english but I think you know what I mean.

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว +1

      Yes, feeling a lot better this week. I'm trying to get to a count 145, still got lots of ideas though.

    • @tobo5757
      @tobo5757 3 ปีที่แล้ว

      @@G6EJD good to hear.!

  • @RandomTorok
    @RandomTorok 4 หลายเดือนก่อน

    I'm monitoring a solar panel to gain information regarding it's output. I have a voltage sensor that has a 30K resistor and a 7.5K resistor. At the moment I'm using the constant for the 0-15v range and it seems to be close to the reading from my multimeter. Of course the resistors are not actually 30K and 7.5. I wonder if I would get more accurate measurements if I used the actual measured resistances?

    • @G6EJD
      @G6EJD  4 หลายเดือนก่อน

      You could, but because it’s all linear, measure the voltage and then check the ESP32 readings and adjust the value with a simple offset like voltage * 1.01 or * 0.99 obviously varying the constant value up or down as required, then it will be calibrated.

  • @TheNCGardener
    @TheNCGardener 2 ปีที่แล้ว

    What would be the downfall of using the map function to map the readings to match the maximum of the voltage range?
    If you know the calculated voltage divider, it seems this would be the most accurate.

    • @G6EJD
      @G6EJD  2 ปีที่แล้ว

      You could use a map function, but as it’s integer only, you’d need to create a new function using floating point, probably double types too.

  • @paulpkae
    @paulpkae ปีที่แล้ว +1

    Curious why you stipulate the capacitor should be rated at 200V or more.

    • @G6EJD
      @G6EJD  ปีที่แล้ว +2

      Well yes strictly not necessary if you rely on the integrity of the resistors and for low voltage measurement absolutely unnecessary, but when your recommending a solution to a wide range of skills it’s better to err on the safer side as applying a high voltage to a capacitor not rated for the expected maximum generally has catastrophic results that is best avoided!

    • @paulpkae
      @paulpkae ปีที่แล้ว

      @@G6EJD OK thanks, I was guessing that was your reasoning.

  • @0124akash
    @0124akash 2 ปีที่แล้ว +1

    Sir can I use this formulation for nodemcu esp8266 ?

    • @G6EJD
      @G6EJD  2 ปีที่แล้ว +1

      You can use the same principles but the maximum input voltage of the ESP8266 ADC input is 1v so all the ratios need to be adjusted, but I give the formulas to use, really the hardest job is choosing the nearest preferred values that resistors are sold as. The ADC input current becomes a limitation if you use the higher value resistors up in the 750K or above values.

  • @s.husain6125
    @s.husain6125 3 ปีที่แล้ว

    Thank you sir so form this example we can monitor battery in better accuracy??

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      When you say accuracy,, what do you mean? You can get 1% linearity and repeatable readings. If you use Vbat---470K---ADC---470K---Gnd and use my polynomial in the links of TN069, it's a very 'accurate' result, as good as my Fluke DVM for readings. You can have as much resolution as you like, but accuracy depends the calibration you apply through the constant adjustment I refer to.

    • @s.husain6125
      @s.husain6125 3 ปีที่แล้ว

      @@G6EJD thank you sir for nice guidance

  • @invatatenis
    @invatatenis 3 ปีที่แล้ว

    Hi David. I am trying to measure a 12.8v battery. I used the 0-15V resistors (470k and 100k). If I use the code from the link, I obtain ~140 on the arduino console. The voltage that gets to pin 36 is ~ 2.2v and the analog read is ~ 1700.0. Could you help in identify what am I doing wrong? Thanks!

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      Hi, A reading of 1700 suggests 1700/4095 * 18.5 = 7.7v. Did you set the constant value correctly for 0-15v input? it should be 18.5
      What sort of board are you using? Do you have the resistors around the right way?

    • @invatatenis
      @invatatenis 3 ปีที่แล้ว

      @@G6EJD I set the constant to 18.5. I think the github code has a problem multiplying twice with the constant though. The board is wemos lolin32. I believe the resistors are wired ok. I managed to get to a good voltage value by changing board pin to 35, changing constant to 20.3 and multiplying just once with the constant.

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      Thanks I don’t know how the double constant entry happened, I’ve removed it now. I suspect your board (many do) has an on-board voltage divider which would skew your readings.

    • @invatatenis
      @invatatenis 3 ปีที่แล้ว +1

      @@G6EJD Thanks, David. If I may, I have one more question if possible: I would like to use the 12v source for which I measure the voltage to also power the Lolin32 board. How do you recommend to do it and what would be the connection points (pins / gnd)?

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      @@invatatenis OK, you will need a 12v/5v voltage regulator, something like: www.aliexpress.com/item/32525895503.html?spm=a2g0o.productlist.0.0.2dda5b10yO0S9j&algo_pvid=7b2fa296-33b2-4c11-8a3b-50db94265578&algo_expid=7b2fa296-33b2-4c11-8a3b-50db94265578-18&btsid=2100bb4916064924717273992e9e6b&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_
      and connect 12v to the input and a gnd, then the 5v output goes to the USB pin with a gnd. The USB pin is effectively 5v, but don't connect a PSU and USB at the same time.

  • @ErikThiart
    @ErikThiart ปีที่แล้ว

    I want to measure my solar panel string ( it's 6 poly panels in series so roughly 220V DC ) good idea to use ESP ?

    • @G6EJD
      @G6EJD  ปีที่แล้ว +1

      Not really, high voltage DC is a specialist subject (any DC voltage above 30v is internationally agreed to be lethal) as the techniques and safety requirements are beyond anything that most people have ever done into contact with. If touched your muscles go into spasm and hold in that state, you can’t let go, arcing of the voltage to ground is a real risk unless the correct construction techniques are used. Best to use the usual inverter measurements.

    • @ErikThiart
      @ErikThiart ปีที่แล้ว +1

      @@G6EJD your probably going to like my follow up question even less then :P
      Can I use a AC breaker... My thinking was to put in a busbar on the 4-Pole 63A AC breaker to mitigate the load, it's a 220V DC at 5A solar string.
      The reason for this shenanigans: I live in sunny South Africa and as might or might not know, we no longer have electricity our corrupt government broke Eskom and now the country only has power for 4 hours at a time permanently.
      Because of this extreme corruption cost of items has skyrocketed. I can get large AC breakers for around R50 however a 500VDC DC breaker costs >R1k because there's zero supply left in South Africa, no more batteries and no more solar panels available in SA as everyone with the means bought it all up the last few months after Eskom announced it can no longer supply the country.
      So I now have to resort to... Less than ideal fuckery to get my solar system working.
      And figured your the smartest person I know so will check by you first.
      PS: your videos are absolutely incredible, the knowledge contained on this youtube channel can be used in universities

    • @G6EJD
      @G6EJD  ปีที่แล้ว +1

      @@ErikThiart the issue with DC circuits is the arc that’s drawn when a circuit is broken, so DC breakers, say for a House Battery feeding an inverter have to operate at very high speed and use contact snubbers to extinguish an arc, this is why they have a higher cost. However, with 220v DC at a maximum of 5A (probably 6-panels peaking at around 36v each) that is a relatively small current and an overrated AC breaker should be sufficient. Generally for the input of an inverter it’s not necessary to provide isolation other than for maintenance. You could use an ESP32 as a voltage monitor, it just needs very careful choice of wiring and well insulated connectors and resistors that are capable of working at high voltage including their power rating at say 0.5W rating or better. All I’m saying is be very careful and employ systematic safety and gloves when working on the connections, or do the work after sunset. Good luck with your supply problems, sounds chaotic.

    • @ErikThiart
      @ErikThiart ปีที่แล้ว

      @@G6EJD "Sounds chaotic" that's the slogan for South Africa :P.
      Thank you for the reply, conceptionally I understand what you are saying regarding the arc.

  • @2ATactical_CEO
    @2ATactical_CEO ปีที่แล้ว

    This is a great video, however, it is better to use a high impedance voltage follower circuit to buffer your circuit from the resistor divider. If you place a resistor divider across a battery to measure system voltage it will act as a parasitic load across your system. A simple switch or follower circuit will prevent this.

    • @G6EJD
      @G6EJD  ปีที่แล้ว

      Yes, but these were more of a DVM example than constant voltage monitoring function.

  • @kokosikzhd
    @kokosikzhd ปีที่แล้ว

    Hi David
    Can you share formula how to calculate constant?

    • @G6EJD
      @G6EJD  ปีที่แล้ว

      A simple voltage divider so vadc = Vin x R2 / (R1 + R2) where vadc = 3.3v and R2 is the resistor between ADC input pin and Ground and R1 is in series with the input voltage and the ADC input pin. You have to be cognisant of the ADC input current so you can’t use really high values for R1

    • @kokosikzhd
      @kokosikzhd ปีที่แล้ว

      @@G6EJD I understand that, it was in previous videos. Currently I'm trying to measure up to 91V(R1 221kOm R2 7500 Om) and trying to find constant.

    • @G6EJD
      @G6EJD  ปีที่แล้ว

      @@kokosikzhd the problem is every ESP32 has a different ADC input impedance and therefore input current, but I used an average of 160K let’s call it Rp so effectively the formula becomes R2//Rp/(R1 + R2//Rp)
      You can never get a repeatable result across many ESP32 as they are all different. The s seer is to make R2 as small as possible whilst enabling the same input current, so in your case I suggest you drop the R2 value down from 7.5K to say 1K making the parallel input impedance very much less dominant and recalculate R1 accordingly higher, you can approach 900K for R1