We used one in 2012 for detecting the balls in our hopper.
It was a sharp IR sensor.
Java code was something like this
Code:
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...