Introduction: Sharp Sensor Tutorial With Arduino Uno - Learn to Measure Distance

About: We're a group of fun-loving, electronic DIY'ers with Smart-Prototyping.com. We hope to share useful guides and tutorials as well as the cool things we think up every-once-in-awhile. Enjoy!

In this tutorial, you will learn how to use the GP2Y0A41SK0F Sharp sensor with a simple intro example. You'll also learn how the sensor works. Afterward, you will be able to display on your monitor the distance between the sensor and an object.

The GP2Y0A41SK0F is very useful for detecting very close objects (4 cm to 30 cm).

To test out the Sharp sensor, you'll need:

Hardware:

The sensor uses a 3-pin JST PH connector. These cables have a 3-pin female JST connector at one end. On the other end, the wires are unterminated, so you must strip them at the appropriate length with scissors or a wire stripper.

Firmware:

  • Arduino IDE
  • Example Code

Tools:

  • A wire stripper (you can find many tutorials on the internet on how to use this tool)

If you bring up the picture of the Sharp sensor, you'll find on the right of the sensor, the position sensitive detector. On the left, is the IR emitting diode.


How does the position sensitive detector work?

Generally, when the detector area is exposed to a light spot, the device will convert light into several outputs of electrical currents in each of the sensor's electrodes. Using the distribution of the outputs currents you can then find the light position

When you add an IR emitting diode you can make a distance sensor.

The diode will produce light with a specific wavelength (IR) and the light sensor will only detect this wavelength. When an object is close to the device, the light will bounce back on it and the intensity of this light will be measured by the sensor (the voltage in our case). The sensor will then use this data to determine the distance of the object.

Let's get started!

Step 1: Hook Up the Sensor and Arduino

After stripping the ends of the wires to enable them to be inserted into the Arduino Uno, connect the sensor to the board.

Here are the connections to make:

Sensor > Arduino Uno

VCC (red) 4.5 to 5.5V > 5V

GND (black) > GND

Signal (yellow) > A0

Step 2: Upload and Run the Code:

  • Connect your Arduino to the computer using a USB cable
  • Open your Arduino IDE (Install the IDE if you haven’t already at www.arduino.cc/en/Main/Software)
  • Verify you have the correct board selected under Tools > Board
  • Verify you have the correct COM Port selected under Tools > Port

Here’s an example code you can upload (or in the picture above). It will print the distance between an object and the sensor.

Upload and run!

The serial monitor (Tools > Serial Monitor) is a feature of the Arduino IDE that is very useful to debug your code or to control your Arduino from your computer. In our case, the distance between the sensor and the object will be printed on this monitor (9600 baud for this code).

Step 3: Let's Understand the Data

Why do we multiply value from the sensor by (5/1024)?

We convert the analogRead() value to a voltage. The analog pins convert the value of the sensor to a byte value which is between 0 and 1023. But we need the real analog value. To do that, we divide the voltage rating, 5V, (maximum voltage that can safely be applied to an electric device) by 1024. Then we multiply the result of this operation by the value from the sensor to get our voltage.


What is pow(volts, -1)?

In this line ‘volts’ is the base and -1 is the exponent (power to which the base is raised).

Based on the Sharp datasheet graph we can calculate the function (For distance > 3cm, it’s exponential).

Now that’s you’ve successfully made this sensor work, it’s time to use it for your own projects! If you need inspiration of what to create, browse the other products on our website, maybe you'll find your next great project there.