Phone

    00852-6915 1330

How to Wire Ultrasonic Sensor-HC-SR04-with Raspberry Pi3?

  • Contents

Ⅰ Introduction

In this project, we will use hardware ultrasonic sensor and  Raspberry Pi 3, Software

Python code. Not everyone is familiar with ultrasonic sensor and Raspberry Pi3. Therefore, in the front part ,we will introduce some basic knowledge about ultrasonic sensor and Raspberry Pi3. This is conducive to understanding the project better. And then, we will have a look at the project of wiring Ultrasonic Sensor  (HC-SR04) with Raspberry Pi3

Catalog

Ⅰ Introduction

Ⅱ Ultrasonic Sensor Related Video

Ⅲ Basic Guide to Ultrasonic Sensor 

3.1 What is an ultrasonic sensor?

3.2 How Ultrasonic Sensors Work?

3.3 Using Multiple Sensors & Avoiding Disruption

3.4 How are Ultrasonic Sensors Used?

Ⅳ Basic Guide to Raspberry Pi3

4.1 What is  Raspberry Pi3?

4.2 What Is the Raspberry Pi3 Capable of?

4.3 How do I Get Started With the Raspberry Pi 3?

4.4 How Is the Raspberry Pi 3 Different From Its Predecessors?

Ⅴ Ultrasonic Sensor (HC-SR04) + Raspberry Pi3

5.1 Hardware

5.2 Wire Setup

5.3  Breadboard 

5.4 Software

Ⅵ FAQ

 

 

 

Ultrasonic Sensor Video Description:

Connecting the Ultrasonic Sensor( HC-SR04) to the Raspberry Pi to measure distance. Equipment you need One 1 kilo-Ohm resistor One 2 kilo-Ohm resistor 8 Female-Male Jumper Wire

 

Ⅲ Basic Guide to Ultrasonic Sensor 

3.1 What is an ultrasonic sensor?

An ultrasonic sensor is a device that uses ultrasonic sound waves  to determine the distance between two objects.

An ultrasonic sensor employs a transducer to send and receive ultrasonic pulses that relay information about the proximity of an object.

High-frequency sound waves  reflect off boundaries, resulting in distinct echo patterns.

Fihure1: Ultrasonic Sensor

 

3.2 How Ultrasonic Sensors Work?

Ultrasonic sensors  operate by emitting a sound wave at a frequency  that is above the range of human hearing. To receive and transmit an ultrasonic sound, the sensor's transducer functions as a microphone. Like many others, our ultrasonic sensors  use a single transducer to send a pulse and receive the echo. The sensor calculates the distance to a target by measuring the time elapsed between sending and receiving the ultrasonic pulse.

Figure2:How Ultrasonic Sensors Work

This module's operation is straightforward. It emits a 40kHz ultrasonic pulse that travels through the air and, if it encounters an obstacle or object, bounces back to the sensor. The distance can be calculated by multiplying the travel time by the speed of sound.

Ultrasonic sensors  are an excellent solution for detecting clear objects. Because of target translucence, applications that use infrared sensors.  for example, struggle with this particular use case for liquid level measurement.

Ultrasonic sensors  detect objects regardless of color, surface, or material for presence detection (unless the material is very soft like wool, as it would absorb sound.)

Ultrasonic sensors  are a reliable choice for detecting transparent and other items where optical technologies may fail.

 

3.3 Using Multiple Sensors & Avoiding Disruption

When putting multiple sensors  into an application, it's critical to connect them in a way that prevents crosstalk and other interference.

To prevent the ultrasonic signals from your sensor from being disrupted, keep the face of the ultrasonic transducer clear of any obstructions.

Common obstructions include:

  • Dirt
  • Snow
  • Ice
  • Other Condensation

We recommend our Self Cleaning sensors  for this application.

Our self-cleaning function is designed to run continuously for the self-cleaning feature to be active. They are intended specifically for applications requiring condensation resistance in high moisture environments.

Please keep in mind that the Self Cleaning function is not intended to remove dirt from the transducer's surface. Its purpose is to clear the transducer's face of moisture so that it can operate normally.

 

3.4 How are Ultrasonic Sensors Used?

Our ultrasonic distance, level, and proximity sensors  are frequently used in conjunction with microcontroller platforms such as Raspberry Pi ARM , PIC,  Arduino , Beagle Board, and others.

Ultrasonic sensors send sound waves  toward a target and measure the time it takes for the reflected waves to return to the receiver to determine their distance.

