measurement of different voltages

Hey,

I want to measure different voltages with my arduino.
Therefore I wrote the following code:

void setup() {
  Serial.begin(115200);  
}

void loop() {
  float iVol_U0 = map(analogRead(0),0,1023,0,1475)/100.00;
  float iVol_U1 = map(analogRead(1),0,1023,0,1475)/100.00;
  float iVol_U2 = map(analogRead(2),0,1023,0,1475)/100.00;

  Serial.print(iVol_U0);
  Serial.print(",");
  Serial.print(iVol_U1);
  Serial.print(",");
  Serial.println(iVol_U2);

  delay(100);
}

When I run this I get the following output in the serial monitor:

4.07,3.68,3.74

The point is that only pin 1 is connected to a power supply right now.
Don't I see the wood for the trees or what is going on here?
My expectation is to only see a value on pin 1 where pin 0 and pin 2 should be 0.

Cheers!

My expectation is to only see a value on pin 1 where pin 0 and pin 2 should be 0.

Why do you think that?

If I have understood you correctly pins 0 and 2 are not connected to anything, that means they have no particular voltage on then. 0V is a particular voltage and would be expected if you connected a pin to 0V. An unconnected input will have a random voltage on it somewhere between 0V and the supply voltage.

Hi,
What model Arduino?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

PerryBebbington:
Why do you think that?

If I have understood you correctly pins 0 and 2 are not connected to anything, that means they have no particular voltage on then. 0V is a particular voltage and would be expected if you connected a pin to 0V. An unconnected input will have a random voltage on it somewhere between 0V and the supply voltage.

I thought that when there is nothing connected there is nothing to measure and therefore it should be 0.

But you already got the right hint. When I connect the open ports to a specific voltage everything works as expected. I already thought that my arduino is broken :slight_smile:

Sorry, the solution was too simple for me :slight_smile:

Hi,
Try this edited part of your code.
If you have nothing connected to an analog output then connect it to gnd, at the moment you have 2 unconnected floating, high impedance inputs, and they can assume any voltage that leaks around them.

You will notice that in the code I read each input twice.
This is because there is only ONE ADC, it has its input switched to each analog input as the code selects it.
However, the ADC has a small capacitor on its input, and this need time to charge to the new input voltage as it is selected.
By reading the input twice and using the last value, you are enabling the capacitor to stabilise.

Also this map function returns int values, so the (float) is needed.

void setup() {
  Serial.begin(115200);
}


void loop() {
  //Read each analog input twice to allow the analog multiplexer to stabilise on each input.
  int U0Raw = analogRead(A0);
  U0Raw = analogRead(A0);
  int U1Raw = analogRead(A1);
  U1Raw = analogRead(A1);
  int U2Raw = analogRead(A2);
  U2Raw = analogRead(A2);


  float iVol_U0 = (float)map(U0Raw, 0, 1023, 0, 1475) / 100.00;
  float iVol_U1 = (float)map(U1Raw, 0, 1023, 0, 1475) / 100.00;
  float iVol_U2 = (float)map(U2Raw, 0, 1023, 0, 1475) / 100.00;


  Serial.print(iVol_U0);
  Serial.print(",");
  Serial.print(iVol_U1);
  Serial.print(",");
  Serial.println(iVol_U2);


  delay(100);
}

Tom.... :slight_smile:

I thought that when there is nothing connected there is nothing to measure and therefore it should be 0.

This is a common misunderstanding for people new to electronics. Everything has a voltage, well, everything physical anyway, so there is always something to measure. What that voltage is depends on the properties of the thing you are measuring and what it is connected to. In the case of Arduino (and other micro controllers) the inputs are basically tiny capacitors which are otherwise open circuit. Being like that they pick up whatever stray voltage there is in the environment and that is what you measure. There are also input protection diodes which limit that voltage to be within (or just slightly beyond) the supply and ground voltage.

all right, thank you both for the help and the explanation.
Now everything works as expected.