How to set up ultrasonic and IR sensors for object detection on a conveyor

Based on contributions by Okay.

Ultrasonic and IR (infrared) distance sensors are commonly used to detect boxes on a conveyor belt, for example to trigger a conveyor stop or tell a robot when to pick up or place an object. This guide covers wiring, basic Arduino code, and testing for both sensor types.

What you need

  • Ultrasonic sensor (e.g. HC-SR04)
  • IR sensor (e.g. GP2Y0A21YK0F)
  • Arduino (or another microcontroller with digital/analog I/O)
  • Breadboard and wires
  • 5V power supply for both sensors
  • Boxes of different sizes to test with

Steps

1. Understand the two sensor types

Ultrasonic sensors emit high-frequency sound pulses and measure how long the echo takes to return, converting that time into a distance. They work well for irregularly shaped or transparent objects and are suited to longer-range detection, object counting, or level measurement.

IR sensors emit infrared light and measure the reflection. They respond quickly and are best for opaque, non-reflective surfaces at shorter range — useful for edge detection and more precise positioning.

2. Wire the ultrasonic sensor

  • VCC → 5V
  • GND → Ground
  • Trig pin → a digital pin (e.g. pin 9)
  • Echo pin → another digital pin (e.g. pin 10)

Double-check the wiring before powering it on.

3. Wire the IR sensor

  • VCC → 5V
  • GND → Ground
  • OUT pin → an analog input pin (e.g. A0)

The IR sensor’s output is analog, so it must be read through an analog input pin.

4. Program the ultrasonic sensor

const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;  // Speed of sound: ~0.034 cm/µs, divided by 2 for the round trip
  Serial.println(distance);

  delay(1000);  // Wait 1 second before the next reading
}

This sends a pulse, waits for the echo, and prints the calculated distance (in cm) to the Serial Monitor.

5. Program the IR sensor

const int irPin = A0;
int irValue;

void setup() {
  Serial.begin(9600);
}

void loop() {
  irValue = analogRead(irPin);  // Read the raw analog value from the IR sensor
  Serial.println(irValue);
  delay(1000);
}

This prints the raw analog reading, which changes with the distance to the object. Convert it to a distance using your specific sensor’s datasheet curve if you need actual units.

6. Position and test on the conveyor

  • Mount the sensors where the boxes will pass. Ultrasonic sensors should point perpendicular to the box surface for the most accurate reading; IR sensors should point directly at the object.
  • Watch the Serial Monitor while boxes pass: for the ultrasonic sensor, check that the measured distance matches the real distance; for the IR sensor, check that the analog value changes consistently as a box gets closer or farther away.
  • Adjust sensor height/angle if readings are inconsistent or boxes are missed.

Related


Rewritten and consolidated (Sept 2026) from the original student how-to’s: How to set up and test Ultrasonic and IR sensors with an Arduino for object detection on a conveyor.