# IR Distance Sensor SHARP GP2Y0A02YK with Arduino

[![KW-1476-2-1400x1050h.jpg](https://bookstack.hku.nl/uploads/images/gallery/2026-03/scaled-1680-/kw-1476-2-1400x1050h.jpg)](https://bookstack.hku.nl/uploads/images/gallery/2026-03/kw-1476-2-1400x1050h.jpg)

The Sharp GP2Y0A02YK infrared ranger is able to continuously measure the distance to an object. The usable range is 20 cm to 150 cm. The device generates an analog voltage that is a function of range, and the output voltage can be measured by an analog-to-digital (ADC) input line. The voltage will range from approx. 0.4V at 150cm to 2.7V at 20cm.

#### 1 Material:

  
\- sensor: [Sharp GP2Y0A02YK ](https://www.kiwi-electronics.com/en/ir-distance-sensor-20cm-to-150cm-gp2y0a02yk-721?search=1476)  
[- Arduino UNO](https://www.kiwi-electronics.com/nl/arduino-uno-rev3-atmega328-729?search=arduino%20uno) (other models also are fine) × 1 + [power cable ](https://www.kiwi-electronics.com/nl/usb-a-naar-usb-b-kabel-05-meter-763)(sold seperately)  
\- [Dupont jumper wires Male - male](https://www.kiwi-electronics.com/en/jumperwires-10-wires-m-m-15cm-589?search=dupont%20jumper%20male)  
[- Capacitor(≥10 µF)](https://www.makerguides.com/sharp-gp2y0a21yk0f-ir-distance-sensor-arduino-tutorial/#Capacitor%E2%89%A510_%C2%B5F)  
\- [breadboard](https://www.kiwi-electronics.com/en/400pt-half-size-breadboard-white-283?search=breadboard)

Solder some male jumper wires on the cable that comes with the sensor for easy connection to arduino or breadboard.

#### 2 Assembly

Connect the parts according this schematic:

[![SHARP-GP2Y0A21YK0F_bb.png](https://bookstack.hku.nl/uploads/images/gallery/2026-03/scaled-1680-/sharp-gp2y0a21yk0f-bb.png)](https://bookstack.hku.nl/uploads/images/gallery/2026-03/sharp-gp2y0a21yk0f-bb.png)  
These type of distance sensors tend to be a bit noisy, so it is recommended to add a capacitor between Vcc and GND. The datasheet suggests a capacitor of 10 µF or more (I used 220 µF). Connect the positive lead of the capacitor to the Vcc wire connection and the negative lead to the GND wire connection (see picture). Capacitors are often marked with a stripe which indicates the negative lead. The positive lead is often longer then the negative lead.

#### 3 Software &amp; code

 Download the [**SharpIR** library written by Guillaume Rico and Thibaut Mauon here.](https://www.makerguides.com/wp-content/uploads/2019/02/SharpIR-master.zip)  
For more information check the[ github page here.](https://github.com/guillaume-rico/SharpIR)  
This library is not available through the library manager of Arduino. You have to manually instal it. For how to manually installing a library [check this page. Scroll down to: ](https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries/)"Importing a .zip Library"

I've edited the code from the source website a little so you only get values and no text as output. Works better with visual programming software like Isadora or touchdesigner.

Copy this code into a new empty file, connect with your arduino and uplaod the code.

<div id="bkmrk-%2F%2Asharp-gp2y0a21yk0f" style="color: #dae3e3; background-color: #1f272a; font-family: Menlo, Monaco, 'Courier New', monospace; font-weight: normal; font-size: 12px; line-height: 18px; white-space: pre;"><div><span style="color: #7f8c8d;">/\*SHARP GP2Y0A21YK0F IR distance sensor with </span></div><div><span style="color: #7f8c8d;"> Arduino and SharpIR library example code. </span></div><div><span style="color: #7f8c8d;"> More info: https://www.makerguides.com \*/</span></div>  
<div><span style="color: #7f8c8d;">// Include the library:</span></div><div><span style="color: #c586c0;">\#include</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">"SharpIR.h"</span></div>  
<div><span style="color: #7f8c8d;">// Define model and input pin:</span></div><div><span style="color: #c586c0;">\#define</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">IRPin</span><span style="color: #dae3e3;"> A0</span></div><div><span style="color: #c586c0;">\#define</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">model</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">1080</span></div>  
<div><span style="color: #7f8c8d;">// Variable to store the distance</span></div><div><span style="color: #0ca1a6;">int</span><span style="color: #dae3e3;"> distance\_cm;</span></div>  
<div><span style="color: #7f8c8d;">/\* Model :</span></div><div><span style="color: #7f8c8d;"> GP2Y0A02YK0F --&gt; 20150</span></div><div><span style="color: #7f8c8d;"> GP2Y0A21YK0F --&gt; 1080</span></div><div><span style="color: #7f8c8d;"> GP2Y0A710K0F --&gt; 100500</span></div><div><span style="color: #7f8c8d;"> GP2YA41SK0F --&gt; 430</span></div><div><span style="color: #7f8c8d;">\*/</span></div>  
<div><span style="color: #7f8c8d;">// Create a new instance of the SharpIR class:</span></div><div><span style="color: #dae3e3;">SharpIR mySensor = </span><span style="color: #f39c12;">SharpIR</span><span style="color: #dae3e3;">(IRPin, model);</span></div>  
<div><span style="color: #0ca1a6;">void</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">setup</span><span style="color: #dae3e3;">() {</span></div><div><span style="color: #7f8c8d;"> // Serial communication at a baudrate of 9600</span></div><div><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">begin</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">9600</span><span style="color: #dae3e3;">);</span></div><div><span style="color: #dae3e3;">}</span></div>  
<div><span style="color: #0ca1a6;">void</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">loop</span><span style="color: #dae3e3;">() {</span></div><div><span style="color: #7f8c8d;"> // Get a distance measurement and store it as distance\_cm</span></div><div><span style="color: #dae3e3;"> distance\_cm = </span><span style="color: #f39c12;">mySensor</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">distance</span><span style="color: #dae3e3;">();</span></div>  
<div><span style="color: #7f8c8d;"> // Print the measured distance to the serial monitor</span></div>  
<div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">print</span><span style="color: #dae3e3;">(distance\_cm);</span></div><div><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">println</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">" "</span><span style="color: #dae3e3;">);</span></div>  
<div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">delay</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">1000</span><span style="color: #dae3e3;">);</span></div><div><span style="color: #dae3e3;">}</span></div></div><div id="bkmrk--1"></div>#### <span style="color: rgb(0, 0, 0);">4 Connect the arduino to your computer. </span>

<span style="color: rgb(0, 0, 0);">Connect Arduino to your computer &amp; Upload the code.  
If you do not know how to upload a Arduino sketch, please visit [https://www.arduino.cc/en/Guide/Windows](https://www.arduino.cc/en/Guide/Windows) for Windows user or [https://www.arduino.cc/en/Guide/MacOSX](https://www.arduino.cc/en/Guide/MacOSX) for Mac user.  
  
Open the serial monitor by clicking the Magnifying glass in the top right corner of the window.  
  
You should now see a stream of data measuring the distance to the object in front of the sensor. Hold your hand in front of the sensor and move it.  
  
</span>

##### For connecting with Isadora[ read this](https://bookstack.hku.nl/books/isadora/page/receive-data-from-arduino-in-isadora)

You can borrow this sensor for testing at the Blackbox location IBB. Contact: blackbox.ibb-pastoe@hku.nl

<span style="background-color: rgb(180, 247, 125);">source: </span>  
<span style="background-color: rgb(180, 247, 125);">https://www.makerguides.com/sharp-gp2y0a21yk0f-ir-distance-sensor-arduino-tutorial/</span>