Calibrate on your own surface. Read the sensor over black and over white, then set the threshold halfway between.
What the message means
Reflective sensors depend on distance, the surface and ambient light. A fixed number copied from a tutorial may not suit your track.
Fix it in this order
- Mount the sensor a few millimetres above the surface, as the datasheet suggests.
- Read and print the value over black and over white.
- Set the threshold between the two readings, or scale readings between the minimum and maximum.
- Shield the sensor from sunlight and bright lamps.
- Re-calibrate when the lighting or the surface changes.
- Use analog readings, not only digital, for smoother control.
Why it happens
A reflective sensor measures how much light comes back from the surface. That amount depends on how dark the surface is, how far the sensor sits from it, and how much ambient light reaches the sensor, so the same line gives different numbers on a different floor or in different lighting.
Picking a number halfway between your own black and white readings puts the threshold where the two cases are furthest from it. A reading close to the threshold flips back and forth with a little noise, so smoothing the reading or using separate on and off thresholds can steady the robot.
If you see this, do this
| You see | Do this |
|---|---|
| Same reading on both | Height or surface problem. |
| Works indoors, fails outdoors | Ambient light. |
Related
line follower oscillates, PID for a line follower.
Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.
Why this happens
A reflective sensor measures how much infrared light returns to its detector. A dark surface returns little, a light surface returns more, but the exact numbers depend on sensor height, the surface finish, the sensor’s own LED current and the ambient light, including sunlight and lamps, which contain infrared. A fixed threshold from a tutorial describes somebody else’s track.
The analog reading is a number from the converter. A 10-bit converter, as on an Uno, gives 0 to 1023, and a 12-bit one, as on an ESP32, gives 0 to 4095. A good threshold sits in the middle of the gap between your black and white readings. Normalising each sensor between its own minimum and maximum also removes differences between individual sensors, so one array can be compared sensor to sensor.
How to confirm the cause
- Hold the robot with the sensor at its working height over white and print 100 readings. Do the same over black. Write down the lowest and highest values for each.
- Check the gap between the two groups, and the spread inside each group. If the spread within one group is as big as the gap, noise or height is the problem.
- Lift the sensor by a few millimetres and watch the numbers. A large change means the height matters and must be fixed in the mounting.
- Cover the sensor from overhead light with your hand. If the values change, ambient infrared is affecting it.
- Check the direction. Some modules read higher on black, and others higher on white.
Worked example
Taking the threshold from the robot’s own readings by sweeping it over the line and the floor for three seconds, and then normalising a reading:
int lo = 1023, hi = 0;
unsigned long t0 = millis();
while (millis() - t0 < 3000) { // move the sensor across line and floor
int v = analogRead(A0);
lo = min(lo, v); hi = max(hi, v);
}
int threshold = (lo + hi) / 2; // e.g. (180 + 820) / 2 = 500
// scaled value 0..1000: (x - lo) * 1000 / (hi - lo)
// x = 300: (300 - 180) * 1000 / 640 = 187Common mistakes
- Calibrating while the sensor is over only one colour.
- Setting the threshold right next to one of the readings, so noise flips it back and forth.
- Setting the sensor too high above the surface.
- Calibrating indoors and running outside in bright light.
- Leaving the potentiometer on a digital sensor module at a random setting.
If this did not fix it
- Hysteresis: using one threshold for turning on and a slightly different one for turning off prevents flicker near the boundary.
- Glossy tape reflects differently from matte tape; test with the real track material.
- Electrical noise from the motors on the analog reference; add a capacitor near the sensor supply.