Sharp Analog Distance Sensor

Hello

I have the above sensor (10 - 80cm version

It is connected to 5V, ground and 0 analog. I have put the sensor into a box to try and achieve a stable reading.

Why would the returned value still change? When I put my hand in the box the reading does jump - which is great, but how can I achieve a more stable reading when there is no activity?

Thank you for any help,

How much variation in reading are you seeing when in stable condition? Many use 'software filtering' where you take say 8 analog input readings in a row adding them together and then divide by 8 to get a smoother value.

Lefty

Many thanks for the advice.

I have now picked up the arduino smoother sketch which seems to run ok. I did have a few lines in my original sketch which would only return exceptional values. How do I add the extra lines?

// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = A0;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}

void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
}

This is the detail I need to include -

int value = analogRead(sensePin);

if (value > 400 || value < 300)
Serial.println(value);

Please use [ code] tags for code -> it is the # button

Try this, fixed a small bug too see below.

// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
long total = 0;                  // the running total
int average = 0;                // the average
int counter = 0;

int inputPin = A0;

void setup()
{
  Serial.begin(115200);                   
  for (int i = 0; i< numReadings; i++) readings[i] = 0;         
}

void loop() 
{
  total -= readings[index];
  readings[index] = analogRead(inputPin);
  total += readings[index];
  index++;
  counter = max(counter, index);
  if (index >= numReadings) index = 0;
  average = total / counter;         

  if (average> 400 || average< 300)
      Serial.println(average); 
}

Problem with the code was that the first numReadings the average is too low because of the division by numReadings...

I believe Sharp also recommends using a 10uF capacitor on the output pin
of the analog parts to smooth the signal.