Connected linear displacement sensor to arduino noise or not?

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?

this is the sensor that I use:

Displacement sensor

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.

What accurasy does the sensor datasheet promise? 3% error is not bad.

There is no need to measure the ADC reference voltage, since both the sensor and the Arduino ADC are ratiometric.

Try this and report what values you get for various displacements:

void loop() {
  Displacement =map(analogRead(potPen),0, 1024, 0, 300);
  Serial.println(Displacement);
  delay(250);
}

The map() function is integer only (so is the ADC), but for slightly higher accuracy you can try the equivalent floating point function.

Why are you calculating voltage? What is the ADC value when displacement is 0? When 300?

voltage is 4.6V now. At 300mm i now get a value of 262. Is this function still relevant and or, how can i change it?

Using what code? Always post the code, using code tags, so we know what you are talking about.

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