Distance Sensor GPIO cont'd - Raspberry Pi and Python tutorials p.7
Key Takeaways
This video tutorial demonstrates how to connect and use the HC-SR04 ultrasonic distance sensor with a Raspberry Pi, using Python to handle GPIO input and calculate distance.
Full Transcript
what is going on everybody and welcome to part 7 of the Raspberry Pi tutorial series in this video what we're gonna be doing is wiring up the distance sensor now to do this we're going to need quite a few things we're going to need a actual distance sensor that's the HCS ro4 and then we're going to need for male to female jumper wires and then we're going to need three 1k ohm resistors or you could have a 1 2 K ohm resistor and then one 1k or any way to make 1k and 2k ohms so the VCC trigger echo ground not sure how well you can see that on camera it's try not focused I turn off autofocus but anyways it says right here on each of the pins what they are so the VCC that's your that's your power trigger is the sensor basically it's going to shoot out so basically when we trigger the trigger it's going to shoot out some system sound and then the echo is the input and that measures when the input actually occurs and then GND is ground so with that let's go ahead and connect everything alright once you've got everything hooked up you've got the Raspberry Pi turned on you're ready to measure distance so let's go ahead and CD into desktop and then let's nano distance sensor PI and we're ready to begin first we're going to just like before import RP lowercase I dot all caps GPIO adds all caps GPIO we're going to import time GPIO dot set mode GPIO woops gbcm so we're going to define trig as four and echo as 18 so the reason why we're doing this is if you notice before in the previous tutorial if you follow along with the LED lights just hard-coding each of the pin values every single time can be pretty tedious if you as you make a larger and larger program it's going to get out of hand should you ever need to change those pin numbers so you probably just want to give them a constant right at the beginning of the script rather than typing it out so that's why we're doing that next what we're going to do is GPIO dot set up we're going to set up the trigger pin to be a GPIO dot out and then GPIO dot set up the echo pin that's going to be a GPIO dot in so the trigger sends out a burst and then the echo takes it in using the known constant that is the speed of sound we can calculate how much distance was traveled based on how long did it take for that sound wave to go out and come back so once you've got that we can go ahead and begin that with a GPIO dot output this weeks are better done or did we note my mouse was in the way I didn't see it anyway GPIO output a trig true and then we're going to do a time dot solely for 0.001 something like that something good enough to just give it a moment's rest then GPIO or a moment to do it is do not output trig false remember we're going to do is we're going to save Wow the GPIO dot input to the echo is equal to false so so long as that's the case we are going to say start equals time time and then we're going to say wow-wow GPIO dot input oh my goodness there we go echo equals true and the equal time that's up then we're going to say the sick time is going to be M minus the start and then we're going to measure the distance now for centimeters if you want inches you can go to the text-based version on break that one down there it's I can also just say it as we put it up anyway distance will be equal to the sick time whatever that is divided by zero point zero zero zero zero five eight so four zeros after the decimal one if you wanted to do inches inches inches would have been zero point zero zero zero one four eight then what we're going to do is we're just going to print this since cm dot format this is and then finally when we're all done let's run a GPIO dot clean up clean up our mix control X to save yes that file now let's do Python distance sensor hi and there we have a distance if for whatever reason you get some sort of error or it gives you a warning just run it again and that cleanup should solve your problem so right now we're reading about a 102 and then I'll put my hand in front of it and now yes we've got a 21 centimeter distance okay so now that we've done that we've got the light we've got the distance sensor what I'd like to do in the next tutorial is kind of combine the two things and create something a little more complex and for that we're going to make a sort of like one of those like garage stop lights where basically as you pull into the garage it's a green light as you get closer closer to the max distance you can pull forward it turns yellow and then when it's time to stop it turns red so we're going to make something like that for the Raspberry Pi although instead of using a car we'll just use our hand as we approach and The Closer that we get it'll turn yellow and then finally red so that's we're going to do in the next tutorial if you have any questions comments concerns whatever feel free to leave them below otherwise I will see you in the next tutorial
Original Description
In this Raspberry Pi tutorial, we're going to introduce a new sensor, the HC-SR04 ultrasonic distance sensor, along with handling GPIO input.
The HC-SR04 distance sensor measures distance based on emitting a sound burst, and timing how long it takes to receive the echo back. Using the known constant that is the speed of sound, we can mathmematically determine the distance of any object in front of this sensor by simply measuring how much time passed while the sound waves were emitted, hit the object in front of the sensor, bounced back, and came back to the sensor.
Text-based version of this series: https://pythonprogramming.net/introduction-raspberry-pi-tutorials/
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
🎓
Tutor Explanation
DeepCamp AI