Arduino analog pin voltage

Hi, I am facing a problem of reading analog values in arduino. I have tried to read these values from my project sensors but i get lot of errors away from what I expect. I programmed the attached program just to visualize what is happening to the the analog pins and I found the pins have a fluctuating voltage even if the sensor is unplugged, can someone help me how to go through it please.

sketch_apr19a.ino (1.27 KB)

I found the pins have a fluctuating voltage even if the sensor is unplugged

Unconnected pins act like an antenna, so the input level may float and read back randomly.
You can use the internal pullup to stabilize those, in setup() add:

pinMode (A0, INPUT_PULLUP);
pinMode (A1, INPUT_PULLUP);
pinMode (A2, INPUT_PULLUP);
pinMode (A3, INPUT_PULLUP);
pinMode (A4, INPUT_PULLUP);
pinMode (A5, INPUT_PULLUP);

Then they should read around 1023 with nothing connected.
Depending on your sensor, you may or may not need to disable the pullup up with:

digitalWrite(Ax, LOW); // turn off internal pullup on an input pin

Making this change may help also:

  sensorValue0 = analogRead(sensorPin0);
  sensorValue0 = analogRead(sensorPin0);  // 2nd read  helps voltage stabilize thru ADC circuit
  sensorValue1 = analogRead(sensorPin1);
  sensorValue1 = analogRead(sensorPin1);

Thank you man.. It worked very well and correctly.