How to set up and test Ultrasonic and IR sensors with an Arduino for object detection on a conveyor

How to Set Up and Test Ultrasonic and IR Sensors for Box Detection

1. Introduction

This guide will walk you through the process of setting up and testing ultrasonic and infrared (IR) sensors for detecting boxes on a conveyor belt. These sensors are commonly used in automated packaging systems to trigger actions like stopping conveyors or instructing robots to pick up or place objects.


2. Understanding the Sensors

Ultrasonic Sensors

Ultrasonic sensors work by emitting high-frequency sound waves and calculating the time it takes for the echo to return after hitting an object. This time is then used to calculate the distance between the sensor and the object (such as a box).

  • Advantages: Works well for detecting irregularly shaped or transparent objects.
  • Typical applications: Long-distance detection, object counting, level measurement.

IR Sensors

IR sensors emit infrared light and detect the reflection that returns when it hits an object. The sensor’s output changes depending on the distance of the object from the sensor.

  • Advantages: Quick response time and suitable for detecting opaque and non-reflective surfaces.
  • Typical applications: Short-range detection, edge detection, and precision applications.

3. Components Needed

To set up and test the sensors, you’ll need the following components:

  • Ultrasonic sensor (e.g., HC-SR04)
  • IR sensor (e.g., GP2Y0A21YK0F)
  • Microcontroller (e.g., Arduino, Raspberry Pi)
  • Wires and resistors for connections
  • Breadboard for building the circuit
  • Power supply (5V for both sensors)
  • Boxes of different sizes for testing

4. Wiring the Sensors

Ultrasonic Sensor Wiring

For an ultrasonic sensor, here’s how to wire it to an Arduino:

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

Make sure to double-check the wiring to ensure correct connections.

IR Sensor Wiring

For an IR sensor, wire it as follows:

  • VCC to 5V
  • GND to Ground
  • OUT Pin to an analog input pin (e.g., A0 on Arduino)

The IR sensor outputs an analog signal that needs to be read through the analog input pins of the microcontroller.


5. Programming the Sensors

Ultrasonic Sensor Code

Here’s an example code for reading distance from an ultrasonic sensor using an Arduino:

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;  // Calculate the distance in cm
  Serial.println(distance);
  
  delay(1000);  // Wait 1 second before next reading
}

This code sends a pulse from the ultrasonic sensor, waits for the echo, and calculates the distance in centimeters. The results are printed to the Serial Monitor.

IR Sensor Code

For the IR sensor, use the following code to read its analog output:

const int irPin = A0;
int irValue;

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

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

This code reads the analog output of the IR sensor and prints the value to the Serial Monitor. The output will vary depending on the distance to the box.


6. Testing the Sensors

Setting Up the Test Environment

  • Place the sensors on a conveyor belt, positioning them where the boxes will pass.
  • Adjust the height and angle of each sensor to optimize detection. For example, ultrasonic sensors should be perpendicular to the box surface for best results, while IR sensors should face directly towards the object to get accurate reflections.

Running the Test

  • Monitor the sensor outputs using the Serial Monitor in the Arduino IDE.
  • For ultrasonic sensors, check if the measured distance corresponds to the actual distance of the box from the sensor.
  • For IR sensors, observe how the analog value changes as the box moves closer or farther from the sensor.
1 Like