Blog Layout

HC-SR04 Ultrasonic Sensor

How to Use the HC-SR04 Ultrasonic Sensor

Introduction

Hi, I’m Lewis, and welcome to DIY Machines! In this guide, I’ll show you how to use the HC-SR04 ultrasonic sensor to measure distances and detect objects using an Arduino or another microcontroller.

Video

I created a detailed video dmeonstrating everything I explain on this page. It's very informative and I spent alot of time on it - so you should totally watch it. :)

What is the HC-SR04 Ultrasonic Sensor?

The HC-SR04 ultrasonic sensor is a compact module designed for measuring distances between 2 cm and 400 cm. It works by emitting ultrasonic pulses and measuring the time taken for the echo to return.

Physical Overview

  • Dimensions:~21 mm (H) × 45 mm (W) × 15 mm (D) (excluding pins)
  • Front: Houses the transmitter, receiver, and ceramic resonator
  • Back: Contains the microcontroller that generates ultrasonic pulses, processes echoes, and outputs the results
  • Components:
    • Ultrasonic transceiver IC: Drives the transmitter and processes received signals
    • Voltage regulator IC: Ensures stable voltage for reliable operation

How It Works

The HC-SR04 emits eight short ultrasonic pulses at 40 kHz. These pulses bounce off nearby objects and return to the sensor. By measuring the time delay between transmission and reception, the sensor calculates the distance to the object.

Pin Configuration

We'll go over the pins in more detail later, but very breifly you'll find the following:

  • VCC → 5V power supply
  • GND → Ground
  • TRIG → Used to trigger the ultrasonic pulse
  • ECHO → Outputs the measured distance as a signal

Wiring for Demo

Setting Up the Sensor with an Arduino

Let’s connect the HC-SR04 to an Arduino Uno:

  1. Connect VCC to the 5V pin on the Arduino.
  2. Connect GND to GND.
  3. Connect TRIG to digital pin 9.
  4. Connect ECHO to digital pin 10.

Uploading the Code

Download the code and paste it into the Arduino IDE. Select your microcontroller board and upload the sketch.


Comments:


By Lewis November 7, 2024
Elegoo Mars 5 Ultra - 7" 9k AI Resin Printer
By Lewis June 27, 2024
3D Print and add some Resin to create some amazing pieces of stained 'glass'.
By Lewis Aburrow May 1, 2024
My Patreons sent in some questions. Because I'd asked them to do this - I thought I'd better write out answers....
By Lewis Aburrow May 1, 2024
CRAZY COASTERS with a TWIST! Laser or 3D Print some DIY Magic.
By Lewis April 2, 2024
KINETIC COASTERS with a TWIST! Laser or 3D Printable
By Lewis January 20, 2024
How To Make Neon LED Signs | Low Cost with Alexa, Hue & WiFi Controls
By Lewis November 15, 2023
A fast and reliable, multi-material, dual print headed printer with a few tricks up its sleeve...
By Lewis September 29, 2023
Build your own Super Smart Magic Mirror
By Lewis July 19, 2023
How to upgrade your desk to include smart responsive LED lighting and 3D printed upgrades to make your life easier!
By Lewis June 23, 2023
We review Elegoo's new computationally superior speed demons... and wow they're fast!
More Posts
Share by: