HomeRaspberry Pi SensorsSonar HC-SR04 ultrasonic ranger for a Raspberry PI & Arduino

Sonar HC-SR04 ultrasonic ranger for a Raspberry PI & Arduino

Connect HC-SR04 to Raspberry Pi and get readings with Python

What does it do?

Sonar HC-SR04 sensor issues an ultrasonic pulse to determine a distance from the board itself. Sensors placement limits the min range to 2cm, and it is possible to detect objects as far as 400cm away (4m). The output is generated to the GPIO21 using the code linked below and the command to check for the distance is issued on GPIO2.

Tech details:

  • Working Voltage DC 5 V
  • Working Current 15mA
  • Working Frequency 40Hz
  • Max Range 4m
  • Min Range 2cm
  • Measuring Angle 15 degree
  • Trigger Input Signal 10uS TTL pulse
  • Echo Output Signal Input TTL level signal and the range in proportion
  • Dimension 45*20*15mm

Connectivity:

4 pin connector, pins as follows:

  • VCC: 5V Supply
  • TRIG: Trigger Pulse Input
  • ECHO: Echo Pulse Output (5v)
  • GND: 0V Ground

Echo Pulse Output requires Voltage to be stepped down to 3.3V to work with Raspberry and prevent damage.

How to use it?

Apart from power (5V Vcc and GND) you will need 2 GPIO pins to use the HC-SR04 ultrasonic sensor with a Raspberry Pi. Triggering pin (TRIG) sends a signal to the module to measure the distance which is returned back via ECHO pin.

Project Download

Download project files here. Bear in mind that Patreon supporters have early access to project files and videos.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

M5Paper

Programable, ESP32 based awesome dev platform with 4.7 e-ink display by M5Stack

More HATs

client-image
client-image

Argon One M.2

Enclose Raspberry Pi 4 inside this great case with custom I/O, cooling and GPIO and M.2 SSD support

More cases on

client-image
client-image

Best Raspberry Pi Projects

How to use Raspberry PI as WOL (wake on lan) server

0
While you could wake up your PC from a mobile directly, having a dedicated server capable of doing so is the best solution. The reason is simple. You can hook up as many devices as you wish with a single endpoint. This is why Raspberry Pi is perfect for this.

How fast Raspberry Pi NAS is?

0
Let's see how fast Raspberry Pi NAS really is?

Argon18: Argon ONE SSD modification

0
Argon One case just got better - now you can boot it from USB without ruining the design thanks to Argon 18: Argon One SSD modification

Slow Internet Warning

0
From time to time my Internet grinds to a stop. Since Raspberry Pi 4 comes with a 1Gbps Ethernet, I decided to take advantage of it and create a reporting system in NodeRED that will monitor and report when the ISP is not keeping the contractual agreements. Works with Alexa, Google Home, Android and Windows 10.

HOW TO...

C/C++ and MicroPython SDK for Raspberry Pi Pico on Windows

0
A guide to SDK toolchain for Raspberry Pi Pico and C/C++ , Micropython on Windows.

A comprehensive guide to Grafana & InfluxDB

0
How to use Grafana and InfluxDB on Raspberry Pi for IoT sensors in home automation

How to boot Raspberry Pi 4 from USB

0
How to set up and boot Raspberry Pi 4 from USB drive - headless guide.

Setting Raspberry Pi WiFi without keyboard or cables

0
Getting Raspberry Pi boards connected in seconds

Everything you need to start using JSON

0
It's JSON not Jason, just saying!