Reflective detection under *controlled lighting* should be possible with off the shelf solutions from Keyence, Banner and the like.
A better solution may be a proximity sensor as it will detect the metal under any lighting conditions. It will just depend on the range you are able to permit the sensor.
All of these solutions are off the shelf, industrial sensors that can be wired into your PLC or DCS. There is absolutely no reason to roll your own solution unless you want the fun of doing it.
You will find most prox sensors to be much less expensive to implement than optical solutions.
This should be achievable under uncontrolled conditions with a little signal processing. Use an emitter-detector pair (an IR LED and a photodiode, for example), and modulate the emitter at a known frequency, say a few tens of kHz. Apply a synchronous detector to the signal from the photodiode, and you can essentially remove the effect of ambient light, even under harsh flickering conditions.
You can do this in analog using a few opamps and a SPDT analog switch, as per this
ADI appnote. I've made a little LTSpice simulation showing this approach:
Here's the signal we expect to see, which is constant light that varies due to reflectivity (I used a sinusoid for this because I'm unimaginative):
Note that because there wasn't an easy way to vary the gain across the optocoupler in the sim (which is what the reflectivity would be doing in real life), I just stuffed that sinusoid on top of the LED supply. In a real situation, that rail is constant and you're just transmitting a square wave with the LED.
Here's what we would expect to see coming out of the photodiode in a noiseless situation:
In this case, I've added a whole ton of low-frequency noise to the signal, in order to simulate the effect of random room light:
This may look a bit hopeless, but because we've chopped up our signal, we can easily demodulate it using the exact same carrier it was chopped with, lowpass it, and yank the original signal back out:
LTSpice file is attached to this message if you want to give it a try. I stuck to models that ship with LTSpice XVII, so you should be able to use it with the default install.
To improve this situation further, I would stick to infrared LEDs and get a photodiode with an IR bandpass window, so that you physically remove the influence of visible light.
While the analog solution I've detailed above is certainly fun, there's no reason you couldn't do it in DSP instead on a microcontroller
. There are also chips you can buy which do the job for you - here's a
classic ADI part.
If you wanted to get *really* fancy, you could do something totally nuts like modulate the LED using a direct-sequence spread spectrum chip code and then perform synchronous detection in software to detect it. That would slightly increase your susceptibility to wideband noise (depending on how much you spread) but would also make you essentially immune to light that flickers at your carrier frequency.