voltage reference cal.

what would be the best way to read the voltage reference voltage in code to update the calculation for sensors. (like a calibration)

I have noticed the ref. changes a lot, depending on the power input level and type.

for example
via USB is around 4.1 volts
via V In is around 4.9 volts

to calc. using: float volts = analogRead(0) * 4.11 / 1023;

right now I need to check with a multi meter and setup each time.
I know I can use a hardware ref. but i am looking for a software solution for now.

doing it this way gives an accuracy of 0.03-0.05v which is good enough


Two ways to solve that problem.

  1. Change Aref from default to one of the internal reference voltages (1.1volt or 2.56volt).
    And drop the “voltage to measure” with a voltage divider to that range.
    This is the easiest way, and only needs one line of code in setup().

  2. Measure VCC internally, and use that value in your code.
    Google “Arduino secret voltmeter” for instructions.

Note that only voltages, or sensors with a voltage output need the above.
Many sensors are ratiometric (pot, thermistor, ACS712 etc.), and give worse results with a stable reference.