<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hc-sr04 Archives | Microcontroller Tutorials</title>
	<atom:link href="https://www.teachmemicro.com/tag/hc-sr04/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.teachmemicro.com/tag/hc-sr04/</link>
	<description>Microcontroller Tutorials and Resources</description>
	<lastBuildDate>Wed, 11 Oct 2023 20:29:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://www.teachmemicro.com/wp-content/uploads/2019/04/blue-icon-65x65.png</url>
	<title>hc-sr04 Archives | Microcontroller Tutorials</title>
	<link>https://www.teachmemicro.com/tag/hc-sr04/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Raspberry Pi Ultrasonic Sensor Tutorial</title>
		<link>https://www.teachmemicro.com/raspberry-pi-ultrasonic-sensor-tutorial/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=raspberry-pi-ultrasonic-sensor-tutorial</link>
					<comments>https://www.teachmemicro.com/raspberry-pi-ultrasonic-sensor-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Thu, 16 May 2019 04:37:16 +0000</pubDate>
				<category><![CDATA[Raspberry Pi Tutorial]]></category>
		<category><![CDATA[hc-sr04]]></category>
		<category><![CDATA[sensor]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=3374</guid>

					<description><![CDATA[<p>An ultrasonic sensor is a common tool for measuring the distance of an object in front of it. While I recommend using an Arduino with ultrasonic sensor to achieve real-time results, it is still possible to use the sensor with a Raspberry Pi. This tutorial shows you how to build a Raspberry Pi ultrasonic sensor &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/raspberry-pi-ultrasonic-sensor-tutorial/">Raspberry Pi Ultrasonic Sensor Tutorial</a> appeared first on <a href="https://www.teachmemicro.com">Microcontroller Tutorials</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-featured.jpg"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-3384" src="https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-featured.jpg" alt="Raspberry Pi Ultrasonic Sensor tutorial" width="800" height="655" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-featured.jpg 800w, https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-featured-300x246.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-featured-768x629.jpg 768w" sizes="auto, (max-width: 800px) 100vw, 800px" /></a></p>
<p>An<strong> ultrasonic sensor</strong> is a common tool for measuring the distance of an object in front of it. While I recommend using an <a href="https://www.teachmemicro.com/hc-sr04-arduino-interfacing/">Arduino with ultrasonic sensor</a> to achieve real-time results, it is still possible to use the sensor with a Raspberry Pi. This tutorial shows you how to build a Raspberry Pi ultrasonic sensor featuring the <a href="https://www.win-source.net/products/detail/osepp-electronics-ltd/hc-sr04.html">HC-SR04</a> for distance measuring.</p>
<p><span id="more-3374"></span></p>
<h3><strong>Ultrasonic Sensor Basics</strong></h3>
<p>The term “ultrasonic” means beyond the sound that humans can hear; the human ear can only detect sound up to 20000 Hz. The HC-SR04 ultrasonic sensor featured here emits 40000 Hz.</p>
<p>Physics taught us that sound is vibration traveling through air. When the <em>ultrasound</em> from the sensor bumps into an object, it reflects back to the sensor. The time between signal emission and reflection (echo) is useful for calculating the distance:</p>
<p><img decoding="async" id="equationview" class="aligncenter" title="This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program." src="https://latex.codecogs.com/gif.latex?D%20%3D%20V_%7Bsound%7D%20*%20%28t_%7Bemit%7D%20-%20t_%7Bref%7D%29" name="equationview" /></p>
<p>The velocity of sound in air is 343 metres per second. Thus,</p>
<p><img decoding="async" id="equationview" class="aligncenter" title="This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program." src="https://latex.codecogs.com/gif.latex?D%20%3D%20343%20*%20%28t_%7Bemit%7D%20-%20t_%7Bref%7D%29" name="equationview" /></p>
<p>The formula above gives the distance (in meters) of the object in front of the sensor.</p>
<p>The HC-SR04 ultrasonic sensor contains four pins as shown:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/05/ultrasonic-pinout.jpg"><img loading="lazy" decoding="async" class="wp-image-3377 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/05/ultrasonic-pinout.jpg" alt="Raspberry Pi Ultrasonic Pinout" width="396" height="243" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/05/ultrasonic-pinout.jpg 600w, https://www.teachmemicro.com/wp-content/uploads/2019/05/ultrasonic-pinout-300x184.jpg 300w" sizes="auto, (max-width: 396px) 100vw, 396px" /></a><br />
Note that the blue-colored module works exactly the same as the one in the picture.</p>
<p>The HC-SR04 sensor draws 5V at 15 mA and can detect objects within 2 cm up to 4 m. Also, the viewing angle of this sensor is 15 degrees.</p>
<p>In order for the sensor to start sending ultrasound, a 10 <span class="st">μ</span>s pulse is sent to the trigger pin. The ultrasound signal consists of 8 cycles of 40000 Hz. After the signal emission, the echo pin will go high then will go low upon detection of the echo. Below is the timing diagram for the signals:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/05/ultrasonic-timing.jpg"><img loading="lazy" decoding="async" class=" wp-image-3379 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/05/ultrasonic-timing.jpg" alt="Raspberry Pi Ultrasonic timing diagram" width="484" height="223" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/05/ultrasonic-timing.jpg 655w, https://www.teachmemicro.com/wp-content/uploads/2019/05/ultrasonic-timing-300x138.jpg 300w" sizes="auto, (max-width: 484px) 100vw, 484px" /></a></p>
<h3><strong>Using the Ultrasonic Sensor with Raspberry Pi</strong></h3>
<h4><strong>Hardware</strong></h4>
<p>The Raspberry Pi GPIO pins use 3.3V signal levels. The trigger pin of the ultrasonic sensor will still work with 3.3V. However, the echo pin, which gives 5 V, will damage the GPIO pin of the Raspberry Pi. Therefore, the 5V must be reduced to 3.3V through a voltage divider network.</p>
<p>Below is an example wiring diagram for connecting the ultrasonic sensor to a Raspberry Pi:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-wiring.jpg"><img loading="lazy" decoding="async" class=" wp-image-3380 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-wiring.jpg" alt="Raspberry Pi Ultrasonic Wiring diagram" width="583" height="518" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-wiring.jpg 757w, https://www.teachmemicro.com/wp-content/uploads/2019/05/raspberry-pi-ultrasonic-wiring-300x267.jpg 300w" sizes="auto, (max-width: 583px) 100vw, 583px" /></a></p>
<p>Note that while a Raspberry Pi Zero is in the diagram, the wiring connection still works for a Raspberry Pi 2 or 3.</p>
<p>Here, a 1k ohm resistor and a 2k ohm resistor creates a voltage divider network which reduces the voltage from the echo pin from 5 V to 3.3 V. The formula is simply:</p>
<p><img decoding="async" id="equationview" class="aligncenter" title="This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program." src="https://latex.codecogs.com/gif.latex?V%20%3D%20%5Cfrac%7B2k%7D%7B2k&amp;plus;1k%7D%20*%205%20V%3D%203.3%20V" name="equationview" /></p>
<h4><strong>Software</strong></h4>
<p>We will be using Python 2 for our code. To generate a pulse of 10 <span class="st">μ</span>s on the trigger pin, we simply make the pin high, wait for 10 <span class="st">μ</span>s and then make the pin low:</p>
<pre class="theme:shell-default-copy lang:python decode:true "><pre><code class="language-cpp">GPIO.output(triggerPin, True)
time.sleep(0.00001)
GPIO.output(triggerPin, False)</code></pre></pre>
<p>Then we note when the echo pin goes low:</p>
<pre class="theme:shell-default-copy lang:python decode:true"><pre><code class="language-cpp">while GPIO.input(echoPin)==0:
     pulseStart = time.time()</code></pre></pre>
<p>And also we take note of when the echo pin goes high:</p>
<pre class="theme:shell-default-copy lang:python decode:true"><pre><code class="language-cpp">while GPIO.input(echoPin)==1:
     pulseEnd = time.time()&lt;b&gt;&lt;i&gt;    &lt;/i&gt;&lt;/b&gt;</code></pre></pre>
<p>The pulse width of the echo pin, which is the time between transmission and reception of the ultrasound, is now:</p>
<pre class="theme:shell-default-copy lang:python decode:true "><pre><code class="language-cpp">pulseDuration = pulseEnd - pulseStart</code></pre></pre>
<p>The pulse duration will be in seconds since the function <em>time.time(</em>) returns values in seconds. The distance of the object to where the ultrasound was reflected can now be known:</p>
<pre class="theme:shell-default-copy lang:python decode:true "><pre><code class="language-cpp">distance = 34300 * time / 2</code></pre></pre>
<p>Here, we used 34300 cm/s instead of 343 m/s because of the given range of the sensor (2 cm to 4 m only). Of course, we divide the time by 2 since we only want the time when the signal is emitted and when it hits the object, not when the signal is reflected back.</p>
<p>We can simplify this to:</p>
<pre class="theme:shell-default-copy lang:python decode:true "><pre><code class="language-cpp">distance = 17150 * time</code></pre></pre>
<p>Here is now the full python code which gives the distance of the object in centimeters:</p>
<pre class="theme:shell-default-copy lang:python decode:true "><pre><code class="language-cpp">import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

triggerPin = 23
echoPin = 24

GPIO.setup(triggerPin,GPIO.OUT)
GPIO.setup(echoPin,GPIO.IN)

#make this infinite loop
while True:
     GPIO.output(triggerPin, False)
     time.sleep(2)

#generate 10us pulse
     GPIO.output(triggerPin, True)
     time.sleep(0.00001)
     GPIO.output(triggerPin, False)

#note the start of the pulse at echo pin
     while GPIO.input(echoPin)==0:
          pulseStart = time.time()

#note the end of the pulse at echo pin
     while GPIO.input(echoPin)==1:
          pulseEnd = time.time()

     pulseDuration = pulseEnd - pulseStart

     distance = pulseDuration * 17150

#round off to 2 decimal places
     distance = round(distance, 2)

     print(&quot;Distance: %s cm&quot;) %distance

GPIO.cleanup()</code></pre></pre>
<p>&nbsp;</p>
<p>Was this tutorial helpful? Kindly drop a comment below!</p>
<p>&nbsp;</p>
<p>The post <a href="https://www.teachmemicro.com/raspberry-pi-ultrasonic-sensor-tutorial/">Raspberry Pi Ultrasonic Sensor Tutorial</a> appeared first on <a href="https://www.teachmemicro.com">Microcontroller Tutorials</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/raspberry-pi-ultrasonic-sensor-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: www.teachmemicro.com @ 2026-07-27 09:52:12 by W3 Total Cache
-->