Sensor

๐Ÿ“ก HSโ€‘SR04 Ultrasonic Sensor

๐Ÿ“Œ Description

The HSโ€‘SR04 ultrasonic sensor is like the robotโ€™s ears that measure distance. It sends out sound waves (too high for humans to hear) and waits for them to bounce back. By timing how long it takes, the sensor knows how far away an object is.

๐ŸŽฏ Use

In our projects, the ultrasonic sensor is used to:

  • Measure how far objects are from the robot.
  • Help the robot avoid obstacles.
  • Make smart decisions, like stopping before hitting something.

๐Ÿ‘‰ Think of the ultrasonic sensor as the robotโ€™s tape measure โ€” it tells the robot how far things are.

Module parameters

Pin name Description
VCC VCC (Power Input Cathode)
TRIG Control side (Rx)
ECHO Receiver (Tx)
GND GND (Power Input Negative)
  • Supply voltage: 3.3V/5V
  • Connection: PH2.0 terminal wire
  • Installation method: screw fixing

๐Ÿ–ฅ๏ธ Code Explanation

import machine
import sonar
import time

while True:
    print(sonar.Sonar(27,14).checkdist())   # Measure distance
    time.sleep_ms(200)                      # Wait 0.2 seconds

Step by Step

  1. import machine, sonar, time โ†’ Tools for pins, ultrasonic sensor functions, and timing.
  2. sonar.Sonar(27,14) โ†’ Sets up the sensor: pin 27 sends the sound signal (trigger), pin 14 listens for the echo.
  3. .checkdist() โ†’ Measures the distance to the nearest object.
  4. print(...) โ†’ Shows the distance on the computer screen.
  5. time.sleep_ms(200) โ†’ Waits 0.2 seconds before measuring again.

๐Ÿ‘‰ Together, this program makes the sensor report the distance every 0.2 seconds.


โœ… Conclusion of the Test

  • If the program prints numbers that change when you move an object closer or farther, the sensor is working correctly.
  • This test shows how the robot can measure distance and use that information to avoid obstacles.
  • Later, instead of just printing, the robot could stop, turn, or slow down when something is too close.
On this page