You can’t read the 5volt rail with the default 5volt Aref.
It will always be 1023, whatever the voltage of the 5volt rail is.
You should use the 1.1volt internal Vref to compare 3.3 and 5volt to.
Here is an example code, with oversampling, that reads to 10.230volt.
Drop the input voltage with a two-resistor voltage divider, as explained in the code.
Hardware trimmed. No weird maths, so no uneven gaps in the readout.
Leo…
// displays the voltage of a battery/supply/USB/Vin/etc. on the serial monitor and/or LCD shield
// works with 3.3volt and 5volt Arduinos
// uses the internal 1.1volt reference > independent of supply fluctuations
// max readout is 10.230volt, 0.001volt resolution, last digit is averaged
//
// ~82k resistor + 10k trimpot (calibration) in series from A1 to +batt/supply
// 10k resistor from A1 to ground
// 100n capacitor from A1 to ground for stable readings
//
// calibration: connect a 9volt battery and a DMM, adjust trimpot to the same reading
//
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // your LCD pins could be different
unsigned long total;
//
void setup() {
analogReference(INTERNAL); // use internal 1.1volt reference, change (INTERNAL) to (INTERNAL1V1) for a Mega
Serial.begin(115200); // ---set serial monitor to this value---
lcd.begin(16,2); // shield with 2x16 characters
lcd.setCursor(0,0); // first row
lcd.print("Voltmeter"); //info text
lcd.setCursor(0,1); // second row
lcd.print("0-10.230 volt");
delay(2000); // info display time
lcd.clear(); // clear
lcd.setCursor(0,0);
lcd.print("Voltmeter 0-10V"); // print once
}
//
void loop() {
analogRead(1); // one unused reading to clear old sh#t
for (int x = 0; x < 1000; x++){ // 1000 readings
total = total + analogRead(1); // add each value
}
// print to LCD
lcd.setCursor(0,1);
if(total == 1023000){ // max A/D reading * 1000
lcd.print("----overflow----");
}
else{
lcd.print("A1= ");
lcd.print(total*0.00001, 3); // convert milivolts to volts, display with 3 decimal places
lcd.print("volt");
}
// print to serial monitor
Serial.print("Raw average = ");
Serial.print(total*0.001, 3); // 1000 readings /100, 2 decimal places
if(total == 1023000){
Serial.print(" ----overflow----");
}
else{
Serial.print(" Analogue input A1 = ");
Serial.print(total*0.00001, 3); // convert milivolts to volts, display with 3 decimal places
Serial.println(" volt");
}
delay(1000); // optional readout delay
total = 0; // reset value
}
Stripped, but two sensors.
Should output some random voltage on the serial monitor, without anything connected to the Arduino.
unsigned long total_1;
unsigned long total_2;
//
void setup() {
analogReference(INTERNAL); // use internal 1.1volt reference, change (INTERNAL) to (INTERNAL1V1) for a Mega
Serial.begin(115200); // ---set serial monitor to this value---
}
//
void loop() {
analogRead(1); // one unused reading to clear old sh#t
for (int x = 0; x < 1000; x++) { // 1000 readings
total_1 = total_1 + analogRead(1); // add each value
}
analogRead(2); // one unused reading to clear old sh#t
for (int x = 0; x < 1000; x++) { // 1000 readings
total_2 = total_2 + analogRead(2); // add each value
}
// print to serial monitor
Serial.print("Raw average = ");
Serial.print(total_1 * 0.001, 3); // 1000 readings /100, 2 decimal places
Serial.print(" | ");
Serial.print(total_2 * 0.001, 3); // 1000 readings /100, 2 decimal places
Serial.print(" Input A1 = ");
Serial.print(total_1 * 0.00001, 3); // convert milivolts to volts, display with 3 decimal places
Serial.print(" volt");
Serial.print(" Input A2 = ");
Serial.print(total_2 * 0.00001, 3); // convert milivolts to volts, display with 3 decimal places
Serial.println(" volt");
//
delay(1000); // optional readout delay
total_1 = 0; // reset values
total_2 = 0;
}