## Introduction: How to Setup a Pololu Carrier With Sharp GP2Y0A60SZLF Analog Distance Sensor

As I started trying to use this sensor, I had some difficulties whilst setting it up.

The whole problem arises when you start to convert the analog voltage into distance. As you may or may not know, the sensor produces an output in analog voltage, that is then, read by the Arduino, converted into an integer from 0 to 1024 values and then calculated in distance.

For this project you need:

1 - Arduino
1 - Pololu Carrier with Sharp GP2Y0A60SZLF Analog Distance Sensor
1 - USB cable for the communications to take place
Some wires to connect it all

## Step 1: Knowing the Sensor

When checking the sensor's manual you see a similar graph to the one above, in which it shows what relation there is between analog voltage and distance.

As you can see, there is no clear nor linear way to solve this problem. Knowing this, there is only one option, calibration.

In order to calibrate the Arduino for this sensor you must measure the output of the sensor for each distance. I measured the output values from 10cm to 100cm with a 1cm increment. This allows you to get much more values therefore a better regression. If you don't need all that precision, calibrate the device with a bigger increment, say 5cm.

Since the values are hard to read and because I wanted more precision in my readings, I made a simple Arduino program that helps to calibrate the sensor properly.

I used the following code to retrieve the values from the Arduino.

In the code, a mean of 500 readings is calculated in order to null down the outliers.

```/*=========================================================Infra-Red calibration: Send mean integer value to serial
Written By Tiago A. on 21 Mar 2015
===========================================================
How to connect the sensor to the Arduino:
sensor - arduino
vcc - 5V
Gnd - gnd
out - A0
EN  - Do not connectBriefing:
This program has the sole purpose of calibrating the Infra-Red sensor.
It calculates the mean of 500 values in order to converge them into one correct value.
I am using this technique to null down the outlier values.How to use:
After uploading this program the Arduino will start to send the mean value of the distance through the serial port.
Don't forget to open the Serial Monitor in the Tools tab. (Ctrl + Shift + M)
After 500 readings are made, a "RESET" string will be printed as well as the LED will blink,
this is to allow the user to know when to move the sensor to another distance.*/
long sensorsum = 0;
int n = 1;
int mean = 0;
int lastmean = 0;// The setup routine runs once when you press reset:
void setup() {
// Sets pin 13 for output in order to blink the LED:
pinMode(13, OUTPUT);
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}// The loop routine runs over and over again forever:
void loop() {

// Read the input on analog pin 0:

// Calculate mean of measures of a specified distance in order to smooth the results and after create the regression line for distance
sensorsum = (sensorsum + sensorValue);
mean = (sensorsum / n);
n = n + 1;

if (n > 500) {
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
Serial.println("-----RESET--------------------------------------------------------------------------------");
n = 1;
sensorsum = 0;
lastmean = mean;
}

// The "Mean" value will vary  the whole loop beacuse it isalways calculating the mean with the read values
// The "Last Mean" value will only show the calculated mean value just to ease the reading of the calculated value
Serial.print(" Mean = ");
Serial.print(mean);
Serial.print(" Last Mean = ");
Serial.println(lastmean);
delay(25);
}```

## Step 3: Connecting the Arduino

I have used a Arduino Uno R3 for this project.

How to connect the sensor to the Arduino:

Sensor - Arduino
VCC - 5V
Gnd - GND
OUT - A0
EN - Do not connect

I connected a buzzer to the pin 13 so I also have an audio aid to the reset of the values for a new mean to be calculated.

## Step 4: How to Use the Program

After uploading this program, the Arduino will start to send the mean value of the distance through the serial port.

Don't forget to open the Serial Monitor in the Tools tab.(Ctrl + Shift + M)

After 500 readings are made, a "RESET" string will be printed as well as the LED will blink, this is to allow the user to know when to move the sensor to another distance.

After you position your sensor with a measured distance to an object of your choice, I used a sheet of paper, let the LED blink twice and then read the "Last Mean" value.

LED blinks along side the "RESET" string printed on the serial port.

## Step 5: The Regression

After all of the measures have been made, just use the trend-line options in your excel sheet and see which line is better for your values.

I used a Power trend-line that correlates the numbers very well.

Put the analogRead values in the X-axis and the Distance in the Y-axis. I've done it like that so I can use the regression function later on another Arduino program.

Due to the growing interest in the values stored in the excel file, it is now available at this Dropbox link.

## Step 6: Retrieving Good Distance Values

After all that work you should be able to get some decent distance measures with the sensor.

Here you have a working Arduino program with the values I got from the regression on excel.
In the line where IRdistance is defined, is where the function is located.

By now you should be able to retrieve the distance values properly.

Hope this instructable helped you somehow. =)

```<p>/*=========================================================<br>Infra-Red distance measures: Send distance value in centimeters to serial
Written By Tiago A. on 21 Mar 2015
===========================================================
How to connect the sensor to the Arduino:
sensor - arduino
VCC - 5V
Gnd - GND
OUT - A0
EN  - Do not connect</p><p>Briefing:
This program has the sole purpose of measuring distances with the Infra-Red sensor.
It uses a previous calculated regression to convert analogRead values into distance values.</p><p>How to use:
After uploading this program, the Arduino will start to send the distance of the sensor to the object through the serial port.
Don't forget to open the Serial Monitor in the Tools tab for debugging purposes. (Ctrl + Shift + M)
*/
const int irsensorpin = A0;</p><p>void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}</p><p>void loop(){</p><p>
// Reads the InfraRed sensor analog values and convert into distance.