Line Detection Sensor Monitoring

This project uses a line detection sensor to detect the presence of a line on a surface. When the sensor detects a line, it sends a signal indicating detection. This information is then sent to the Serial Monitor for monitoring.

How It Works

A hunt sensor — the KY-032 and the very similar IR obstacle-avoidance modules — pairs an infrared emitter with a 38 kHz receiver on one small board, both facing the same direction. It detects objects by reflection rather than by break-beam: the emitter floods the area ahead, and anything close enough bounces light back into the receiver.

Using a modulated 38 kHz carrier rather than steady IR is what makes the module usable in daylight. The receiver is the same kind of filtered part used for remote controls, so ambient light and incandescent lamps are rejected.

Two potentiometers set the behaviour. One adjusts the emitter drive, effectively setting range; the other sets the comparator threshold. Range is typically adjustable between about 2 cm and 40 cm. The output is active LOW — it pulls to ground when an obstacle is seen.

The important limitation is that reflectivity, not just distance, decides detection. A white wall is seen far further away than black fabric, which may absorb enough IR to be nearly invisible. This sensor reports "something reflective is near", not a distance.

Components Needed

  • Arduino Uno
  • Infrared Obstacle (Hunt) Sensor module
  • Arduino Uno
  • USB cable for programming and power
  • Arduino Uno

Wiring to the Arduino Uno

Connect OUT to D2, VCC to the 5 V rail and GND to ground. Most carriers run happily from either 3.3 V or 5 V, which makes this one of the easier modules to move between boards.

Set range with the potentiometers while watching the onboard indicator LED. Place a target at the distance you want to trigger at, adjust until the LED lights, then remove the target and confirm it goes out. Calibrate against the least reflective surface you expect to encounter.

The Arduino Uno runs on 5 V logic, which matches the output swing of most hobby modules, so the signal pin connects directly with no level shifting.

Module pinArduino Uno pinFunction
OUTD2LOW when an obstacle is detected
VCC / +5VSupply (3.3–5 V)
GND / −GNDCommon ground
EN—Enable, jumpered on by default

Example Code

Obstacle detection with edge reporting, as used in a robot's avoidance loop. Upload it with the board set to Arduino Uno and open the Serial Monitor at 9600 baud.

Obstacle detection with edge reporting, as used in a robot's avoidance loop
const int OBSTACLE_PIN = 2;

bool blocked = false;

void setup() {
  Serial.begin(9600);
  pinMode(OBSTACLE_PIN, INPUT);
}

void loop() {
  bool nowBlocked = (digitalRead(OBSTACLE_PIN) == LOW);   // active LOW

  if (nowBlocked != blocked) {
    blocked = nowBlocked;
    Serial.println(blocked ? "OBSTACLE — stop and turn" : "path clear");
  }
  delay(20);
}

Applications

A infrared obstacle (hunt) sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:

  • Obstacle avoidance on small mobile robots
  • Table-edge and cliff detection, by aiming the sensor downward
  • Automatic taps, dispensers and bins triggered by an approaching hand
  • Object presence checks on a conveyor or in a chute
  • Simple line following, using the difference between reflective and dark surfaces

Working with the Arduino Uno

The Arduino Uno is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.

The Uno runs at 5 V, so most hobby sensor modules connect directly with no level shifting.

With only 2 KB of SRAM, avoid large buffers and prefer the F() macro for constant strings.

The single hardware UART is shared with the USB connection, so heavy Serial printing competes with uploads.

Arduino Uno characteristicValueWhy it matters here
Logic voltage5 VMatches most hobby modules directly
ADC resolution10-bit (0–1023)Sets how finely an analog reading can be resolved
Analog inputsA0–A5 (six channels)Determines how many analog sensors can share the board
PWM outputsD3, D5, D6, D9, D10 and D11Needed for brightness, speed and tone control
I²C pinsA4 (SDA) and A5 (SCL)Fixed by hardware — wiring copied from another board may not match
Interrupt pinsD2 and D3 onlyRequired for counting fast or asynchronous events
Seriala single hardware UART shared with USBMonitor runs at 9600 baud by default

Troubleshooting

Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:

  • Dark objects are never detected — black surfaces absorb infrared. Increase range, or choose ultrasonic sensing instead.
  • The output is permanently LOW — the threshold pot is turned too far, or the sensor is seeing part of the robot chassis. Check nothing sits in its field of view.
  • Range changes between indoors and outdoors — strong sunlight still degrades performance despite the 38 kHz filtering. Shade the sensor.
  • Two sensors interfere with each other — their beams overlap. Space them apart or enable them alternately in software.
  • Code written for an ESP board gives odd analog values — the Arduino Uno uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
  • An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Uno I²C is on A4 (SDA) and A5 (SCL).

Taking It Further on the Arduino Uno

Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Uno specifically:

The Uno’s shield ecosystem is its real advantage. Once the circuit works on a breadboard, a prototyping shield turns it into something permanent that still stacks with a data-logging or Ethernet shield without rewiring.

Logging to an SD card via a shield is the natural next step, since the Uno has no onboard storage and no network. Timestamp each reading with a DS3231 real-time clock so the log survives power cuts with correct times.

Because SRAM is limited to 2 KB, keep logged strings short and write them out immediately rather than buffering. Building a long String in memory is the most common cause of an Uno sketch that runs for hours and then freezes.

Notes and Practical Limits

This sensor gives a yes/no answer, not a measurement. If your project needs to know how far an obstacle is, use an HC-SR04 ultrasonic module or a Sharp IR distance sensor — both report distance, which this module cannot.

Mounting matters as much as calibration. Angle the sensor slightly downward on a robot so it sees obstacles rather than distant walls, and shield it from the robot's own indicator LEDs.