This sensor is an electronic device that transmits ultrasonic sound waves  to measure the distance to a target and then converts the reflected sound into an electrical signal.

Our sensors are frequently used as proximity detectors.

Ultrasonic sensors are also used in obstacle detection systems and in manufacturing.

Our ShortRange sensors provide the option for closer range detection in situations where a sensor that ranges objects as close to 2cm is required. These are also designed with very low power requirements in mind, as well as environments requiring noise rejection.

 

Ⅳ Basic Guide to Raspberry Pi3

4.1 What is  Raspberry Pi3?

The  Raspberry Pi 3 Model B is the most recent model of the $35 Raspberry Pi computer. The Pi isn't your typical machine; in its most basic form, it lacks a case and is simply a credit-card-sized electronic board, similar to those found inside a PC or laptop but much smaller.

 

4.2 What Is the Raspberry Pi3 Capable of?

Surprisingly large. For starters, the Pi 3 can be used as a low-cost desktop, media center, retro gaming console, or router, as shown below. That, however, is only the tip of the iceberg. There are hundreds of projects where people have used the Raspberry Pi to build tablets, laptops, phones, robots, smart mirrors, take pictures on the edge of space, and run experiments on the International Space Station.

 

Figure3:The Raspberry Pi 3.

 

4.3 How do I Get Started With the Raspberry Pi 3?

One thing to keep in mind is that the Pi is merely a bare board. You'll also need a power supply, a monitor or TV,  HDMI  cables to connect to the monitor, and a mouse and keyboard.

After connecting all of the cables, the simplest way for new users to get up and running on the Pi is to download the NOOBS (New Out-Of-Box Software) installer. Once the download is complete, follow the instructions to learn how to install an operating system on the Raspberry Pi. The installer makes it simple to install various operating systems, though the official OS Raspbian is a good choice for first-time users—other operating systems are listed below.

Raspbian's appearance and feel should be familiar to any desktop computer user. The operating system, which is constantly being updated, recently received a graphical makeover and now includes an optimized web browser, an office suite, programming tools, educational games, and other software.

 

4.4 How Is the Raspberry Pi 3 Different From Its Predecessors?

The Raspberry Pi 3 quad-core processor is both faster and more capable than its predecessor, the Raspberry Pi 2. For those interested in benchmarks, the Pi 3's CPU—the board's main processor—outperforms the Pi 2 by roughly 50-60% in 32-bit mode, and is 10x faster than the original single-core Raspberry Pi (based on a multi-threaded CPU benchmark in SysBench). Real-world applications will see performance increases ranging from 2.5x for single-threaded applications to more than 20x when video playback is accelerated by the chip's NEON engine when compared to the original Pi.

Unlike its predecessor, the new board can play 1080p MP4 video at 60 frames per second (with a bitrate of around 5400Kbps), further enhancing the Pi's media center credentials. That's not to say that all videos will playback this smoothly; performance will vary depending on the source video, the player used, and the bitrate.

With built-in Wi-Fi and Bluetooth, the Pi 3 also supports wireless internet right out of the box.

The most recent board can also boot directly from a USB-attached hard drive or a pen drive, as well as from a network-attached file system via PXE, which is useful for remotely updating a Pi and sharing an operating system image between multiple machines.

 

Ⅴ Ultrasonic Sensor (HC-SR04) + Raspberry Pi3

A distance measurement is required or advantageous for many (outdoor) projects. These small modules, which start at 1-2 dollars and can measure distances of up to 4-5 meters using ultrasound, are surprisingly accurate. The connection and control are demonstrated in this tutorial.

5.1 Hardware

  • Raspberry pi 3
  • Ultrasonic Sensor(s) - HC-SR04
  • A set of resistors for each sensor you are connecting
  • 330Ω and 470Ω
  • Jumper wires to connect the sensor(s) to the pi
  • Breadboard to connect the sensor(s) to the pi

 

5.2 Wire Setup

Pins

The sensor has four (labeled) pins that must be connected to the Raspberry Pi's pins.

  • Pin 2 to VCC (5v - power)
  • Pin 6 to GND (ground)
  • Pin 12 receives a TRIG signal (GPIO18)

The ECHO resistor 330

- Attach it to Pin 18 at one end (GPIO24)

- Connect it to Pin6 as well, using a 470 resistor (ground).

- This is done because GPIO pins can only withstand a maximum voltage of 3.3V.

 

5.3  Breadboard 

As shown in the circuit diagram, connect the sensor to the pi using a breadboard.

