Reading 2 voltages simultaneously with Arduino

I’ve got the code from the Adruino-Examples-Basic-ReadAnalogueVoltage file, but I want to read 2 voltages (A0 and A1) at the same time and display them. I just want to test if my Mega is faulty on the analog but don’t know coding, and don’t really want to spend 3 months learning it for a 3 min test. Any simple hacks.

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}

Thanks

It’s not possible to do 2 things at the same time.
But you can read A1 just after A0:

void loop() { // read the input on analog pin 0: int sensorValue0 = analogRead(A0); int sensorValue1 = analogRead(A1); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage0 = sensorValue0 * (5.0 / 1023.0); float voltage1 = sensorValue1 * (5.0 / 1023.0); // print out the value you read: Serial.println(voltage0); Serial.println(voltage1); }

Just read that, but I’ll give that code a go.

thanks

If possible make sure the voltage you are measuring is stable (i.e not changing) as there will be some time (for the C code to execute, this could be reduced by moving to assembler, but likely isn’t worth it) so if you are measuring a varying signal you will get slightly (or a lot if it is varying fast and the A/D converter is fast) different results. That should tell you if the Mega is functioning correctly, if it is and your original task still isn’t behaving then you are likely in to the fun of analog circuit design and sampling theory.

Peter Van Epp

I just wanted to test if my Mega2560 is working properly.

I loaded the Speeduino IDE and opened TunerStudio (virtual car dashboard), but when I varied the voltage of pin A1 both the Inlet Air Temp and Coolant temp gauge move together like they are connected. A0 moves only the IAT gauge. So I tested the ReadAnalogVoltage on both pin A0 and A1, and they weren’t connected together.

Yes it sounded like you were trying to check the hardware (always a good place to start troubleshooting :slight_smile: ). Given the hardware seems to be working correctly that would point at an error in the code, possibly they are updating both readings from the same variable which isn’t correct or (a more scary thought!) they are using pointers and their indexing is incorrect (more scary because its usually harder to find). Good luck!

Peter Van Epp

They say their units work fine, but I installed the latest all the new software (IDE, TunerStudio, FW) for the first time last week, so maybe their updated programs have remnant files that make theirs work.

I kind-of need someone with a Mega, some resistors, and a pot to try a full fresh install.

This should be a no brainer, but I can’t find my Mega :frowning: I have one, I even saw it about a month ago while looking for something else, but I can’t find it now. I’ll keep looking in case you don’t find someone else.

Peter Van Epp

Sorry, found the problem.

I was checking the ReadAnalogueVoltage of A0 and A1 one at a time, but I was supposed the pull up or down the other pin at that same time.

That’s good because I still haven’t found my mega :slight_smile: but that said I’m somewhat confused. Are they reading a differential voltage (i.e. 2 pins reading the opposite ends of a resistor to measure the difference between the two voltages? Otherwise I don’t see why the two analog pins would have any relationship to each other.

Peter Van Epp

That;'s what I thought

The sensors are a temp dependant resistor - pot for sim - to ground between a voltage divider, with the Inlet Air temp is on A0 and the coolant temp is on A1. And it you don’t pull up or down on the other when it’s not connected to the pot they can affect each other. A0 IAT was fine, but pot on A1 would make both move together without the puller on A0.

Ah, now it makes sense. Yes with CMOS inputs if you leave them floating they can do odd things (such as drift to the value of the pin next door) so its not a matter of a differential input but rather that both analog pins need to be set to a proper value which I didn’t realize wasn’t the case, I’d assumed 2 pots one to set each value.

Peter Van Epp

I could only read one at a time so I only used 1 pot. My bad.

Got the thing running. Still waiting for IGN ICs.

Hi, I need to read 40 voltages with Arduino,
Can someone help me?

Since there are only 5 analog inputs on an Arduino you would need to use (probably several) external analog multiplexers such as 2 74hc4067 16 to 1 and 1 74hc4051 8 to 1 in core parts to get your 40 voltages multiplexed down to 3 analog input pins. Then you need to ensure the input voltages aren’t too high or low for the inputs (0 to 5V) and see if you can multiplex reading all the analog values in a time acceptable to your application.

Peter