Proximity (GP2Y0A21YK ) distance Sensor with Arduino

Last Updated on March 28, 2024

The IR (Infra Red) proximity sensor or distance sensor has two elements one is IR emitter and another one is IR receiver. We using GP2Y0A21YK wide-angle distance measuring sensor. It will produce 32 KHz range IR pulse and typical response time of IR receiver will be 39 ms.

If there is an object in front of this sensor (10 to 80 cm) that object reflects some IR rays back to the IR receiver by the way we can get detect objects. This circuit has two level of output that is Green and Red LED. When the object moves near to the sensor red LED will glow, otherwise the green LED will glow if the object detected by the sensor.
To use this IR distance sensor with arduino we need library file, get GP2Y0A21YK sensor library here.


Circuit diagram


The IR light isn’t visible to naked eye. You can see it thru mobile camera or digital camera.

Arduino Proximity (GP2Y0A21YK ) distance Sensor sketch code

// //

#include <DistanceGP2Y0A21YK.h>

int ledGreen = 9;
int ledRed = 10;
int proximity = 0;

void setup(){



void loop () {

int value = analogRead(proximity);
int redValue = map(value,0,1023,0,255);
int greenValue = map(value,0,1023,255,0);



Components List

1.Arduino uno1
2.GP2Y0A21YK sensor1
3.LEDGreen 1
Red 1
4.Resistor 220 Ω2
5.Connecting wiresas required

Leave a Reply

Your email address will not be published. Required fields are marked *