Language: EN

medir-distancia-con-arduino-y-el-sensor-gp2y0e03

Measuring distance with Arduino and the GP2Y0E03 sensor

What is a GP2Y0E03?

The Sharp GP2Y0E03 sensor is an infrared distance sensor that incorporates electronics for data processing and can be connected to a processor such as Arduino.

The GP2Y0E03 sensor is similar to the Sharp GP2Y0A02 sensor family that we saw in this post, but it is smaller, more precise, and incorporates digital I2C communication, while still maintaining analog output.

arduino-GP2Y0E03-interior

The measurement range is from 4 to 50 cm, maintaining a high degree of accuracy throughout the interval. The supply voltage is 2.7 to 5.5V and the current consumption is 26mA. The refresh interval between measurements is about 40ms.

The operation and characteristics are similar to the Sharp sensors. In particular, we remember that these types of sensors are not very sensitive to the color and reflectivity of the detected object, nor to the ambient temperature. The detection angle is narrow, about 5° fan-shaped in front of the sensor.

They also share their major limitations, such as the influence of ambient light, the possibility of interference when using multiple sensors, noise in the measurement, and, especially, the inability to determine if an object is outside the measurement range (too close or too far away).

The GP2Y0E03 is easy to connect and use. However, it is important to keep in mind that it incorporates a JST (Japanese Solderless Terminal) connector for its connection, so we will have to use a connector of this type or solder the terminals directly to the board.

arduino-GP2Y0E03-esquema

Applications for the GP2Y0E03 include distance detection in robots, touchless systems, presence detection, object detection in a specific area, among others.

Price

We can find a GP2Y0E03 sensor for about €3.80 from international sellers on Ebay and Aliexpress. At that price, it is more expensive than an ultrasonic sensor like the HC-SR04, similar to the rest of the Sharp sensors, and slightly cheaper than a TOF sensor like the VL53L0X.

arduino-GP2Y0E03-componente

How does a GP2Y0E03 work?

The GP2Y0E03 works similarly to the longer-range Sharp distance sensors. An infrared LED emits a pulse that illuminates a narrow beam in front of the sensor.

arduino-GP2Y0E03-funcionamiento

A CMOS sensor (an array of infrared sensors) detects the reflection of the beam on the object located in front of it, and the sensor’s electronics calculate the distance to the object based on the point on the sensor that detected the beam.

The GP2Y0E03 sensor is part of the family that includes the GP2Y0E02A and GP2Y0E02B sensors. However, it is the only one that allows supply voltages of 5V.

SensorOutputVoltageXYZ
GP2Y0E02AAnalog2.7-3.3V18.98.05.2
GP2Y0E02BDigital (I2C)2.7-3.3V18.98.05.2
GP2Y0E03Analog + Digital2.7-5.5V11.016.75.2

Wiring diagram

The GP2Y0E03 has both analog and I2C output, and either can be used. The pinout diagram of the GP2Y0E03 is as follows.

arduino-GP2Y0E03-montaje

If using analog connection, it is not necessary to use the SCA and SCL pins. If using I2C connection, we can leave the Vout pin unconnected.

arduino-GP2Y0E03-conexion

Code examples

Analog reading of the sensor is similar to the other infrared distance sensors we have seen. An example of code is as follows.

const int sensorPin = A0;
void setup()
{
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop()
{
  auto raw = analogRead(sensorPin);
  Serial.println(raw);  

  delay(100);
}

And here is an example of code for I2C communication.

#include <Wire.h>

// 7 highest bits

#define ADDRESS         (0x80 >> 1)


#define SHIFT_ADDR      0x35

#define DISTANCE_ADDR   0x5E

#define RIGHT_EDGE_ADDR 0xF8 // C

#define LEFT_EDGE_ADDR  0xF9 // A

#define PEAK_EDGE_ADDR  0xFA // B

uint8_t distance_raw[2] = { 0 };
uint8_t shift = 0;
uint8_t distance_cm = 0;
char buf[100];

void setup()
{
  Wire.begin();
  Serial.begin(9600);

  delay(2000);

  Serial.println("Initializing");

  Wire.beginTransmission(ADDRESS);
  Wire.write(byte(SHIFT_ADDR));
  Wire.endTransmission();

  Wire.requestFrom(ADDRESS, 1);
  if (1 <= Wire.available())
  {
    shift = Wire.read();
  }

  Serial.print("Read shift bit: ");
  Serial.println(shift, HEX);
}

void loop()
{
  // Read basic measurement
  Wire.beginTransmission(ADDRESS);
  Wire.write(byte(DISTANCE_ADDR));
  Wire.endTransmission();

  Wire.requestFrom(ADDRESS, 2);

  if (2 <= Wire.available())
  {
    distance_raw[0] = Wire.read();
    distance_raw[1] = Wire.read();

    // Print distance in cm
    distance_cm = (distance_raw[0] * 16 + distance_raw[1]) / 16 / (int)pow(2, shift);
    sprintf(buf, "Distance %u cm", distance_cm);
    Serial.println(buf);
  }
  else
  {
    Serial.println("Read error");
  }
  delay(1000);
}

Download the code

All the code from this post is available for download on Github. github-full