I am programming a displacement sensor to give me the displacement from 0-310mm with a voltage of little over 4,6V from a powersupply to the arduino. My question is wtih 5V you use 1023 to calculate the real world voltage if I 4,6 volt is it correct to use 893 because it gives me good numbers but I got to it trying. Is it correct or what should i change?
void loop() {
readValue = analogRead(potPen); //Read potPen and put value
Voltage = ((4.621 / 893.) * readValue) ; //calculating the real world voltage
Displacement =((310./4.621)* Voltage);
Serial.println(Displacement,5); //Print out real world voltage
delay(250);
}
No, you should still use 1023. The voltage is not relevant, it's the distance you are measuring.
By default, the Arduino's analog inputs measure the input voltage relative to the power supply. When the power supply is 4.6V, an input voltage of 4.6V will give a reading of 1023, just like it would if the supply was 5V.