How to use Ultrasonic Sensor HC-SR04 with Raspberry Pi

In this project we connect an HC-SR04 an Ultrasonic Sensor to Raspberry Pi.
We use Python Threading Library to read incoming Data.
The Data is shown in Graphic by using Tkinter Sprite Library.

  1. On Raspberry Pi Create a directory
  2. downloadTest_HC-SR04 and unzip them in that folder
  3. On Raspberry Pi under Menu Programming select Thonny Python IDE
  4. from menu file open file
  5. Connect Serial Device to Raspberry Pi
  6. run the Python file

A basic ultrasonic sensor consists of one or more ultrasonic transmitters , a receiver, and a control circuit. The transmitters emit a high frequency ultrasonic sound, which bounce off any nearby solid objects. Some of that ultrasonic noise is reflected and detected by the receiver on the sensor. That return signal is then processed by the control circuit to calculate the time difference between the signal being transmitted and received. The distance wird calculated base on time difference of Transmit and receive Time and sound speed (about 34300 meter per second).

Time Difference = 2 *Distance (in meter) / 34300 (meter per second sound speed) or

Distance = 17150 * Time Difference

HC-SR04 Specifications

  • Working Voltage: DC 5V
  • Working Current: 15mA
  • Working Frequency: 40Hz
  • Max Range: 4m
  • Min Range: 2cm
  • Measuring Angle: 15 degree
  • Trigger Input Signal: 10µS TTL pulse
  • Echo Output Signal Input TTL lever signal and the range in proportion
  • Dimension 45 * 20 * 15mm
  • Walk Through code
    • we use a Thread (HC_SR04_Thread ) to set Trigger and wait for Echo and measure the time difference
    • using tkinter Library for Graphics


download:  Test_HC-SR04

Leave a Reply

Your email address will not be published. Required fields are marked *