Calibrating Sharp GP2D120 Sensors

Hi all,
I'm having some trouble with the calibration of my sharp sensors (GP2D120, http://www.sharpsma.com/webfm_send/1205). My code for testing the sensors is as follows:

#define ds_pin 0       // Pin to which the distance sensor is connected
int ds_value;          // Integer to hold the sensor’s reading

float ds_scaledValue;  // Distance sensor’s scaled value

void setup ()
{
  Serial.begin (9600); // Initiates serial communication at 9600 baud
}

void loop ()
{
  ds_value = analogRead (ds_pin);  // Reads the distance sensor
  ds_scaledValue = ((float)ds_value / 615) * 1024;  
  // Calculates the scaled value
  //dist_value = (1 / ( m * ds_scaledValue + 

  Serial.println ((int)ds_scaledValue);  // Screams out the scaled value as integer
  delay (200);                  // Delay of 200ms
}

I have soldered a 100nF and 10uF capacitor onto the sensor following http://letsmakerobots.com/node/22781. When I plug the sensor into the usb port of my computer and test it gives me an extremely inconsistent, jumpy result (1st image below). I then tried to power it using 6xAA batteries through the board while keeping the usb plugged in and this gave me a graph that initially dropped but then flattened out nicely, however the value was almost identical for all distances I tried so I'm assuming there is an error there. Just wondering if the issue is purely powering through the computer and if it is how can I record the sensor values to work out my calibration without plugging it into the usb port?
Thanks in advance for any help! :slight_smile:

Well looking at Figure 4 in that datasheet, it does not appear that the distance is linearly proportional to the voltage ( or to the analog input result ).

In fact, it appears that the voltage is inversely proportional to the voltage. Which is what figure 5 appears to show. So you need to rethink your formula.

But firstly, I'd be concerning with getting plausible inputs from the A/D converter first, and then worrying about the conversion to actual distance numbers later.

When you hold a piece of cardboard right in front of the sensor, you should get 3 volts, or around 600 from the analog read. When you move the cardboard away, the voltage should fall to about 0.2 Volts, or about 40 from the analog read.

I don't know what pin you are trying to use there for input. Pin 0 would be the pin which is used for serial communications with the PC. You need to use an analog pin. I would lose that #define and just use " ds_value = analogRead( A0 ) ".

You can also check the voltage using an ordinary multimeter as it is a plain voltage and not some high-speed signal.

I don't understand what you think you are doing, trying to plug this device into a USB socket.

You don't need either a computer nor an Arduino to test this device.

It is a 5V device. It needs a 5V source, not a 7V or 9V source. If you have put 9V into it, you have probably ruined it.

If you connect 5V and ground to the Vcc and Gnd pins, You should be able to test the device by observing the output voltage on the V0 pin with an ordinary multimeter.