Ir sensor with unstable reading

Dear All
Have nice day

I am writing a program to detect black box and white box (Box size is 10cm) using Ir sensor
see below picture for Ir sensor that I used

I tried both DigitaRead and AnalogRead and values are receiving but the values are coming from the sensor is not stable

Please help me to make the values keep stable or any other sensor that I can use for my project

Or is there any special code that I can use

image

Please help
Thanks in advanced

How far from the sensor is the box?

How much ambient light is present?

This type of sensor is designed to detect objects at very close distance and, ideally, shielded from ambient light

Ambient light contains IR light also and this can interfere with the sensor.

How unstable is the signal? Are the min and max analog readings for the white box different to the min and max readings for the white box?

Perhaps you can use Serial Plotter to show the forum what values you can see when the black box is present, the white box, and no box.

1 Like

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.

1 Like

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?

1 Like

Distance from the box is around 5cm

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

Please advise

orientation of boxes always same
image

modulate the IR to block it

you ignored the request for code...
and schematic...

1 Like

I don't think that is possible with this type of sensor.

The IR led would need to be driven direct from an Arduino output or other circuit that can produce modulation signal at the correct frequency.

The sensor would have to be one of those used in IR remote control receivers.

1 Like

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.

2 Likes
  1. 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 ...

  2. Make the background color grey

  3. 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.

  4. 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

@anon57585045
Sorry for late reply
please see the code that we used

int x;
void setup() {

Serial.begin(9600);
}

void loop() {
  pinMode(A0, INPUT);
  while (digitalRead(A0)) {
    x = analogRead(A0);
   // Serial.print(x);
  }
}

This is the one method that we used in code
pinMode(A0,INPUT); also put to the Setup but result is same

Please advise
Thanks in advnaced

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.

I'm still waiting for project details.

@anon57585045
Sorry for delayed reply

This is line following robot with object detection and color detection

Object detection using Ultrasonic sensor

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:

1 Like

Quite simple, but try this instead, for a better testing and paste here the result (together with the square movement you made):

int x;
void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
}

void loop() {
  x = analogRead(A0);
  Serial.println(x);
  delay(500);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.