Tag Archives: arduino voltmeter

Arduino Voltmeter – 2 (more precise)

On some Arduino boards there is a Pin labelled as AREF or Analog Reference. This pin is very useful in cases where a stable and precise voltage reference is needed for the ADC. Practically the supplied voltage to the board can vary a bit, specially it can fall below the rated voltage depending on the load to the board.

By default Arduino uses the voltage of the board, that is 5v or 3.3v (depending on the board). Using the function analogReference() this can be changed. The function accepts one parameter called type. If the type mentioned is DEFAULT then it will use the voltage of the board. INTERNAL type will use a built-in reference equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8. And EXTERNAL will use the voltage supplied to the AREF pin. 

(More about analogReference can be read at https://www.arduino.cc/en/Reference/AnalogReference)

Please note the voltage at AREF can 0-5 volts only and the input voltage at the I/O pin (here A0) cannot exceed that of the AREF pin.

The simple voltmeter described in a previous article has been modified here. It is now supplied with a precise reference voltage at the AREF pin.

Volt Meter Precise

The 5K resistance is not a necessity, it has been added for safety (switching to the INTERNAL while external power is given to AREF can damage the board with that resistance). If the resistance is used then the voltage at AREF will not be exact as supplied. That is because internally there is a 32k resistor connected to the AREF pin.

So in above case the voltage that the AREF actually getting is 3.3*32/(32+5) = 2.8540 (the actual will vary due to tolerance of components, so for cases where high precision is needed, the values of the resistors will have to be measured and also the voltage of the zener, and the voltage in the code nees to be changed accordingly).

#define READINGS 5

int sensorPin = A0;
short int readingsTaken = 0;
float voltage = 0.0, readingTotal = 0.0;

void setup() {
  // put your setup code here, to run once:
  pinMode(sensorPin, INPUT);
  analogReference(EXTERNAL);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  readingTotal = 0.0;
  readingsTaken = 0;
  
  // we will take 5 readings at 1 sec interval and then do an average of that
  while(readingsTaken < READINGS) 
  {
    readingTotal += analogRead(sensorPin);
    readingsTaken++;
    delay(1000); //at every 1 second interval
  }

  voltage = (readingTotal/READINGS) * (2.8540/1024); // 2.8540 is the ref voltage being used. //the value in voltage at this point is what Arduino read based on input from voltage divider network. Need to calculate the original -- see below

  voltage = (20/2.609) * voltage; //unitary method to calculate the actual voltage that is read. When voltage read is 2.609, the input is 20v. With the above voltage divider used at input (100K ohm and 15k ohm) the input voltage at the I/O (pin A0) will be 2.609 when 20v is applied. 
   
  Serial.println(voltage); 
}

Voltmeter using Arduino

Using precisely calculated resistors and a stable power supply, Arduino can be used to work as a voltmeter.

Below is the schematic

Arduino Voltmeter

Though I used an Arduino Uno, but any Arduino can be used. (If the 3.3v versions are used then calculations will have to be done accordingly).

The resister for voltage divider has been chosen such that at 20 volts (measurement) supply the voltage at Arduino input pin is 5.0v. Greater than 5v at the input pin can damage the pin.

Here is the code

#define READINGS 5

int sensorPin = A0;
short int readingsTaken = 0;
float voltage = 0.0, readingTotal = 0.0;

void setup() {
  // put your setup code here, to run once:
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  readingTotal = 0.0;
  readingsTaken = 0;
  
  // we will take 5 readings at 1 sec interval and then do an average of that
  while(readingsTaken < READINGS) 
  {
    readingTotal += analogRead(sensorPin);
    readingsTaken++;
    delay(1000); //at every 1 second interval
  }

  voltage = (readingTotal/READINGS) * (5.0/1024); // the value in voltage at this point is what Arduino read based on input from voltage divider network. Need to calculate the original // 5.0 - is the ref voltage used by ADC. It is the default configuration and uses the voltage supplied to the board. To change the ref voltage/source please see this article

  voltage = (20/4.992) * voltage; //unitary method to calculate the actual voltage that is read. When voltage read is 4.922 (or 5.0v), the input is 20v. With the resistor divider at the input, the voltage at I/O will be 4.992

  Serial.println(voltage);
}

This is a simple and basic way. Where the precision will not be very good. Because the reference voltage being used by the ADC, which is actually the supply voltage can vary depending on the load to the circuit. To make it more precise an external reference voltage can be supplied to the Arduino. How to use an external reference voltage and the use of the AREF pin has been described in this article