Digital output is just a triggered analog value (you can adjust the sensitivity with the blue trimmer), but you should specify "unstable". That sensor isn't made to read distances, but just a IR reflection, so if "unstable" means it doesn't give you a signal every time the black square comes under, if using DO you probably just need to adjust the sensitivity to match your requirement, and if using AO you must use your code to "trigger" the desiderd level.
Otherwise, post a better and more detailed description of what "unstable" means for you, together with your code and a schematic.
Remember, your description is for the light that we humans can see. IR is different! What is giving the black and the white colors? Is it paint? Usually black paint is a mixture of several colors, including black. White may absorb the IR light and appear black, If the paint is not applied evenly, the reflectivity will vary. IS the orientation of your boxes ALWAYS the same?
ambient light is present General light not measured
output values
In Black 200 to 450 and in white 300 900 during the analog read
sometime 200 to 400 and in white 600 to 900
Some time 50 to 200 in black
I think no, it is designed with AGC for sensitivity. In this application, a linear response is desired, and there is no need for extreme sensitivity, the sensing distance is very small. I was thinking more along the lines of a "chopper" circuit. Running at a fairly low frequency, not 38kHz.
You're right about the modules, though... I guess the IR LED is constantly on. You could disable it and drive a separate IR LED...
I was thinking, in order to avoid saturation from specular surfaces, the IR LED is not in a favourable orientation anyway. It should be positioned slightly to the side, pointing sideways into the sensing area. That will send the specular reflection away from the detector.
Generally you would turn on the LED, take a reading, turn it off, take another reading. Subtract and the result is compensated for the ambient light.
@PA3040 hasn't been very forthcoming with details. It sounds like this equipment already exists, yet we are denied a close look at it.
Bend the IR and photo-transistor parts (vertically and horizontally) a little bit so that they aim at an imaginary dot at the center of your white box that's placed 2.5 cm away ...
Now adjust the sensitivity trim pot so that the analog reading is at 50% (2.5V or adc 512).
This will give maximum response for white to black detection.
Use hysteresis for your detection method, for example (adjust as necessary):
if the adc value goes above 400, then white box is detected
if the adc value goes below 350, then black box is detected
Try leaving the IR LED turned on for a bit longer. I discovered why the IR LEDs are black. I have a trail camera with clear IR LEDs. Most nighttime pictures of animals show the animal looking at the camera. One night I discovered why that was happening. I triggered the camera and could see the IR LEDs glowed red before the light changed to invisible IR. The animals were reacting to the red flash.
If your IR LED does the same and is opaque to red light, you will not be aware of the initial red light. But you will need to wait for the IR light to begin. So try various delays between turning the LED on and turning it off.
Delay is no problem, we are not in a hurry. However, you aren't following the forum guidelines that explain what is necessary to post, in order to receive the best help with your project. Basically, you are posting few sentence answers to questions and only a fraction of the technical information that is required from you. I looked back in the thread and see that nobody actually referred you to it, so here it is now. Please read it, and post the appropriate project descriptions, complete code and schematics that are outlined there: