Sharp IR on Arduino, color feedback

TheOther1's picture
Time to build: 
1 hour

Sharp IR sensor on Arduino with color LED feedack


I bought a Sharp GP2Y0A21YK0F Distance Sensor to use in my robot.  It was very easy to see the data returned in a serial console window, but I thought it would be cool to see the distance in zones represented by colors from a tricolor LED.  Then you would not need to be connected to the computer to tell distance.  Green would indicate a safe distance, yellow would represent a cautionary distance and red would be for distances that would require my robot to take evasive action.

For this How-To, I have used the following equipment and pin outs:

*  Arduino Diecimila
*  Sharp GP2Y0A21YK0F Distance Sensor
      Pin 1 => Analog 2
      Pin 2 => Gnd
      Pin 3 => +5V
*  1K 5% resistor (Brown Black Red Gold)
*  T-1 3/4 5mm Full Color LED (Radio Shack part #276-028)
      Pin 1 => Digital 9
      Pin 2 => 1K resistor => +5V
      Pin 3 => Digital 10
      Pin 4 => Digital 11

Using the sensor is as simple as connecting the output to an analog pin and power to, well, power. All you have to do is read the pin value:

variable = analogRead(pin);

I noticed that the values tended to jump so I thought I could smooth the output by averaging the results. This seemed to work pretty well. It was easily done by defining a variable for number of results to average, looping through a function that many times while reading the analog data, adding them up and dividing by number of loops.  The function returns the average:

//==========================================================================================
// Fuction to get distance from Sharp IRED unit
//==========================================================================================
int getDist()
{
  for (i=0; i < avgFactor; i++)
  {
    distRead = analogRead(sharpIR);
    runningTotal += distRead;
  }
  distAvg = (runningTotal / avgFactor);
  return distAvg;
}


Once I have the average, I determine the appropriate color for the LED using a function:

//==========================================================================================
// Function to change LED color based on distance:
//==========================================================================================
int colorLEDs(int)
{
  if (distAvg >= 500)  //Turn on LED red if > 500
  {
    redLED();
  }
  else if ((distAvg >= 200) && (distAvg < 500))  //Turn on LED yellow-ish if 200 < dist > 500
  {
    yellowLED();
  }
  else if (distAvg < 200)  //Turn on LED green if dist < 199
  {
    greenLED();
  }
}


//==========================================================================================
// Light up LED Red
//==========================================================================================
int redLED() {
analogWrite(ledR, 0);
analogWrite(ledB, 255);
analogWrite(ledG, 255);
}

//==========================================================================================
// Light up LED Yellow
//==========================================================================================
int yellowLED() {
analogWrite(ledR, 70);
analogWrite(ledB, 240);
analogWrite(ledG, 10);
}

//==========================================================================================
// Light up LED Green
//==========================================================================================
int greenLED() {
analogWrite(ledR, 255);
analogWrite(ledB, 255);
analogWrite(ledG, 0);
}

//==========================================================================================
// Turn off LED
//==========================================================================================
int offLED() {
analogWrite(ledR, 255);
analogWrite(ledG, 255);
analogWrite(ledB, 255);
}

The above values work out to about: < 3.5" = red, 3.5" - 8" = yellow, and +8" = green.  Of course, you can change these to whatever you want.

Full code is attached. Feel free to use, modify, comment, ridicule or ignore.  I hope this proves to be useful to someone.  I don't know where my video went...

Your rating: None Average: 4.7 (3 votes)
fritsl's picture

Very nice, I learned a lot

Very nice, I learned a lot reading it!

analogWrite(ledR, 70);
analogWrite(ledB, 240);
analogWrite(ledG, 10);

} //end yellow

 

Hmm.. OK.. interesting.. inspiring.. :)

OH - and regarding using "feedback" from robot: It can be hard in sharp daylight with a robot half hiding behind something, the robot turning away from you, and you facing your monitor.. to find out why it started driving backwards, backwards, backwards..

Even if an LED was giving a (short) flash when something happened.

So I find that adding a speaker is very useful too; If it gave a "Beep Bip Boouiip" (line 267 in code) then you KNOW that line 267 has been parsed - even if you did not catch the LED.

And also I then find that the better sounds tend to stay in the robot (I never get to the part of removing them again if they are not annoying) - and then the robot ends up having little "funny sounds" (sounds that you actually understand and have a relation to).

Communication with your electronic friend on a high level with visuals and audio is cool ;D

Thanks for sharing,

/ Fritsl

TheOther1's picture

Thanks Frtis.  The code for

Thanks Frtis.  The code for "yellow" is as slose as I could get without wasting too much time on it.  It's not really yellow, but it's close enough for me.  Once he is mobiloe, I may add some sounds.  Great idea.
Nicola's picture

Nice idea :)

Nice idea :)
TigPT's picture

very nice, thats the way =D

very nice, thats the way =D
Krumlink's picture

Wow this is great! This

Wow this is great! This could be used in a lot of things, survalence, etc.
TheOther1's picture

Thanks.  I like being able

Thanks.  I like being able to define functions and make the code as modular and reusable as possible.  I'm lazy like that!  ;)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.