Sunday, March 13, 2011

Arduino IR distance sensor

For a long time I’ve been trying to create some sort of robot, to accomplish that I need a bunch of different sensors. One of the big problems for me was a distance sensor.


I’ve found PING ultrasonic sensor, but it’s almost impossible to get one in my county. So I’ve been looking for a cheap solution. I’ve decided to use 2x IR LEDs one receiver and one transmitter.  Transmitter transmits for human invisible IR light it bounces from the object and returns to the receiver. Receiver it’s hooked up to an analog pin on the arduino. At first I’ve made measurements and then I’ve made equation based on which  you can get distance in centimeters.




Code for Arduino :

int IRSense = A0;
int IRTrans = 2;
int valueAmbient = 0;

void setup()
{
pinMode(IRSense, INPUT);
pinMode(IRTrans, OUTPUT);
Serial.begin(9600);
digitalWrite(IRTrans,LOW);
}

void ambient()
{
digitalWrite(IRTrans,HIGH);
delay(20);
valueAmbient = analogRead(IRSense);
delay(20);
}

void loop()
{
ambient();
digitalWrite(IRTrans,LOW);
delay(20);
int value = analogRead(IRSense);
int value2 = round(value - valueAmbient);
int dist = (((value2+15.0)/(value2*(value2+100.0)))*2000.0);
Serial.print(dist);
Serial.print("cm.");
Serial.println("");
delay(20);
}


Code for Processing :

import processing.serial.*;
Serial Port;
String distance ="";
String data ="";
int index = 0;
PFont font;

void setup()
{
size(500,500); 
Port = new Serial(this, "COM3", 9600);
Port.bufferUntil('.');
font = loadFont("AgencyFB-Bold-200.vlw");
textFont(font, 200);
}

void draw()
{
background(0,0,0);
fill(200,0,0);
text(distance,50,300);
}

void serialEvent (Serial port)
{
data = port.readStringUntil('.');
data = data.substring(0, data.length() - 1);
distance = data;
}

22 comments:

  1. elegant solution :-) nice work !

    ReplyDelete
  2. Thanks for your post.
    Does your solution detect millimeters?

    thanks again

    ReplyDelete
  3. Well you can use float instead of int so device can detect millimeters, but to make sure that it is precise a new calibration has to be done :)

    ReplyDelete
  4. Sensors are transforming the way engineers think about position sensing


    Distance Sensor

    ReplyDelete
  5. Could you explain the circuit?

    ReplyDelete
    Replies
    1. Well it's simple, IR LED beams IR light that bounces from object it hits and it comes back to the IR Sensor. Of course it's not very accurate, because different materials absorb different amount of light, but this method is really cheap and I believe that centimeter resolution is good enough for this sort of projects.

      Delete
  6. Do you think you could draw a quick schematic of your circuit and also what resistors where you using for the transmitter and reciever. I'd greatly appreciate it, Thanks

    ReplyDelete
    Replies
    1. http://legwinskij.blogspot.sk/2011/03/arduino-ir-distance-sensor-update.html :)

      Delete
  7. very nice, could this sense a fast moving object

    ReplyDelete
    Replies
    1. Well this sensor is very low level, it has small range and it's very inacurrate, so I don't think it's a good choice for moving objects, I would recommend you Ping sensor :).

      Delete
  8. On witch program do you need to upload the procesing code

    ReplyDelete
    Replies
    1. Processing is the name of language heres IDE http://processing.org/

      Delete
  9. Hi, i would like to know how you made this equation: (((value2+15.0)/(value2*(value2+100.0)))*2000.0)

    ReplyDelete
    Replies
    1. Well tha was a long time ago, but basic principle should be (1/value)*1000 then I was just tinkering with the numbers

      Delete
  10. No problem :), but it really depense on what type of IR receiver you have, because all of them have different values. So what I can recommend, is to measure values at exact centimeters and chcek them with ruler, then throw numbers into excel and let it make you an equation.

    ReplyDelete
  11. Hello, i have making project, but with 4 sensor ir, how can I do to read the 4 sensors and show on the screen each sensor?
    thx

    ReplyDelete
    Replies
    1. Hi ! If you need to use multiple sensors, you can perhaps use digital ones, but if you want to use analog ones, you can make array of analog pins and then access elements of that array via for loop reading different sensor everytime, only problem though, might be timing, so you would need to tune the code a bit

      Delete
    2. i test with one sensor ir and use resistor for led 300ohms and for receiver 10k, values ​​are ranging very far from being equal to the actual values

      Delete
    3. HI, Well each analog IR receiver has different analog values, in different ambient lighting conditions, so if you want to get precise measurements from your setup, you would have to manually calibrate it, for instance print out analog values for every centimeter that you move away from it, and then try to find some relation, excel could help you with that. If you don't want to go through this process, you can perhaps find some digital distance sensor, which would give you direct, and much more precise measurements

      Delete
    4. I did actually I need to measure 4x4x4cm cubes in various formats , for that thought of using four sensors to know what format I have. unfortunately so have sensor ir

      Delete
  12. how to connect a dc motor please give me the code

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete