Distance Sensor GPIO cont'd - Raspberry Pi and Python tutorials p.7

sentdex · Beginner ·🛠️ AI Tools & Apps ·9y ago

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 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This tutorial shows how to connect the HC-SR04 distance sensor to a Raspberry Pi and use Python to calculate distance. The sensor emits a sound burst and measures the time it takes for the echo to return, allowing for distance calculation.

Key Takeaways
  1. Connect the HC-SR04 distance sensor to the Raspberry Pi using jumper wires and resistors
  2. Import the RPi.GPIO library and set up the trigger and echo pins
  3. Use the GPIO library to send a trigger signal and measure the echo response
  4. Calculate the distance using the speed of sound and the time difference between the trigger and echo signals
  5. Print the calculated distance to the console
💡 The HC-SR04 distance sensor uses the speed of sound to calculate distance, and the Raspberry Pi can be used to read the sensor data and perform calculations using Python.

Related Reads

📰
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Learn how a solo dev built a free AI-powered YouTube SEO toolkit with zero budget and the lessons they learned from the experience
Medium · Startup
📰
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
📰
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
📰
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
Up next
I Asked Gemini to Build a Dashboard... I Didn't Expect This
Patech
Watch →