# reading 2 voltages at the same time

i cant seem to read 2 voltages at the same time. can it be done?
my code is as follows the 3 is reading fine but the 5 returns 0.00 with or without a feed
i am reading the voltages from 2 voltage regulators that i want to keep an eye on during the running of the program

``````void loop() {
// read the input on analog pin 0:
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (3.5 / 750);
delay(1000);
// print out the value you read:
Serial.print("3 3Volt Supply = ");
Serial.println(voltage);

// read the input on analog pin 5:
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltageFive = sensorValueFive * (5 / 1023);
// print out the value you read:
Serial.print("5 Volt Supply = ");
Serial.println(voltageFive);
}
``````

can it be done?

No. You can't do two things, regardless of what they are, at the same time. However, the time that it takes to read one after another is about 208 microseconds. For most people, that's close enough to "at the same time".

That amount of time is completely dwarfed by your 1,000,000 microsecond delay.

``````float voltageFive = sensorValueFive * (5 / 1023);
``````

5 / 1023 is 0.

5.0/1023 is not. 5/1023.0 is not.

This has nothing to do with ADC

(5 / 1023) is a integer expression which evaluated to zero ( integer division ). The first time it works because you use 3.5, a non integer.

Use (5.0 / 1023)

But that is not what should be doing anyway. Read up on Vref for the ADC. Both lines should have the same scaling factor.

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() {
for (int x = 0; x < 1000; x++){ // 1000 readings
}
// 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");
}
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() {
for (int x = 0; x < 1000; x++) { // 1000 readings
}
for (int x = 0; x < 1000; x++) { // 1000 readings
}
// 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");
//
total_1 = 0; // reset values
total_2 = 0;
}
``````

Spot on i've confirmed the levels with a multimeter
i just have to wait for the resistors and the 10k trimpot's to come in the post tomorrow...
(scavenged the parts for one side off and old circuit board..
but they will be replaced with new tomorrow

thanks Wawa

5 Volt Supply = 4.55
5 Volt Supply = 4.91
5 Volt Supply = 4.84
5 Volt Supply = 4.92
5 Volt Supply = 4.86
5 Volt Supply = 4.84
5 Volt Supply = 4.88
5 Volt Supply = 4.93
5 Volt Supply = 4.89
5 Volt Supply = 4.93
5 Volt Supply = 4.83
5 Volt Supply = 4.89
5 Volt Supply = 4.82
5 Volt Supply = 4.85
5 Volt Supply = 4.83
5 Volt Supply = 4.87
5 Volt Supply = 4.84
5 Volt Supply = 4.82
5 Volt Supply = 4.92
5 Volt Supply = 4.90
5 Volt Supply = 4.85
5 Volt Supply = 4.92
5 Volt Supply = 4.87
5 Volt Supply = 4.91
5 Volt Supply = 4.84
5 Volt Supply = 4.93
5 Volt Supply = 4.93
5 Volt Supply = 4.81
5 Volt Supply = 4.92
5 Volt Supply = 4.82