Ir Distance Sensor Probelm

Hello.
I am new to arduino and followed most of the tutorials in the help section. Also got an lcd display and a solar motor working. :slight_smile:

But my Ir Distance Sensor, it's a DIRRS+ (GP2D12). (Under Download Resources you can find the datasheet: http://www.hvwtech.com/products_view.asp?ProductID=91)

It has 4 Pins. I connected
Pin 1 to (Arduino) Ground
Pin 3 to (Arduino) 5V
Pin 4 to (Arduino) Analog Pin 2.

I do not use any resistors.(Tried it though but it does not change the behaviour of the values I get) If I connect the wire only to (Arduino) Analog Pin 2, I get random values from 0 to 1000. Is this okay?

I am using this code:

int sharpIR = 2;
int avgFactor =10;


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


void loop()
{
  
  int val = getDist();
  Serial.println(val);
  delay(100);

 
}


int getDist()
{
  int runningTotal=0;
  for (int i=0; i < avgFactor; i++)
  {
    int distRead = analogRead(sharpIR);
    runningTotal += distRead;
  }
  int distAvg = (runningTotal / avgFactor);
  return distAvg;
}

At first I did not do any smoothing, but it does not change much.
In 99 of 100 values I always get 1023. If I move a large newspaper around the ir distance sensor it doesn't change its values.

Please help me.

Sincerely,
Maxi

you need to put a capacitor between 5v and ground on the sensor preferably soldered right on it ;D this will smooth the power supply to the sensor an give better results. There are a few other posts about this sensor in this forum, but I currently don't have links :stuck_out_tongue:

Thank you. I searched the forum and found this article: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213773881/2#2
One person said that he used an 100uF capacitor.
And I found a link: Sharp Infrared Distance Sensor Test Apparatus, Page 3 - Robot Room - Scroll to Section: Analog Signal Capacitance
dealing with this sensor. I think this article suggest an 47 nF capacitor?

The one is uF, the other nF. So it's a huge difference between both of them?

I already soldered a 4-pin-socket (http://www.nkcelectronics.com/2mm-10pin-soc.html) to the 4 pins of the ir distance sensor. And the wires connecting from arduino to the sensor will be about 15 to 20 cm long.

What's important to me is that I can detect if an object is 20 cm or if it's 60 cm away of the sensor.

And what type of capacitor do I need? I looked on the website of an electronic shop which is nearby and it has more than 20 different types of capacitors. What kind of capacitor do I need, with which parameters? If I don't know this they old guy in my hardware store will roll his eyes....

Out of curiosity, did you actually read the user's manual for your sensor (the one you linked in your first post)? According to the manual, your sensor has been modified by HVWTech to output its readings synchronous or asynchronous serial depending on the configuration mode. You can't use analogRead() to read this serial output because the sensor is not outputing an analog voltage. Your routine would probably work fine if you had a standard Sharp GP2D12.

Additionally, lack of a bypass capacitor wouldn't explain the behavior your are seeing, but adding a bypass capacitor could eventually help limit the effect the sensor has on your system's power, and it could lead to cleaner results. If you look at the Sharp GP2D12 datasheet (you can find this on digikey), the notes section on page 8 gives you some handy tips, including:

A 10 [ch956]F (or larger) bypass capacitor between VCC and GND near the sensor is recommended.

You will probably want to use an electrolytic capacitor placed as close to the actual sensor as possible.

  • Ben

Thank you.
I have a printed version of the online manual and I read it. On the first page it said:

Configuring the DIRRS+
The GP2D12 has the ability to sense an object between 10 and 80 cm and
produce a voltage proportional to the distance measured. The voltage ranges
from 2.6V for objects at 10cm, to 0V for objects at 80cm. An Analog to Digital
converter reads the voltage and provides a value ranging from 255 for objects at
10cm, to 0 for objects at 80cm. This value is then transmitted depending on the
configuration you have selected.

So I thought it gives me an analog value.

If I use the default configuration:

Configuration 1: Default
Mode: Serial HEX
A single 8 bit value is transmitted on Pin 4.
4800 bps RS-232 TTL 8N1 (8 bits, no parity, 1 stop bit)

I have to read 8 bits, that means i have to call 8 times digitalRead()? But how does a stop bit look like, so that I can recognise the beginning of one 8 bit value? How do I kow what delay to use between the 8 digitalRead statements?

Your sensor is communicating to you using asynchronous serial. Connect this output to your Arduino's UART's receive line (I haven't used an Arduino so I don't know if this is considered poor practice or will interfere with uploading sketches) or connect it to an arbitrary digital I/O and use software serial to get the values.

  • Ben

if you leave it plugged into the hardware serial ports, the sketch will not upload properly. so either unplug it while using serial communication and uploading, or use software serial (Like bens said ;))

Where can I find information on software serial communication?

Where can I find information on software serial communication?

google
try: arduino softwareserial