Project showcase

Distance Measuring Sensor © GPL3+

Using SHARP 2Y0A21 Infrared Proximity Sensor

  • 7 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

About this project

Experimenting with a distance measuring sensor

The SHARP 2Y0A21 proximity sensor measures distance by shining a beam of infrared light and uses a phototransitor to measure the intensity of the light that bounces back. 

The bildr guide has explains the 2Y0A21 as well as other proximity sensors.  The product description for the SHARP 2Y0A21 can be found here.

The wires connected to the sensor are stranded wires, so I soldered each end to a piece of solid core wire to make it easier to insert into the Arduino.  I also wrapped electrical tape around the exposed wire connections to prevent short circuits.

The effective distance measuring range for this proximity sensor is 10-80cm.  If an object is closer than the shortest distance, it reports a significantly higher analog output and is inconsistent with expected in-range results.  For example, when an object is 1 cm away from the sensor, it reports an analogRead() value of 322.  However, when the object is 5 cm away, the output is 655.

I attached the proximity sensor to fixed object (in my case, I used the clips of the soldering iron stand to secure the sensor).

 With the proximity sensor in place, I took measurements from different distances.

With the proximity sensor in place, I took measurements from different distances.

Here are the analogRead() results:

20cm:  272

40cm:  145

60cm:  116

80cm:  104


Arduino Code for Proximity SensorArduino
This code works with any analog sensor!
//collects data from an analog sensor

int sensorpin = 0;                 // analog pin used to connect the sharp sensor
int val = 0;                 // variable to store the values from sensor(initially zero)

void setup()
  Serial.begin(9600);               // starts the serial monitor
void loop()
  val = analogRead(sensorpin);       // reads the value of the sharp sensor
  Serial.println(val);            // prints the value of the sensor to the serial monitor
  delay(400);                    // wait for this much time before printing next value


Connecting Proximity Sensor to Arduino
Sharp ir arduino 0
Analog Sensor Schematic
Schematic uses a light sensor, can also be replaced with a proximity sensor
1.b.ii.3 analog read serial schem



Jennifer Chen
  • 9 projects

Members who respect this project


and 6 others

Add projectSign up / Login