Skip to content
LeafTeaNeko edited this page Mar 12, 2020 · 2 revisions

Hardware: Generic HR SC04 Ultrasonic Ranging Module

Library: This sensor does not require a library.

OVERVIEW: Ultrasonic ranging module HC - SR04 provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules include ultrasonic transmitters, receiver and control circuit. The transmitter sends a high-frequency signal, when the signal finds an object, it is reflected, and the receiver receives it. The time between the transmission and reception of the signal allows us to calculate the distance to an object.

WIRING: The sensor is wired up to PINX and PINY on Raspberry Pi-zero (or Raspberry Pi-3B+) with both pins connected to any open pin on the Raspi board except power lines, ground or lines for I2C communication. It is also connected to 5V line from PIN2 for power and Ground to GND.

MECHANISM: The sensor uses the timing between the transmitted ultrasonic sound and received ultrasonic sound to calculate the distance to the detected object. The sensor is triggered using the TRIG pin which starts the transmitter to send out an ultrasonic sound. This is done by shifting the PIN connected to TRIG by setting it high: GPIO.output(TRIG, True). The Transmitter must be shut down by GPIO.output(TRIG, False) after transmission otherwise the receiver will be affected due to constant interference from sound transmission and reflection. We do this by setting the sensor to sleep time.sleep(0.00001), timing of the sleep and the reflected wave can be adjusted accordingly, please refer to the data sheet for more details. pulse_start = time.time() and pulse_end = time.time() are used to record the time the pulse was sent and when the pulse is received. The delay between the transmission and reception using the value of speed of sound in open air, we can determine distance by distance = pulse_duration * 17150

NOTE: It is possible for the sensor to output wrong data if the sensor is too close to the ground, this happens when the transmitted cone hits the ground just below the sensor instead of the object far way. It is advisable to use the sensor at an appropriate height from the platform (at least 5 cm off the platform). Please refer to the data sheet for the detection cone and angles.

HOW-TO-USE:

STEP 1: Mount the sensor on platform (Breadboard or PCB)

NOTE: It is possible for the sensor to output wrong data if the sensor is too close to the ground, this happens when the transmitted cone hits the ground just below the sensor instead of the object far way. It is advisable to use the sensor at an appropriate height from the platform (at least 2 cm off the platform). Please refer to the data sheet for the detection cone and angles.

STEP 2:

  • Connect the 5V line to the Vcc of the sensor Pin
  • Connect the GND line to GND of the sensor
  • Connect TRIGGER line to the PIN 7 of the Pi (Refer to Raspi pin input diagram)
  • Connect ECHO line to the PIN 11 of the Pi (Refer to Raspi pin output diagram)
  • Pass the echo line through a voltage divider with 1K and 2K resistors, pass the divider voltage to ECHO pin , refer to schematic for more details.

STEP 3: Set up the GPIO board type by using GPIO.setmode(GPIO.BOARD). Set up the input pin (echo) and output pin (triggering) by using GPIO.setup(TRIG, GPIO.OUT) and GPIO.setup(ECHO, GPIO.IN). Set up to ignore all warnings from the sensor about echo lines by using GPIO.setwarnings(False).

STEP 4: The process time for the ultrasonic sensors is 100ms, we must delay the sensor detection, so we must set the delay time to 0.001s.

STEP 5: Start the transmitter using GPIO.output(TRIG, True) and wait for the response from Echo, once received, process the time delay using pulse_start = time.time() and pulse_end = time.time()

STEP 6: Once the time delay has been established, multiply the result with 17150(speed of sound in cm/s). The final distance is calculated by sonar = round(distance+1.15, 2), we have to add 1.152(calibration) based on the detection range provided in the data sheet.

STEP 7: Clean all GPIO configuration once the process is done by using GPIO.cleanup()

STEP 8: Run the code and check output, the distance should be in centimetres.

STEP 9: Take caution in detecting ranges more than the max range in the data sheet, use appropriate timing intervals for more robust performance.

Clone this wiki locally