A little curiosity. A whole lot of making.Robotics, CNC & connected ideas
robodatech.
← Learning library
Build & understand

Ultrasonic Sensor

An ultrasonic sensor sends a short sound pulse above human hearing range. The pulse reflects from an object and returns as an echo. The controller measures the time delay and converts it into distance.

SENSORTargetTransmitReturnMeasure a round tripdistance = speed × time ÷ 2
Concept illustration. Check the component datasheet before wiring.
Try it yourself

Change an input.
See what follows.

A simplified learning model. Values describe the model, not a connected robot.

Working Principle

An ultrasonic sensor sends a short sound pulse above human hearing range. The pulse reflects from an object and returns as an echo. The controller measures the time delay and converts it into distance.

Step by Step

  1. Trigger pin sends a short pulse.
  2. The transmitter creates an ultrasonic sound wave.
  3. The wave reflects from an object.
  4. Echo pin reports how long the sound took to return.
  5. Distance is calculated from time and speed of sound.
SENSORTargetTransmitReturnMeasure a round tripdistance = speed × time ÷ 2
Concept illustration; component appearance and scale are simplified.

Practical learning notes

Key idea

HC-SR04 sends ultrasonic sound near 40 kHz. Arduino sends a 10 microsecond trigger pulse and measures the echo pulse time.

Use in robot

Good for front obstacle distance. It is not a camera and cannot identify object color, shape, or material.

Safe wiring

VCC is 5V, GND is common ground, TRIG is an Arduino output, and ECHO is an Arduino timing input on the Nano.

Classroom check

Measure the same object at 10 cm, 20 cm, and 30 cm. The echo time should increase almost linearly.

Pin Details and Wiring

HC-SR04 PinConnect ToPurpose
VCCArduino Nano 5VPowers the ultrasonic module.
TRIGNano D4Controller sends a 10 microsecond start pulse.
ECHONano D7Sensor returns a HIGH pulse; pulse width represents travel time.
GNDNano GND and battery/module common GNDShared reference for all signals.
Classroom formula: distance in cm = echo time in microseconds / 58. More exact: distance = echo time x 0.0343 / 2.
Connect a sensor to a controllerControllerSensorRequestResultRated supplyCommon GND
Signal and power overview. Exact connections are listed below.
Connection reference for this build
  • Controller and wiring connection
  • +5V rail
  • Common GND rail
  • Arduino Nano
  • D4 -> TRIG output
  • D7 <- ECHO input
  • 5V -> VCC
  • GND -> GND
  • HC-SR04
  • VCC
  • TRIG
  • ECHO
  • GND
  • D4 trigger pulse
  • D7 echo timing
  • pulseIn()

Speed of Sound and Propagation

At about 20 degrees C, sound travels near 343 m/s, which is 0.0343 cm per microsecond. The sensor measures round-trip travel, so the controller divides by 2 to get one-way distance.

Propagation means the wave spreads outward through air, reflects from a surface, and returns. Hard flat objects reflect strongly; soft cloth, angled surfaces, and small objects can reflect weakly or away from the receiver.

Timing carries informationTRIGECHO10 µsRound-trip durationIllustrative timing; horizontal scales differ.
Concept illustration; component appearance and scale are simplified.

Drawbacks and Safe Use

Blind zone

Very close objects can be unreliable. Keep a practical minimum distance of about 2 to 4 cm.

Surface angle

Slanted surfaces may bounce sound away, so the echo may not return to the receiver.

Soft materials

Foam, cloth, hair, and curtains absorb sound and reduce echo strength.

Cross talk

Two ultrasonic sensors firing together can hear each other. Trigger them one at a time.

Temperature

Sound speed changes with air temperature, so precision measurements need calibration.

Not a camera

The sensor gives distance in one direction. It does not identify object shape or color.

Take the next step

Where this is used

Explore real robotics applications, from mobile robots to inspection and joint motion. Define a measurable target and test one change at a time.

Moving to a 3.3 V controller

The legacy wiring above is for a classic 5 V Nano. A typical HC-SR04 ECHO signal is 5 V: use suitable level shifting for a 3.3 V controller. Treat a timeout as an invalid measurement and choose a conservative response.

Good ideas start here.

Free robotics lessons, CNC build notes and product updates. Confirm your email to join. Unsubscribe anytime.