I have connected the displacement sensor to the arduino 5V. i am trying to get the displacement as output but the problem is that for example if i have the displacement 100 messured the output gives around 102,9... is it due to noise or is the code wrong but 0V i get 0mm and 5V i get 300mm. Max displacement is 300mm.
It feels like the almost 3mm is to much to be noise. Any tips?
int potPen = A1; //Assigning potPen to A0
int readValue; //Declaring out readValue Variable
float Voltage; //Declare out Voltage variable
float Displacement;
void setup() {
pinMode(potPen, INPUT); //Declare potPen an input
Serial.begin(9600);
}
void loop() {
readValue = analogRead(potPen); //Read potPen and put value
Voltage = ((5.000 / 1023.) * readValue) ; //calculating the real world voltage
Displacement =((300./5.)* Voltage);
Serial.println(Displacement,5);
Serial.println(Voltage, 5); //Print out real world voltage
delay(250);
}
A 0.1uF to 1.0uF cap from the wiper of the pot to ground will often help with noise.
By default the ADC uses the value of Vcc as its reference. You are assuming that Vcc is exactly 5.000V. For more accutacy, in terms of measuring Volts, measure the actual value of Vcc and use the measured voltage in your calculation.