I built this Arduino conveyor sorter to classify boxes by height, and most of the less obvious parts of the code are there to solve problems I ran into while building it. This post walks through each mitigation, what it addresses, and the published background behind it.
Code and documentation: https://github.com/mosalah34/box-sorter-arduino
What the sketch does
The sketch drives a belt motor through a driver with direction inputs (IN1, IN2) and an enable/PWM input (ENA) at fixed speed 80 of 255. Two ultrasonic trigger/echo sensors watch for a box: one above the belt, one at the side. A servo on pin 9 moves a gate between 0 and 160 degrees. A 16x2 I2C LCD at address 0x27 shows status. The sketch does not fix specific part models, so any model named below is a common choice that fits these interfaces. A published example of the same pattern, with an Arduino Uno, an HC-SR04, a servo and an I2C LCD, is the IEOM 2024 paper "Automatic Product Counting & Sorting by Conveyor Belt" (https://ieomsociety.org/proceedings/bangladesh2024/217.pdf).
Problem 1: ultrasonic readings are noisy
Sources note that HC-SR04 modules return missing or wildly wrong readings: soft or angled surfaces lose the echo, and random large values appear from earlier pings or far walls. UMass's Physical Computing Makerspace page recommends waiting 60 ms or more between pings and using a median of five readings (https://make.cs.umass.edu/uc/ultrasonic.html). An Arduino Forum thread on 2000 cm+ outliers also points to a median filter (https://forum.arduino.cc/t/how-do-i-remove-outliers-in-the-data-provided-by-the-hc-sr04-ultrasonic-sensor/1279240/2).
In the sketch: readDistance uses a 25 ms echo timeout and returns 0 for anything under 2 cm or over 200 cm. The measurement takes 5 top readings 100 ms apart, sorts them, takes the median, drops readings more than 0.35 cm from it, and requires at least 4 accepted. Otherwise it shows "Unstable Read" and moves on instead of guessing.
Problem 2: false triggers
One bad echo should not stop a belt. The sketch requires both sensors to see a change: the top sensor 0.8 cm closer than the 7.0 cm empty value, and the side sensor 0.8 cm closer than a median baseline taken at startup. It needs 2 confirmations in a row. If the side baseline cannot be read (fewer than 5 valid out of 9), it stops with an error instead of running uncalibrated.
Problem 3: vibration and a moving target
A box measured while the belt moves gives unstable values. The sketch stops the motor, waits 250 ms, then measures. After classification it waits for both sensors to read empty again before accepting the next box, which prevents one box being counted twice.
Problem 4: borderline sizes
Distances near a threshold flip between classes. The sketch uses a dead band: 3.4 cm or less is large, 3.6 cm or more is small, and the range between is not classified as either. A caution on the display: the "Height" shown is the sensor-to-box-top distance. Real box height is the sensor's mounting height above the belt minus that reading.
Problem 5: power and wiring (what sources warn about)
These are common failures reported in guides, not claims about this build.
- Servo power. Arduino's help page says servos can draw more than the board supplies and may need a separate supply, with the grounds of the board and the external supply connected (https://support.arduino.cc/hc/en-us/articles/360017053760-Troubleshoot-servo-motors).
- Motor driver. For an L298N-style module, a missing common ground is a frequent cause of a motor not turning. If PWM drives ENA, the ENA jumper must be removed and ENA wired to a PWM pin. The module also drops about 2 V, so low PWM values can make a motor only hum (https://simtrok.com/en/errors/l298n-motor-not-turning). Relevant here because the sketch's speed value of 80/255 is low; whether it works depends on the motor, supply and driver, which this article does not claim to know.
- Sensors. HC-SR04 modules need 5 V and Trig/Echo wired to the right pins, per the UMass guide above.
Limits
This post does not report measured accuracy, throughput, or hardware validation. The thresholds (3.4 / 3.6 / 7.0 cm) are values in the sketch and would need calibration for any physical setup.
Sources
- IEOM 2024: Automatic Product Counting & Sorting by Conveyor Belt, https://ieomsociety.org/proceedings/bangladesh2024/217.pdf
- UMass Physical Computing Makerspace, Ultrasonic Distance Sensor, https://make.cs.umass.edu/uc/ultrasonic.html
- Arduino Help Center, Troubleshoot servo motors, https://support.arduino.cc/hc/en-us/articles/360017053760-Troubleshoot-servo-motors
- Simtrok, L298N DC motor does not turn, https://simtrok.com/en/errors/l298n-motor-not-turning
- Arduino Forum, HC-SR04 outliers, https://forum.arduino.cc/t/how-do-i-remove-outliers-in-the-data-provided-by-the-hc-sr04-ultrasonic-sensor/1279240/2

