My first project ( using sharp IR sensor )

Hi everyone! :slight_smile:

I got the arduino about an hour and a half ago, and after playing with an LED and an IR sensor I came up with this simple program.

It's not very exciting, but it might help anyone wanting to experiment with the sensors. Im using a Sharp GP2Y0A21YK from sk-pang.co.uk.

The script will basically turn the LED on pin 13 on or off when you wave your hand in front of it. The int time is used to make sure that it doesnt constantly flash on or off if you hold your hand or another object there, it should wait for about 1100 miliseconds.

Im hoping to eventually use this as part of a home lighting system :smiley:

// Written by Chris17
// Date: 8th May 2009

int sensorPin=0; // The signal pin for the sensor
int sensorVal=0; // The value that the sensor returns

int time; // The time since the last switch

int active = 0; // Whether the light is active

int ledPin = 13; // The pin the LED is connected to

void setup() {
pinMode( ledPin, OUTPUT ); // Sets the led pin to an output mode
}

void loop() {
sensorVal = analogRead(sensorPin); // Reads the signal from the sensor, returning a number. The number is higher the closer an object gets.

if( sensorVal > 250 ) // If an object is fairly close (around 25 centemetres or closer for me)
{

if( time > 1100 ) // If it has been more than 1100 miliseconds since the light last switched (ensures it doesnt blink randomly if it detects your hand twice)
{
time = 0; // Set the last time switched back to 0

if( active == 0 ) // If the LED isn't already on
{
active = 1; // Now it is!
digitalWrite( ledPin, HIGH ); // Turn on the power to the LED pin
}
else // Otherwise...
{
active = 0; // Now it isn't
digitalWrite( ledPin, LOW ); // Turn off the power to the LED pin
}
}

}
time += 10; // Adds 10 miliseconds to the time since it was last switched
delay(10); // Waits for 10 miliseconds
}

Nice little program. You should throw a RGB led in there for more fun :slight_smile:

Hehe, funny you should say that...

I actually have 4 RGB LEDs and I wired up one of them for this, but because I'm pretty new to electronics and it was my first thing with the arduino i just stuck with using pin 13 and ground to make it light up red on one of the pins. When I can get hold of some resistors i'll mess around with it though :D.

Glad you liked the program =D my first one with the Arduino.

Oh btw, Im now working on a RC toy car mod that will allow it to drive around the room on its own, avoiding objects using the sensor. I'll post everything when its finished, maybe a vid too =)

this is pretty cool m8 i wanna see this expanded further (like you said, a full lighting system for your house)

Heyo Chris,

Funny that.. My first project was something quite similar...
Even weirder, after Led13, i got out me RGBs!

Distance = Amount of colour.
Far away(80/630) = 100% red
Medium (240/630) = 100% green
Close (~600/630) = 100 % blue.

Distances inbetween were mixtures of the colours either side of the value.

Fun little IR sensor, and a refresh of 39us isnt bad! (for a 9 Euro sensor anyway!)

Enjoy RGB maddnessss -Harry

hello, i'm nicole... i have seen your work about the led and the sharp sensor... i'm doing the same thing for a projetc in my university and i have tried to change the distance of reception(from 250 to 300 or more) but it doesn't work... my sharp sensor is 2Y0A2I F 89 but i don't think make difference.. and i have an another question.. if i would to have the led switch off when nothing is in front of the sensor and.. then switch on and fixed when there is something how can i change the code? in the time? and at the end... if a would have more than one led... for exemple 3 different... what do i have to add to the code? just repeat the part of the first led with new int = led pin? thank u soooo much if u can help me.. i'm just a beginner!

nicky

hey i'm nicole, i've looked to your experiment and i'm doing a thing like this for a university project. I would like to do some questions to you...
-if a would change the distancem should i ust change the 250 value? b/c it doesn't work just doing like this;
-if i want to use more than a led.. but 3 what should i write?
-if i want to have the fixed light when something is in front of the sensor and then off light when nothing happens what i have to write? i have tried some changes but... i could do it! thanks a lot if u can help me! i'm a very beginner!

nicole

Hi, I am Nurul.... I need GP2Y0A21YK ir distance sensor for my final year project.... kindly inform me where i can get it....