Ardan si robot penghalang is an Arduino-based autonomous rover that uses ultrasonic time-of-flight sensing to detect physical barriers and dynamically alter its DC motor drive states to prevent collisions. In a real circuit, implementing this concept changes your microcontroller from a simple open-loop output device into a closed-loop feedback system, requiring precise timer interrupts, pulse measurement, and PWM modulation rather than static digital highs. Beginners frequently confuse ultrasonic time-of-flight (ToF) obstacle avoidance with infrared (IR) reflective proximity sensing, assuming both behave identically under varying lighting and material conditions—a mistake that leads to immediate failures when the robot encounters black surfaces or bright ambient sunlight.

The Physics and Timing of HC-SR04 Ultrasonic Sensing

The core of the Ardan si robot penghalang platform relies on the HC-SR04 ultrasonic transceiver. This module operates by emitting a 40 kHz acoustic burst and listening for the echo. Because the speed of sound in dry air at 20°C (68°F) is approximately 343 meters per second, we can calculate the exact distance to an object by measuring the time it takes for the pulse to return.

Critical Timing Constraint: The speed of sound is 0.0343 centimeters per microsecond. Because the sound wave must travel to the obstacle and bounce back, the total transit time is exactly double the one-way distance. You must always divide your final calculation by 2.

Worked Numeric Example: Calculating Distance from Pulse Width

Let us calculate the exact distance when the Arduino pulseIn() function returns an echo duration of 5,830 microseconds (5.83 ms). According to the Arduino pulseIn() reference, this function measures the length of a pulse on a pin, which perfectly matches the HC-SR04 echo pin behavior.

Formula:
Distance = (Time × Speed of Sound) / 2

Calculation:
Distance = (5830 μs × 0.0343 cm/μs) / 2
Distance = 199.969 cm / 2
Distance = 99.98 cm

If your robot is traveling at 0.5 meters per second, a 100 cm detection range gives your microcontroller exactly 2 seconds of lead time to execute a deceleration or turning routine before impact. However, if the pulseIn() function times out (default timeout is 1 second, but practically you should set it to ~25,000 μs to cap the max range at roughly 4 meters), it returns 0, which your code must interpret as "no obstacle detected" rather than "obstacle at 0 cm".

Motor Drive Logic: Translating Distance to PWM

Where You Meet This in Practice

You meet this exact translation layer when your robot approaches a wall at full speed and needs to decelerate gracefully rather than slamming into it or burning out the plastic gears of your TT motors from a sudden, high-current direction reversal. Static digital logic (just turning pins HIGH or LOW) is insufficient here; you must map the continuous distance variable to a Pulse Width Modulation (PWM) duty cycle on the enable pins of your motor driver.

Most Ardan si robot penghalang kits use the L298N dual H-bridge motor driver. The Texas Instruments L298 datasheet specifies that the ENA and ENB pins accept PWM signals to control the average voltage delivered to the motors, while IN1, IN2, IN3, and IN4 dictate the polarity (direction).

Distance-to-PWM Mapping for Smooth Obstacle Avoidance
Measured Distance Robot Action Direction Pins (IN1/IN3) ENA/ENB PWM Value (0-255)
> 60 cm Full Speed Forward HIGH / LOW 255 (100% Duty)
30 cm - 60 cm Decelerate Forward HIGH / LOW 150 (Proportional)
15 cm - 30 cm Stop and Scan LOW / LOW 0 (Brake)
< 15 cm Reverse and Turn LOW / HIGH 200 (High Torque)

By implementing this proportional mapping, the robot's kinetic energy is managed safely. If you skip the deceleration zone and jump straight from 255 to 0 at 15 cm, the inductive kickback from the DC motors can cause voltage spikes on the 5V rail, potentially resetting your Arduino Uno R3 mid-maneuver.

Common Confusions and Edge Cases in Obstacle Avoidance

The most common error when building an Ardan si robot penghalang is confusing ultrasonic ToF sensing with IR reflective sensing. IR sensors (like the TCRT5000) bounce infrared light off a surface and measure the return intensity. They fail completely on black objects (which absorb light) and are easily blinded by sunlight. Ultrasonic sensors bounce sound waves, making them entirely immune to ambient light and object color. A black couch and a white wall will both reflect 40 kHz sound waves reliably.

However, ultrasonic sensors have their own distinct edge cases that you must program around:

  • Acoustic Absorption: Soft, porous materials like heavy curtains or foam acoustic paneling will absorb the 40 kHz pulse rather than reflecting it, resulting in a timeout (false clear path).
  • Specular Reflection (Angled Surfaces): If the robot approaches a smooth wall at a shallow angle (e.g., 15 degrees), the sound wave will bounce away from the receiver like light off a mirror, causing the sensor to read a false "clear" distance.
  • The 2 cm Blind Spot: The HC-SR04 cannot distinguish the echo from the initial trigger burst if the object is closer than 2 cm. If your robot gets pushed into a corner and the distance drops below 2 cm, the sensor will output erratic high values. Your code must include a fallback routine (like a physical bumper switch or a time-based dead-reckoning reversal) if the robot is stuck.

FAQ: Ardan Si Robot Penghalang Long-Tail Questions

Why does my Ardan si robot penghalang spin in circles instead of turning away?

This usually happens due to a mismatch in motor wiring polarity or an uncalibrated PWM deadband. DC motors require a minimum voltage to overcome static friction. If your turn logic sets one motor to PWM 100 and the other to 0, the "0" motor might still be dragging due to inertia, while the "100" motor isn't receiving enough voltage to actually spin (the L298N has a ~2V internal voltage drop). Fix this by setting the reversing motor to a negative direction with a PWM of at least 180, and ensure your left/right IN-pin logic matches your physical wiring.

How do I fix the HC-SR04 returning 0 cm or random 400 cm values?

Random 400 cm values indicate that the echo pulse exceeded your pulseIn() timeout, meaning the sound wave scattered and never returned. A hard 0 cm usually means the trigger pin didn't fire correctly or the echo pin is floating. Ensure you are pulling the Trigger pin LOW for at least 2 microseconds before sending the 10-microsecond HIGH pulse. Additionally, wire a 0.1 μF ceramic decoupling capacitor across the VCC and GND pins of the HC-SR04 to filter out high-frequency noise from the TT motors that can falsely trigger the echo pin.

Can I use an ESP32 instead of an Arduino Uno for Ardan si robot penghalang?

Yes, but you must adjust your voltage logic. The ESP32 operates at 3.3V logic, while the standard HC-SR04 and L298N expect 5V logic. While the ESP32 can usually read a 5V echo pulse if you use a simple voltage divider (e.g., 1kΩ and 2kΩ resistors) on the Echo pin, the 3.3V Trigger output might not reliably switch the HC-SR04. Use a logic level converter or power a specifically designed 3.3V-compatible ultrasonic sensor (like the RCWL-1601) to prevent brownouts and erratic timing on the ESP32's fast RTOS cores.

What is the maximum reliable speed for an ultrasonic obstacle robot?

For a standard 2WD rover using yellow TT gearmotors (approx. 200 RPM at 6V), the top speed is roughly 0.6 meters per second. At this speed, the robot travels 12 cm during the 200 ms it takes to complete one full sensor scan, pivot, and motor-reversal cycle. Therefore, your minimum safe stopping distance must be programmed to at least 25 cm to account for mechanical braking lag and the sensor's 60 ms acoustic transit time. If you upgrade to high-RPM N20 metal gearmotors, you must increase the scanning frequency and the safe stopping distance proportionally.