In this project, we will use GP2Y0A21 distance sense to measure distance between GP2Y0A21 and an obstacle object。The detectable distance range is 20cm to 150cm.

Parts and Devices:

Arduino UNO R3 x 1pc;
GP2YoA21 distance sensor x 1pc

3、Circuit connection graph:


Download project source code from following link:


Run above source code from Arduino IDE:

Open the serial terminal window in Arduino IDE upper right corner, you will see following result. The distance value will become large when obstacle is moving away from sensor.