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 :
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;
}
elegant solution :-) nice work !
ReplyDeleteThanks for your post.
ReplyDeleteDoes your solution detect millimeters?
thanks again
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 :)
ReplyDeleteSensors are transforming the way engineers think about position sensing
ReplyDeleteDistance Sensor
Could you explain the circuit?
ReplyDeleteWell 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.
DeleteDo 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
ReplyDeletehttp://legwinskij.blogspot.sk/2011/03/arduino-ir-distance-sensor-update.html :)
Deletevery nice, could this sense a fast moving object
ReplyDeleteWell 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 :).
DeleteOn witch program do you need to upload the procesing code
ReplyDeleteProcessing is the name of language heres IDE http://processing.org/
DeleteHi, i would like to know how you made this equation: (((value2+15.0)/(value2*(value2+100.0)))*2000.0)
ReplyDeleteWell tha was a long time ago, but basic principle should be (1/value)*1000 then I was just tinkering with the numbers
DeleteNo 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.
ReplyDeleteHello, 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?
ReplyDeletethx
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
Deletei 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
DeleteHI, 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
DeleteI 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
Deletehow to connect a dc motor please give me the code
ReplyDeleteThis comment has been removed by the author.
ReplyDelete