# Voltage calibration

**URL:** <https://forum.arduino.cc/t/voltage-calibration/1260802>\
**Category:** Programming\
**Created:** [May 16, 2024, 5:08pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802 "2024-05-16T17:08:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 16, 2024, 5:08pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/1 "2024-05-16T17:08:18Z")

</div>

Hello

I have a voltage divider dropping a voltage range of 0-15v down to a level an ESP32 can read.  
The actual voltage at 15v is about 2.63v

So, I sample the analogue voltage and do a conversion. I decide to add a calibration value (float) so that I could dial-in the displayed screen voltage with the actual voltage being read.

But... if I calibrate the voltage to be displayed at say 10v, then the value drifts when either side of that calibration point.

```arduino
    currentvoltage[channel] = analogRead(analoguePINS[channel]); // Sample the voltage
    currentvoltage[channel] = currentvoltage[channel] * calibration; // Float voltage calibration value (Below 1 lowers the voltage, above 1 increases it) 
    adjvoltage[channel] = adjvoltage[channel] + mapfloat(currentvoltage[channel], 0, 4095, 0, 15); // Convert the value to 0-15v

```

So... clearly I am doing this all wrong!  
Am I applying the calibration value at the wrong point?

I am probably doing it all incorrectly to be honest.

The value adjvoltage is actually sampled 250 times after the above code then averaged out to give a steadier result.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [May 16, 2024, 5:55pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/2 "2024-05-16T17:55:00Z")

</div>

We don't know which variables are 'int' or 'unsigned long' or 'float' or 'double'.  
There is a website for that: [https://www.snippets-r-us.com/](https://www.snippets-r-us.com/)

Can you show a small sketch that shows the problem ?

The Arduino Uno with its 10-bit ADC is very good and averaging/oversampling will increase the resolution (not the overall accuracy). The ESP8266 and ESP32 are not that accurate. You don't have try that hard to increase measured voltage, because the ADC is not very good.

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [May 16, 2024, 7:01pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/3 "2024-05-16T19:01:48Z")

</div>

Some ESP32 boards already have a potential divider on the PCB.

If you connect an external potential divider to the analogue input, then there is an interaction between the two potential dividers.

Have a look at the schematic of your ESP32 board to see whether that is the case.  
(or post a link to it here.)

It is easier just use a single resistor in series with the upper resistor on the board potential divider, than to do the mathematics required for the two potential dividers.

---

<div class="post-metadata">

**Author:** ![DVDdoug](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dvddoug/32/671533_2.png) [@DVDdoug](https://forum.arduino.cc/u/DVDdoug)\
**Post date:** [May 16, 2024, 7:59pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/4 "2024-05-16T19:59:41Z")

</div>

> [@baffled2023](#):
>
> The actual voltage at 15v is about 2.63v

Take your (average) raw reading at 15V, then use that to convert to voltage:  
_Current Voltage = Current Raw Reading x (15 / Raw Reading at 15V)_

...A standard straight-line calibration also includes an offset correction which is added or subtracted.

Generally the offset is measured at (or near) zero. Feed-in 0V (i.e. ground the input) and then add or subtract (if necessary) to correct the actual reading to zero. Now re-measure at 15V (or at or near the maximum, or the "most expected" reading). Add or subtract the offset from the reading at 15V to get a "corrected raw reading" and re-calculate a new slope so it's still accurate at 15V (as well as at zero).

The offset is done first because it affects all readings equally. The slope is a multiplication factor it doesn't change the (already corrected) reading at zero.

---

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 16, 2024, 8:07pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/5 "2024-05-16T20:07:25Z")

</div>

Thank you all for the suggestions.  
I will have another look at this then and see if i can dial it in better using your ideas

---

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 17, 2024, 12:53pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/6 "2024-05-17T12:53:18Z")

</div>

This is driving me insane....

So, everything is a float.

At 15v reads 3440 on the analogue pin. So I try this to test the theory.....

float temp\_adjvoltage = ((3440/100) \*(1500/3440));  
Serial.println(temp\_adjvoltage);

Why does it serial print 12? The actual answer to that math is 15?

I divided the analogue voltage by 100 (and therefore the 15 is x100) because it would not divide the following correctly.

float temp\_adjvoltage = Measured\_analogue\_value x (15/The value at 15v)

It didn't like the 15/The value at 15v. This always returned zero.

---

<div class="post-metadata">

**Author:** ![rsmls](https://avatars.discourse-cdn.com/v4/letter/r/5daacb/32.png) [@rsmls](https://forum.arduino.cc/u/rsmls)\
**Post date:** [May 17, 2024, 12:59pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/7 "2024-05-17T12:59:36Z")

</div>

> [@baffled2023](#):
>
> This is driving me insane....

And it will continue to do so, even/especially as you get closer:

> **[ESP32 Analog Input with Arduino IDE | Random Nerd Tutorials](https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/)**
>
> Learn how to read ESP32 Analog Inputs with Arduino IDE using analogRead function. Analog reading is useful to read values from potentiometers, analog sensors, etc

The ESP32 ADC is pretty non-linear. It's good enough for very rough approximations; not much more unless you're going to calibrate it throughout its range with something like a lookup table.

> [@baffled2023](#):
>
> float temp\_adjvoltage = ((3440/100) \*(1500/3440));

Try  
`float temp_adjvoltage = ((3440.0/100.0) *(1500.0/3440.0));`  
Keep in mind that C++ will assume integers unless specified otherwise. If you want floating point math - specify otherwise!

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [May 17, 2024, 1:01pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/8 "2024-05-17T13:01:54Z")

</div>

You need to use analogReadMilliVolts(uint8\_t pin);

and read the documentation here:

[https://docs.espressif.com/projects/arduino-esp32/en/latest/api/adc.html](https://docs.espressif.com/projects/arduino-esp32/en/latest/api/adc.html)

---

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 17, 2024, 1:59pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/9 "2024-05-17T13:59:25Z")

</div>

Thanks

The ' `float temp_adjvoltage = ((3440.0/100.0) *(1500.0/3440.0));`' fixed that issue.

Stupid of me to even remotely think that stating it was a float would mean it would treat it as a float 🙄

Anyway... after all this, its no more accurate than the original way I was doing it.  
Now it's accurate at 15v, and drifts off all over the place below that.

Yes, I could attempt the 'analogReadMilliVolts(uint8\_t pin)' approach, but I am kinda over these ESP32's now.  
$2 LED voltmeter off AliExpress it is

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [May 17, 2024, 2:35pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/10 "2024-05-17T14:35:43Z")

</div>

You could just get a moduke like this

> **[SparkFun I2C DAC Breakout - MCP4725 - BOB-12918 - SparkFun Electronics](https://www.sparkfun.com/products/12918)**
>
> You've always wanted to output analog voltages from a microcontroller, the MCP4725 is the DAC that will let you do it! The MCP4725 is an I2C controlled Dig

Adafruit have another that is four channels and not much different in price.

  

> [@baffled2023](#):
>
> Stupid of me to even remotely think

No. Ignorant not stupid! All that maths stuff is just something you have to know, the left hand side being a float does not alter the manner in which the right hand side does its calculations.

You now know one way is to have a 42.0 float number k there, this gets seen and floating point is used.

There are similar around integers with respect to their size and whether they are signed or not.

It's a good idea to check the results of calculations, especially when you see they aren't producing the same results you get with your calculator or spreadsheet or by hand.

a7

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [May 17, 2024, 3:34pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/11 "2024-05-17T15:34:19Z")

</div>

Hi, @baffled2023

Do you understand that the ESP32 ADC is non linear at its limits.  
So using a linear correction function will not work.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/1/3/7/137b1c3f5be9cbe6d153ccf1c379025e06e07bed.jpeg)

Do you understand this chart?

Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 17, 2024, 3:42pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/12 "2024-05-17T15:42:34Z")

</div>

Yes, I did know it would drift at either end of the scale, but not to this degree.  
My only other option is some kind of lookup-chart/array maybe.

Trouble is, I am reading to 2 decimal places. That is a LOT of lookup values to store.

Using 'uint32\_t analogReadMilliVolts(uint8\_t pin)' didn't make it any more accurate.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [May 17, 2024, 4:00pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/13 "2024-05-17T16:00:15Z")

</div>

Hi, @baffled2023

> [@baffled2023](#):
>
> $2 LED voltmeter off AliExpress it is

Is that what you are using to calibrate your readings?  
One like this;

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/8/b/b/8bbd31743eed4596bb209c561504a247867a8cc7.jpeg)or ![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/3/4/534309f5974c5ddc94649f82024511e774e4ff16.jpeg)

Thanks.. Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [May 17, 2024, 4:13pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/14 "2024-05-17T16:13:16Z")

</div>

> [@baffled2023](#):
>
> Using 'uint32\_t analogReadMilliVolts(uint8\_t pin)' didn't make it any more accurate.

Then that's the end of the road for the ESP32 ADC. You need an external ADC.  
For 3 digits a 12bit ADC will be more than enough

---

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 17, 2024, 4:16pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/15 "2024-05-17T16:16:47Z")

</div>

No. Using my bench multimeter and the voltmeter on my Bench PSU (they seem to show the same reading).

I think maybe the easiest is to go back to my original simple AnalogueRead routine and add a follow on routine that adjusts the value via a set of rules to get it nearer the required result.

---

<div class="post-metadata">

**Author:** ![rsmls](https://avatars.discourse-cdn.com/v4/letter/r/5daacb/32.png) [@rsmls](https://forum.arduino.cc/u/rsmls)\
**Post date:** [May 17, 2024, 4:20pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/16 "2024-05-17T16:20:38Z")

</div>

> [@baffled2023](#):
>
> add a follow on routine that adjusts the value via a set of rules to get it nearer the required result.

That could work; there's a pretty long linear region that you can exploit.  
I'd recommend chopping off both the toe and the shoulder of the range so you only end up using the linear midsection:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/1/c/51c62540df6145845a29f60fc484689b8e633e2e.png)

---

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 17, 2024, 4:32pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/17 "2024-05-17T16:32:18Z")

</div>

I have a calibration routine I can activate with a certain button press.

I am just trying a routine where 'calibration' steps up from 1-15v, one volt at a time upon you pressing a button, and you press a store button to store that raw analogue to the preferences.h storage facility on an ESP32.

Might work/help

---

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 17, 2024, 5:12pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/18 "2024-05-17T17:12:11Z")

</div>

So, set up the calibration routine to request you set up the input voltage in 1v steps.  
Press the button, it stores the value.

Its pretty linear over 16 samples 1-16v

![scatter-plot](https://europe1.discourse-cdn.com/arduino/original/4X/2/c/e/2cedafb0a908014a7f84e595b5f849750acb94bb.png)

So, makes me think I am just simply messing up the math

Just for reference, this is a small tester to measure voltage on some ribbon cables. The actual voltages measured are usually 0-12v, but I have left some overhead (you can actual throw 20v down the ADC pin before tears).  
That also eliminates the ADC anomalies at the top end of the range.

The SD card is the speech (it can say the voltage etc)

 ![20240517_172910](https://europe1.discourse-cdn.com/arduino/original/4X/3/0/d/30d5309b97b346a855f0ce0a254ea1b6867ed124.jpeg)

---

<div class="post-metadata">

**Author:** ![baffled2023](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/baffled2023/32/1080823_2.png) [@baffled2023](https://forum.arduino.cc/u/baffled2023)\
**Post date:** [May 17, 2024, 8:10pm UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/19 "2024-05-17T20:10:14Z")

</div>

Well...... clearly beyond me 🙄

I sample the analogue voltage 250 times and average it out... that works fine and I get a reasonably stable variable.

It returns 0 at 0v and around 3474 at 15v.

So... I throw that into a map....

final\_adjvoltage = mapfloat(adjvoltage[0], 0, 3474, 0, 15);

This map works off the following routine to obtain the float values:

```arduino
//--------------------- Voltage mapping --------------------------

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) // Conversion to get float values for the voltage
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

```

At 15v input, I get 15.00v on the display.  
Turn that voltage down, and it drifts out by nearly 1.8v at 7.5v.

I would expect a little drift, but that is excessive. I know it's not a linear curve, but I sampled all the voltages from 1v to 16v (in 1v steps) and they were nearly all around 215 apart and far more linear than I expected.

Looks like another project for the fail bin!

---

<div class="post-metadata">

**Author:** ![bluejets](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bluejets/32/1013044_2.png) [@bluejets](https://forum.arduino.cc/u/bluejets)\
**Post date:** [May 18, 2024, 12:38am UTC](https://forum.arduino.cc/t/voltage-calibration/1260802/20 "2024-05-18T00:38:25Z")

</div>

Map uses int values.

[Next page](https://forum.arduino.cc/t/voltage-calibration/1260802.md?page=2)
