# IR Sensors

Hello All!

How can I code a Sharp IR sensor in Labview so that it accurately measures distance and changes in distance.

We used one in 2012 for detecting the balls in our hopper.
It was a sharp IR sensor.

Java code was something like this

``````
AnalogChannel ballDetector = new AnalogChannel(channelNumber); //where channelNumber is the port you've plugged

...

public double getBallDetectorUpper(){
double sensorVoltage = ballDetector.getVoltage();

if(sensorVoltage > 4.0 || sensorVoltage < 0.0){        //dont add in bogus data points
sensorVoltage = 0.0;
}
return sensorVoltage;
}

``````

Our code was a little more involved than that, but this is the general idea.

If you wanted to convert the voltage measured to a value in inches, there is usually a curve in the datasheet for the sensor which correlates voltages to distance. This can be used to generate an equation to convert voltage to distance.
We found that this wasn’t necessary, at least for our uses in 2012. If the voltage was below a certain level a ball was considered present, otherwise a ball was not present. No need to determine the distance away in inches…

What you didn’t say was the model of the Sharp sensor. But don’t even bother telling us that until you’ve taken that model number and put it into Google and found a spec sheet. That will tell you what kind of output it gives you. It may answer your question, or it may just let you ask a more specific question that people can give you help on.

Many such sensors give an analog output that is proportional to the distance. The spec sheet will give you a formula or a chart that shows the voltage that corresponds to a distance.

Good news is you have analog inputs on the Crio. They accept 0-5 volts. If the sensor also gives an output of 0-5 volts, you’re good. If it’s more, say 0-10 volts, you will need to scale it. That can be done as simply as a voltage divider with a couple of resistors.

If the sensor has some other output, say serial or I2C, you may be up to programming it or not. I don’t know you level of programming ability.

But the first thing you need to know is what you’re up against. The spec sheet will tell you that.

If it’s the Sharp 2D120X IR reflective sensor here’s a snippet of Labview code that I used on our trainer. I set the threshold to 2 volts which corresponds to a object being sensed at about 7 cm from the sensor.

That was added a few hours after the original post.