0
\$\begingroup\$

I am trying to connect a Sharp Analog IR Distance Sensor GP2Y0E02A to a nano v3. It accepts 2.7 - 3.3 v so I have it connected to 3.3 v from the arduino but I dont get serial output, just complete blank. When I change the code to something a little similar (see code under buggy code below) I get voltage values. So I think the problem might be with the code which I am using to convert voltage to cm below.

Buggy Code (voltage to cm)

/***************************************************************
  Arduino GP2Y0E02B example code
  Gets range from GP2Y0E02B and prints it to the serial monitor.

  By James Henderson 2014
***************************************************************/

#include <Wire.h>

int distance = 0;                // Stores the calculated distance 
byte high, low = 0;              // High and low byte of distance
int shift = 0;                   // Value in shift bit register

#define ADDRESS       0x80 >> 1  // Arduino uses 7 bit addressing so we shift address right one bit
#define DISTANCE_REG  0x5E
#define SHIFT         0x35

void setup()
{
  // Start comms
  Wire.begin();
  Serial.begin(19200);

  delay(50);  // Delay so everything can power up

  // Read the sift bit register from the module, used in calculating range
  Wire.beginTransmission(ADDRESS);    
  Wire.write(SHIFT);
  Wire.endTransmission();

  Wire.requestFrom(ADDRESS, 1);
  while(Wire.available() == 0);
  shift = Wire.read();
}

void loop()
{
  // Request and read the 2 address bytes from the GP2Y0E02B
  Wire.beginTransmission(ADDRESS);
  Wire.write(DISTANCE_REG);
  Wire.endTransmission();

  Wire.requestFrom(ADDRESS, 2);

  while(Wire.available() < 2);

  high = Wire.read();
  low = Wire.read();

  distance = (high * 16 + low)/16/(int)pow(2,shift); // Calculate the range in CM

  Serial.print("Distance is ");
  Serial.print(distance);
  Serial.println("CM");

  delay(50);
}

Simple Example (works by giving voltage)

int sensorpin = 5; // 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(100);                    // wait for this much time before printing next value
}

enter image description here

Schematic

enter image description here

enter image description here

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The problem is you are using i2c (the protocal behind the wire libary) to communicate with an analoge device. The sensor you have chosen returns the distance to the controller as an analog voltage in the range -0.3 to +2.8V (see the datasheet linked from the store page). Therefore you cannot communicate with it via a digital protocal.

To Make this work wire it up like this:

schematic

simulate this circuit – Schematic created using CircuitLab

The code you need to control it should look something like this:

void setup()
{
  // Start serial connecton back to PC
  Serial.begin(19200);  
}

void loop()
{
  // Enable the sensor
  digitalWrite(15, HIGH);

  // Wait for it to turn on
  delay(100);

  // Read the analog value from the device
  value = analogRead(0);

  // power down the sensor
  digitalWrite(15, LOW);

  distance = /* do math here, see datasheet for typical values */; // Calculate the range in CM

  Serial.print("Distance is ");
  Serial.print(distance);
  Serial.println("CM");

  delay(50);
}

Also I would strongly recomend reading the datasheet of the device you have ordered carfully.

EDIT: I noticed that the code you are using is for the GP2Y0E02B not GP2Y0E02A. The GP2Y0E02B is digitally controlled over I2C, your GP2Y0E02A only outputs an analog value. Is it possible you ordered the wrong version of the sensor?

\$\endgroup\$
6
  • \$\begingroup\$ thanks! Where would I put that in the code, would I have to remove any part of the code? Not very familiar with i2C \$\endgroup\$ Commented Aug 10, 2016 at 12:18
  • \$\begingroup\$ you need to ditch all of the i2c code and replace it with completely new code. I'll add a sample to the answer \$\endgroup\$
    – Segfault
    Commented Aug 10, 2016 at 12:25
  • \$\begingroup\$ Yes you are correct, I got the wrong one!! Thanks for noticing that. Will use the code above on the model I have now though \$\endgroup\$ Commented Aug 10, 2016 at 12:43
  • \$\begingroup\$ I just uploaded the schematic from the docs and also took a pic of the circuit close up. I am having a little difficulty figuring out which one is Vout and GPIO1. The VCC and GND I assume are just the red and black (next to red in the pic above). But the other 2 seem ambiguous.. any ideas? \$\endgroup\$ Commented Aug 10, 2016 at 12:50
  • \$\begingroup\$ yellow is Vout blue is GPIO1, I better not be doing your job for you? XD \$\endgroup\$
    – Segfault
    Commented Aug 10, 2016 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.