Pages: [1]   Go Down
Author Topic: Problem with 2 Sharp GP2D12 IR sensors  (Read 1331 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to read data from two IR sensors but the results are wrong, some times returning 0 and mostly values don't make any sense.
The code i'm using is the following:

int IRpin = A0;                                 
int IRpin1 = A1;

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

void loop() {
  float distance = analogRead(IRpin);   
  float distance1 = analogRead(IRpin1);
 
  Serial.println("Sensor A0: ");
  Serial.println(distance); 
  Serial.println("Sensor A1: ");
  Serial.println(distance1);
  delay(1000);                                     
}

Any ideas?
Logged

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12489
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are the sensors wired?
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13896
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You first should get it working with one sensor.
Note the raw reading is not yet the distance in inches/cm...

check - https://wiki.engr.illinois.edu/display/ae498mpa/The+Sharp+GP2D12+Infrared+Range+Finder

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried with one, using necessary calculations to convert the results. The returned variables aren't stable but quite accurate. Then I connected the second sensor at A1 pin, GND, and 5V (as the other). Then it looks like something is really wrong smiley-confuse .
Thanks     
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13896
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Does both sensors work when used separately?

Might add a delay between the readings,
Sometimes doing 2 analogRead()'s and only use the last one helps to settle the signal.

Quote
The returned variables aren't stable but quite accurate.
You can do 16 readings per sensor and average them

your code refactored with averaging and the use of arrays for some vars

Code:

int pin[2] = { A0, A1 };  // array with two pinnumbers
int distance[2];   // array to hold the distances

void setup()
{
  Serial.begin(115200);    // we go for max speed ;)
  Serial.println("Setup...");
  // ...                     
}

void loop()
{
  // make 2 measurements
  for (int p = 0; p < 2; p++)   // do it twice for index 0 and index 1
  {
    distance[p] = 0;
    for (int i=0; i< 16; i++) distance[p] += analogRead( pin[p] );   // make 16 readings
    distance[p] /= 16;  // average them
    // optional delay(100) here..
  }
 
  // print them
  Serial.println( "Sensor A0: " );
  Serial.println( distance[0] );
  Serial.println( "Sensor A1: " );
  Serial.println( distance[1] );

  // and wait
  delay(1000);                                     
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your advise.
Dont know if something is wrong with my compiler but it returns something like: æԔ©Zè-á_xÊÙx́JøæêòÚ_{ÕÙyÌÙxÍË   ìÉøøÙxÀËm tØà°àØ
Have any idea?
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

make sure you serial monitor shows the same bit rate as you initialize your serial to in your code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh didn't notice that.
Looks like it works thank you very much
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13896
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Looks like it works thank you very much

do the readings make sense ?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When nothing is over the sensor, arduino returns many random variables from 0 to 300 or so (it happens with many codes Ι found on internet which uses the IR sensor with robotics). However when results are converted in cm, apart from the fact these aren't steady, it returns the correct distance when detects an object.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13896
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The return values are not random just raw measurements, that need to be processed to be meaningful for us humans.

As said you can minimize noise by averaging readings, can also be done with a running average:

Code:
#define PIN A0
long distance;

void setup()
{
  Serial.begin(115200);   
  Serial.println("Setup...");
  // ...                     
}

void loop()
{
  distance = (90*distance + 10 * analogRead(PIN))/ 100;   //  every new reading can change the disctance by 10%
 
  Serial.println( distance );
  delay(1000);                                     
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: