Measuring DC voltage with Arduino Mega

I created this simple project to measure a DC voltage using an Arduino Mega. The circuit diagram is as follows:

The power jack supplies 9 V ideally. With my multimeter, I measured it to be 10.16 V.

The code for measuring the voltage is:

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd;

const int LCD_COLS = 16;
const int LCD_ROWS = 2;

const float R1 = 4.55 + 0.099;  // ideally 4.8 kohm
const float R2 = 1.166;  // ideally 1.2 kohm

void setup() {
    lcd.begin(LCD_COLS, LCD_ROWS);
    lcd.setCursor(0, 0);
    lcd.print("Voltage: ");
    lcd.setCursor(0, 1);
}

void loop(){
    float reading = (analogRead(A0) * 4.9) / 1000;

    float voltage = (reading * (R1 + R2)) / R2;
    
    lcd.setCursor(0, 1);
    lcd.print(voltage);
    lcd.print("V");
    delay(5000);
}

To get an accurate reading, I replaced the resistance values in the code with what I measured using the multimeter.

But, as you can see below, there is still a difference of around 2 V.

Any idea how I can rectify this?

What do you measure with the DVM across the 1.2k resistor ?

I get 1.257 V.

And assume your 5v power supply is 4.9v ?

And

V(4.8k) = 8.883v

The DC jack is providing 10V, which is what I am trying to measure. I am powering the Arduino using a USB cable from my laptop. So, yes, the power supply to the Arduino is 5V.

V(4.8k) = 8.883v ?

Yes, I am getting 8.87 V.

If you indeed have 4.8k and 1.2k.

(10.14 * 1.2k) / 6k = 2.028v across 1.2k

(10.14 * 4.8k) / 6k = 8.112v across 4.8k


float reading = (analogRead(A0) * 4.9) / 1000;
should be:
float reading = (analogRead(A0) * 5v) / 1023;

1 Like

Also, is there AC ripple on the 9v DC ?

As I said in the OP, the resistors are not exactly 4.8k and 1.2k; I am using the correct value in the code (correct as in measured by my DVM).

With this, the reading on the LCD becomes 9.97 V, far closer to the real value. Thanks!

There could be some minor ripples, but I can't measure it with my oscilloscope, as I use that same adapter to power the DSO1838.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.