By replicating this exact setup on the other half of the breadboard, an additional sensor can be connected to the pi.

Connect the VCC and GND pins together (2 and 6)

For the TRIG and ECHO connections, use any two GPIO pins.

Just make sure to include the correct GPIO pins in your code.

Figure4: Connecting resistors and jumper wires between sensors and pi

 

 

 

5.4 Software

Python

  • Create a new script

Figure5:Creating a new script in Python 3

  • Choose Menu → Programming → Click on Python 3 to create a new script
  • When you run the code, the script below will print the distance of the object in front of the sensor.
  • Because this code is easily manipulated to add another sensor, all variables have a "1" after them.
  • Simply copy and paste each section of code, renaming variables with a "2."
  • Make sure to connect a TRIG2 and an ECHO2 to the pi's two new GPIO Pins and to mirror the circuit diagram on the other half of the breadboard.

import  RPi.GPIO  as GPIO

import time

GPIO.setmode(GPIO.BCM)

TRIG1 = 18

ECHO1 = 24

#print ("Distance Measurement In Process")

GPIO.setup(TRIG1, GPIO.OUT)

GPIO.output(TRIG1, False)

GPIO.setup(ECHO1, GPIO.IN)

#print ("Waiting For Sensor1 To Settle")

time.sleep(.1)

GPIO.output(TRIG1, True)

time.sleep(0.00001)

GPIO.output(TRIG1, False)

while GPIO.input(ECHO1) == 0:

    pass

    pulse_start1 = time.time()

while GPIO.input(ECHO1) == 1:

    pass

    pulse_end1 = time.time()

pulse_duration1 = pulse_end1 - pulse_start1

distance1 = pulse_duration1 * 17150

distance1= round(distance1, 2)

print ("Distance1:",distance1, "cm")

time.sleep(10)

GPIO.cleanup()

 

Make a copy of your script and save it as ultrasonic distance.py.

Go to File and click on Save as

In the Save in field, navigate to the C: drive and then select a folder to save in.

In the File name field, enter ultrasonic distance.py.

Select All Files in the Save as type field.

Click the Save button.

To run the script, use the terminal.

Clicking on the monitor icon at the top of the screen will launch the terminal.

Enter cd "folder name" to change directory to your pythonpractice folder, then enter ultrasonic distance.py to run your program.

Ⅵ FAQ

1. Does HC-SR04 need resistor?

If you are using the ultrasonic transmitter from a HC-SR04 , I think you will find it needs between 5 and 12V to drive it. So you don't need a resistor you actually need a transistor circuit to provide the greater voltage under the control of the gpio.

2. What is the range of HC SR04?

2 cm to 400 cm

The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package from 2 cm to 400 cm or 1” to 13 feet.

3. Is ultrasonic sensor digital or analog?

The output of the Ultrasonic Sensor is digital. Two of the four pins are forsupplying power to it, one is for sending an echo signature to it, and the other is for getting output from it.

4. What is ultrasonic sensor HC-SR04?

The HC-SR04 Ultrasonic Distance Sensor is a sensor used for detecting the distance to an object using sonar. ... The HC-SR04 uses non-contact ultrasound sonar to measure the distance to an object, and consists of two ultrasonic transmitters (basically speakers), a receiver, and a control circuit.

5. What are the types of ultrasonic sensor?

All together there are four types of ultrasonic sensors, classified by frequency and shape: the drip-proof type, high-frequency type, and open structure type (lead type and SMD type).

6. Is HC SR04 analog or digital?

One of them is digital and the other is analog. We choose to use two sensors that measure: The UltraSonic Sensor (HC-SR04): Digital Sensor.

Kynix

Kynix was founded in 2008, specializing in the electronic components distribution business. We adhere to honesty and ethics as our business philosophy and have gradually established an excellent reputation and credibility in our international business. With the accurate quotation, excellent credit, reasonable price, reliable quality, fast delivery, and authentic service, we have won the praise of the majority of customers.

Join our mailing list!

Be the first to know about new products, special offers, and more.

Leave a Reply

We'd love to hear from you! Feel free to share your thoughts and comments below. Rest assured, your email address will remain private.

Name *
Email *
Captcha *
Rating:

Kynix

  • How to purchase

  • Order
  • Search & Inquiry
  • Shipping & Tracking
  • Payment Methods
  • Contact Us

  • Tel: 00852-6915 1330
  • Email: info@kynix.com
  • Follow Us

authentication

Kynix

© 2008-2026 kynix.com all rights reserved.