Sharp IR Sensors Library

Hi all,

I've written a small library that gives a steady distance reading (in cm) of Sharp IR sensors. It works for the GP2Y0A21YK and GP2Y0A02YK, but it easy enough to expand it for the rest of the sensor family.

What the library does is to get a bunch of readings and, if one reading is close enough to the previous reading it takes it into account to calculate an average distance.

In my tests to set it to use 25 readings to calculate the average, it takes aprox 53 ms. The averaged distance matches quite well to the actual measured distance. The time taken is fast enough for the application I give to the sensors, I hope it's usufull to somebody else.

It can be found here: Arduino Playground - HomePage

hope you like it.

Hi, I have a Sharp GP2Y0A21YK0F infrared distance measuring sensor
Can you suggest what I can do to add this model to your library?
I am not sure what to look at on its data sheet to get the values.

Thanks

Hi, I re-read your cpp and example sketches and am now using model 1080 for my SHARP GP2Y0A21YK0F a 10-80cm ir sensor.

I am also running a parallax ping sensor. mounted at the same distance to the wall as the Sharp IR sensor.

It appears that the parallax ping sensor is reporting (within 1 cm) the actual distance in cms and the Sharp IR sensor using your example code is reporting roughly twice the distance in cm.

I measured with a metric ruler the distance from both the ping and sharp sensors at 15 cm
Ping reports 16 cm, the sharp ir sensor reports 35 cm

Any ideas as to why the large difference?

Thanks

PARALLAX_PING_SHARP_IR_TEST_1.ino (4.12 KB)

I'd suggest changing your tolerance parameter to the percentage of the previous distance that each reading must be within to be considered valid. E.g. in your example, values have to be within 7% of the previous value (not 93%).

Also, you check the see if the latest reading is >= (_tol * _previousDistance) which checks for values that are low, but doesn't insulate against values that are out of tolerance on the high side.

Mike

Mike,
thanks for the suggestions.
while checking my code I found that I set the model to 20150 in my code when I was using a 1080 sensor.
I change my code to set the model to 1080 and now it reports correctly. Same distance values as the ping within 1cm.

hey everyone, hope anybody can help me with this one:
for my project i use 2 sharp sensors, (ranges 100-550 and 20-150) building kind of guiding device/obstacle detector for blind. unfortunately both sensors have the same ‘problem’ i can’t fix. as seen from the datasheet, the output as a function of distance is parabola - therefore every output voltage may have 2 options for distance. despite the fact i want to use only the range 100-550 (for ex.), the sensor does also react to 0-100 giving different outputs which may be interpreted as other distances leading my blindman to hit the wall or something.. . i’m using the sensors with Arduino, and can’t find a way to handle this double values problem
have no idea for logical solution

will be very very grateful for solutions or any ideas Gary

Hey

i successfully uploaded your library and i am really excited to use it however every time i try to compile this code i get an error before sharp saying "expected initialer before sharp".... but the SharpIR is orange and is the initializer, why is it missing this?

also i am relatively new to all this. I would really appreciate if you checked the rest of the short code to see if it will do what i want.

thank you!

#include <SharpIR.h>


const int
PWM_A = 3,
PWM_B = 11,
DIR_A = 12,
DIR_B = 13,
BRAKE_A = 9,
BRAKE_B = 8,
SNS_A = A0,
SNS_B = A1,

SharpIR sharp(A2, 25, 93, 1080);

void setup() {
  
 pinMode(BRAKE_A, OUTPUT);
 pinMode(BRAKE_B, OUTPUT);

 pinMode(DIR_A, OUTPUT);
 pinMode(DIR_B, OUTPUT);
 
  
}

void loop() {
  
  int dis = sharp.distance();
  
  if(dis > 10) {  // If distance is greater than 10 cm do the following
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
  
  digitalWrite(DIR_A, HIGH); // motor 1 forward
  digitalWrite(DIR_B, HIGH); // motor 2 forward
  
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // mtoor 2 speed 200 
  delay(3000); // go for 3 seconds
  
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake

  }
  else {
    
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
    
  digitalWrite(DIR_A, LOW); // motor 1 reverse
  digitalWrite(DIR_B, LOW); // motor 2 reverse
    
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // mtoor 2 speed 200 
  delay(3000); // go for 3 seconds  
    
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake
  delay(1000); // wait a second
  
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
 
  digitalWrite(DIR_A, LOW); // motor 1 reverse
  digitalWrite(DIR_B, HIGH); // motor 1 forward
  
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // motor 2 speed 200 
  delay(1600); // I am going to tweak this number to get as close to a 90 degree turn as possible
  
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake
  
  }
}

Do you have the source code hosted on github or anywhere else? I'd like to add support for the new Sharp GP2Y0A60S sensor.

Hi, I am working with this library and works very good. But now I need connect two sensors to the arduino uno. I was trying to do it but this library don't works. What can I do? Thanks