Calculating the real world voltage

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.

Look at these 2 lines:

  Voltage = ((4.621 / 893.) * readValue) ;
  Displacement =((310./4.621)* Voltage);

The first line multiplies something by 4.621. The second line uses that result and divides it by 4.621. Result: the value cancels itself out.

Can only be more certain about this answer when you post your schematic and links to component data sheets.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.