Skip to content

Measure distance using Sharp IR proximity sensor with Arduino + OLED []


Sponsored by DigitSpace
Products used: Sharp IR proximity sensor and 0.96″ OLED display 128×64 Blue/Yellow


            Hello and welcome, this the first sponsored project from DigitSpace, and it’s about distance measuring using the Sharp IR proximity sensor GP2Y0A02YK0F, the measurement is in both metric and imperial systems, and we’ll use an OLED screen, there are a lot of possibilites to do with it: display the measurements with different units in the same time, switch between them…

           The sensor emits beams of infrared light, and they reflect back to the sensor. the output can sometimes be digital like FC 51 IR module, which means at a certain distance it will give you a HIGH or LOW level, or like the one we’re using here which has an Analog Ouput: It gives you a voltage value depending on how far is the object to detect, but don’t forget that it depends on the object reflectiveness and the lighting conditions.

Looks more aggressive than the Ultrasound one
As you can see the module has an emitter and receiver

The range can also differ depending on the version mein has 20 cm -> 120 cm, so it’s better to use yours and test it to know its limits.

The Module has 3 wires: Vcc, Gnd and The Data or Output (Analog).

Test 1




Wiring 1



There are different libraries to use but this one worked for me very well, you can download it from the link right here.

To install it, just: Arduino IDE -> Sketch -> Include library -> Add .ZIP file and select the library.


The first time you can choose to test with the library example it’s very simple or test with this code.
Download the code here. (My website code editor isn’t good, so you can’t copy it, it’s better to download the .ino version)


Upload the code and open the serial monitor:

Shows the measured distance in the serial monitor

Sensor problem

The issue with the module is that at distances inferior to 20 cm it starts to give strange values (40 cm 35 cm…) which is not suitable for close range applications.

OR AM I THE ONLY ONE WHO HAS THIS ISSUE? Well test and tell me.

click to expand the picture

Test 2

The test 2, just replace the serial monitor with an OLED display, and add a push button to switch between some units (cm-mm-in).

Wiring 2

For the push button I like to use this wiring, and in the code I activate the internal pull-up, the button is always at HIGH state and when I press it goes LOW, you can use the usual method but you need to wire it with 5v and add a pull down resistor then set the pinMode as Input only and in the code activate the state as HIGH level instead of LOW.

+ OLED and push button

And of course you can replace the OLED with the 128×32  version or LCD or whatever you like.


In addition of the sensor library used before: download it from the link right here, you should add the OLED Adafruit libraries:

Download here  Adafruit OLED SSD1306 library.
Download here Adafruit GFX Library.


Download the code here (It contains the comments needed).


Default display in mm
Press button to go to cm
Or use some strange units

That’s it for this module, the only downside was that the sensor doesn’t give a warning or a stable value when it’s measuring under 20cm, but rather gives false readings…. Don’t forget to support the website and channel.


Big thanks to DigitSpace for their sponsorship.


Yassine View All

Automation and Electrical Engineer, Electronics amateur trying to share my little projects.

One thought on “Measure distance using Sharp IR proximity sensor with Arduino + OLED [] Leave a comment

Leave a Reply

%d bloggers like this: