Simple obstacle detection

Hello,
At first, let me tell you what i want to do (1), what hardware i have (2), what i think i know (3) and what i need (4).

  1. I want to make a simple obstacle detection system using a Sharp IR sensor and a red LED (or a buzzer).

  2. I have an Arduino UNO, Ir sensor (Sharp GP2Y0A21YK) and red LED.

3.1. I am supposed to connect the sensor to GND, 5v and an Analog pin. (A3)
3.2. The red LED should be connected to the Digital ground pin and one of the Digital I/O pins. (D13)

  1. I need a code so that when the distance to the obstacle is < 50 cm the red LED will be On, and when the distance is > 50 cm, the led will be Off.

So, can somebody help me?

If i move the IR sensor from Analog pin 3 to Digital pin 3, will this code work?

#define IrSensorPin 3
#define RedLed 13


void setup() {  
  Serial.begin (9600);
  pinMode(IrSensorPin, INPUT);
  pinMode(RedLed, OUTPUT); 
}

void loop() {
  long duration, distance;
  digitalWrite(IrSensorPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(IrSensorPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(IrSensorPin, LOW);
  
  if (distance <= 50) {
    digitalWrite(RedLed, HIGH);
}
  else {
    digitalWrite(RedLed, LOW);
}

delay(500);
}

That looks like ultrasound code to me, not IR, and because you don't appear to give "distance" a value, the comparison may or may not fire, depending on what value "distance" happens to find on the stack.

You are right, i was trying to convert a code written for ultrasound into one for IR. I will keep trying.

Thanks for your quick feedback.

Hi Codnes, I think what you should focus on first is getting sensor data from your sensor. This is pretty basic but it's important to know what the data means since in it's raw for it can seem like an arbitrary number.

int IR;

void setup()
{
Serial.begin(9600);   //Initialize serial with baud rate 9600
}

void loop()
{
    x = analogRead(2); //Read value from analog pin 2 (You can change this if needed)
    Serial.println(IR);    //Print the value from the sensor over the serial monitor

As I mentioned before, this will return a value from the sensor but it's more or less an arbitrary number. It is useful though to test your sensor to make sure it's working. If you put your hand in front of the sensor and move it, you should see the numbers going up or down. If you don't, the sensor might not be working or is not connected to the analog pin the Arduino is reading. You might also notice that the numbers from this code fluctuate a little bit even if you have an object sitting in front of it that isn't moving. This is just noise in the signal and there are ways around it.In order to get an actual measurement out of that number we need to add another line that does the math for us. From what I have gathered the math used to turn the output from this sensor into distance in centimeters is Distance (cm) = 4800/(SensorValue - 20). So that can be added to the above code like so..

int IR;

void setup()
{
Serial.begin(9600);   //Initialize serial with baud rate 9600
}

void loop()
{
    x = analogRead(2); //Read value from analog pin 2 (You can change this if needed)
    int CM = (4800 / (IR -20)); //Convert the raw value from the sensor to CM
    Serial.println(CM);    //Print the sensor value in CM

This should have your Arduino outputting sensor data and giving you a relatively accurate measurement in CM. The last step in your request is to light an LED when the sensor picks up an object within 50CM. This is pretty basic as well and can be added into the code above. In your loop you will be checking the value from "CM" to see if it's "less than, or equal to" 50CM and under the condition that this is "True" light the LED.

Along with your LED, you will want a resistor. Anything between 220 ohms and 1k ohms should work fine. Connect the LED with your resistor in series between GND and one of the digital pins on your Arduino. I'll say D13 since it's right next to GND but you can use what ever pin you prefer. Let's get to the code..

int IR;

void setup()
{
pinMode(13, OUTPUT); //Set digital pin 13 as an output
Serial.begin(9600);   //Initialize serial with baud rate 9600
}

void loop()
{
    x = analogRead(2); //Read value from analog pin 2 (You can change this if needed)
    int CM = (4800 / (IR -20)); //Convert the raw value from the sensor to CM
    Serial.println(CM);    //Print the sensor value in CM
    if (CM <= 50)
    {
        digitalWrite(13, HIGH); //This turns the pin HIGH which will light the LED
    }
    else
    {
        digitalWrite(13, LOW); //Turn the pin LOW and thus turn off the LED
    }

Basically the loop will now always have pin 13 set LOW unless the sensor value in CM is less than or equal to 50. Each pass of the loop looks at this if / else set and should only light the led if the if statement returns true. This is very useful logic when you're trying to do things like make a robot turn away from a wall rather than run into it.

You need to make sure your Arduino board is connected to your computer to be able to see the serial outputs. From the Arduino IDE you can open the serial monitor after uploading your sketch and when the micro-controller reboots, you should start seeing output.

I hope the above examples are helpful to you.

Man I was busy typing this big long reply and the thread went on without me :slight_smile:

Thank you very much Steverobey, the explanation is great! This is just what i needed!

Glad I could help :slight_smile: