Raspberry Pi Projects

Raspberry Pi With Ultrasonic Sensor Tutorial

This is a tutorial on Raspberry Pi With Ultrasonic Sensor Tutorial or how to use an ultrasonic sensor module with a Raspberry Pi. The ultrasonic sensor module is used to measure distance. The module can be used to measure the distance to an object. The module can also be used to detect ultrasonic sounds. How to Setup a Raspberry Pi for the First Time | Getting Started with Raspberry Pi

The HC-SR04 module

ultrasonic sensor

The HC-SR04 module is an Ultrasonic sensor that can be used with Raspberry Pi or Arduino. It is a cheap and easy way to get started with using Ultrasonic sensors. The sensor can be used to measure distance, and can also be used to detect objects.

The sensor has two transducers, one that emits a sound wave, and one that receives the sound wave. The sound wave is used to determine the distance to an object. The HC-SR04 module can be used to measure distances up to 400 cm. The module is easy to use and can be interfaced with Raspberry Pi using just a few lines of code.

The HC-SR04 pinout

The HC-SR04 pinout is as follows:

ultrasonic sensor with Arduino pinout

  • Vcc is the power supply voltage, which is +5V for the HC-SR04.
  • Trig is the trigger input,
  • The echo is the echo output.
  • GND is the ground.

How Does the Ultrasonic Sensor Work?

Raspberry Pi With Ultrasonic Sensor

The HC-SR04 sensor works by sending out a ray of ultrasound at a frequency of 40kHz. When this ultrasound hits an object, it will bounce back and be received by the HC-SR04. The HC-SR04 will then output a pulse whose width is proportional to the time it took for the ultrasound to bounce back. The HC-SR04 can be used to measure distance in a variety of applications. For example, it could be used to measure the distance between a sensor and an object in order to determine if the object is close enough to trigger a reaction.

Distance = Time x Speed 

  • Time – Time that is taken by the sound wave to reach the receiver
  • Speed – Speed of the sound wave. 

Interesting Note –  

  1. Speed of sound changes due to the change in temperature and change in humidity. 

  2. The speed of sound, however, is 4.3 times as fast in water as in air 

Connecting The Raspberry Pi With Ultrasonic Sensor

As discussed before, ultrasonic sensors have a transmitter (Trigger) that can be operated to transmit infrared sound waves and have a receiver that receives reflected sound waves(ECHO pin). 

Raspberry Pi With Ultrasonic Sensor Tutorial

The trig pin must be connected to an output pin, and the echo pin must be connected to an input pin. Then, the microcontroller can be used to trigger the sensor and read the echo pulse. The HC-SR04 is a viral sensor and is relatively easy to use. It is a versatile sensor that can

Python Script for Raspberry Pi With Ultrasonic Sensor

Now for the script to really take some measures. In this example, I am using Python. Why Python? It’s a famous language on the Raspberry Pi.

You need to update the package version of Raspberry Pi using the below command.

Code:

Save the program named distance.py and type python distance.py to run the program in a terminal window. Now, you will see that the sensor starts reading at one-second intervals. Here, you can see an image of a Distance reading.

  • RPi.GPIO – This module helps us to talk with the GPIO pins of Raspberry Pi. 
  • Time – We are operating the Time Module to add a delay between transmitting infrared sound waves and receiving them.

  • Open Terminal
  • Launch IDLE IDE by typing
  • After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
  • Type the code below in the window
  • Save the code by FILE>SAVE or Ctrl+S
  • To run your code RUN>RUN or Ctrl+F5

Python Shell

Raspberry Pi With Ultrasonic Sensor Tutorial 2022

Conclusion 

Raspberry Pi With Ultrasonic Sensor reading

Related Articles

Leave a Reply

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

Ad
Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker