Main Content

MCUVoltage

MCUVoltage measures the voltage supply (Vcc) of Arduino without extra components.

MCUVoltage measures the voltage supply (Vcc) of Arduino without extra components. Supported board includes Uno, Leonardo, Mega as well as the ATtiny3224/3226/3227. This library also supports oversampling and averaging. Hardware oversampling for the ATtiny3224/3226/3227 is also supported.

Testing of ATtiny3224 is done on the megaTinyCore by SpenceKonde.

While the Vcc is often assumed to be 5V for the Arduino, it is often not the case. Due to factors like having diode on the power rail, long USB cable, inaccurate bus voltage etc, Vcc can vary, usually between 4.5 to 5.25V.

Since analogRead() uses the Vcc as a reference by default, it may be useful to know the actual Vcc instead of making an assumption that it is 5V.

While the out-of-the-box measurement using this library may not be better than the 5V assumption, this library allows for calibration of the Vcc reading, and is able to achieve consistent results across various USB power supplies. Sadly, needs to be done on a board by board basis. An example, Calibrate_Bandgap is included to help with this calibration along with a digital multimeter.

This library is also useful for battery powered projects to read the current battery voltage, assuming it is hooked up directly to the Arduino.

While oversampling of the ADC is supported, this library calculates the result integrally with a precision of up to 1mV, thus excessive oversampling over 13 bits may not be useful.

I will attempt to explain how the ATmega328P ADC, bitmasking and oversampling works, mainly for my own reference.

Supported microcontroller units (MCUs):
- ATmega48/88/168/328 and their ‘P’, ‘A’, ‘PA’, ‘PB’ versions
- ATmega16u4/32u4
- ATmega640/1280/1281/2560/2561
- ATtiny3224/3226/3227

This also means the Arduino Uno, Nano, Leonardo, Micro and Mega are supported.

Since the ATmega48/88/168/328 has so many variants, I did not manage to check the datasheet on all of them, and made the assumption after looking through a few that they should be mostly similar.

The following boards are tested personally by me:

- Arduino Uno Compatible Board
- Arduino Pro Micro Compatible Board
- Arduino Leonardo Compatible Board
- Arduino Mega Pro Compatible Board
- My own ATtiny3224 Development Board

See also byte getDevice() on how boards and MCUs are detected.”

Link to article