Raspberry pi with Python for Robotics 9 - Adding HC-SR04 Distance Sensor
Skills:
Tool Use & Function Calling80%
Key Takeaways
This video tutorial series demonstrates how to use a Raspberry Pi with Python for robotics, specifically adding an HC-SR04 ultrasonic distance sensor to a robot car. The tutorial covers the setup and configuration of the sensor, including wiring and coding, to enable distance measurement and autonomous navigation.
Full Transcript
What is going on everybody? Welcome to another robotics and Python and Raspberry Pi tutorial video. This video in the subsequent few is going to be covering the addition of this distance sensor. This is the HC SR4. So, by now uh my Raspberry Pi car is using that anchor for portable power to the Pi and we've got that Edamax Wi-Fi dongle. So, our car is now remote controllable via Wi-Fi and could actually be controlled via the internet in say a browser or via some sort of phone app, but now we want to add a distance sensor to it. So, we can begin to make the transition to an autonomous robot. So, for me, I'm more interested in the software aspect than the hardware aspect. And I'm more interested in having um a robot be autonomous rather than remote controllable. Luckily along the path to autonomy we can also just add in remote control anyways. So we have already done that but my goal is more aut uh for more autonomy. So for that we want to start adding sensors. One of the easiest sensors is this HCSR4. And so we're going to be using that. So this is a distance sensor. What it is is it's an ultrasonic sensor that uses sonar to determine the distance of objects from it much like uh bats do. So what it does is it's going to transmit some sound and that's sound is going to travel to an object. So basically what this is going to do is say it shoots sound out of here. It's going to travel to this object bounce back and be received. Okay. And from here using the known constant of the speed of sound, the amount of time it takes to occur, we can use these together and we can accurately measure how far away an object is. So some of you guys might be thinking, well what about what about uh angles? So right here, if this was to shoot to the spreadboard and back, that's a straight shot. But what if it was like this? Well, the sound might bounce here and it might actually miss the sensor. So because of this, we have to kind of take this into consideration. So from a bunch of tests, uh, this sensor performs best at less than a 22° angle and anything over a 30° angle should really just be distrusted. So for this reason we can bring more sensors on board and use the lowest distance measurement uh as our distance. Okay. So we can bring more sensors on board and also we can pre-program that that specific sensor's spot right that specific sensor's placement on the car or whatever we have and we use the lowest one to determine which sensor is the most accurate. Okay. And so so we can do that because if if the degree is higher than 30 either what's going to happen is either it it just doesn't get a signal back or when it finally does come back it's a massive number like 500 or like let's say 500 centimeters because we're going to use centimeters. So say an object was 45 degrees away. What's going to happen is say it was like this. It's going to bounce off here come to me bounce back and then bounce to the receiver. So, for this reason, it's unlikely that you'll get an angle like with an angle. It's not it's unlikely that you'll get a false positive that an object is too close, but you might certainly get false negatives and actually an object is much closer than we thought. So, enough on that. Let's go ahead and break down this specific sensor. So, as you can see, the HC SR4 has basically four connections to it. You've got VCC, trig, which is trigger, echo, and G&D for ground. VCC is basically the power and this is a 5volt sensor. So we're going to need to send 5 volts to this. Trigger is going to be triggered by an output, a GPIO output, and that's going to trigger the release of an ultrasonic wave. Then echo, echo is basically receiving. So that's going to be a GPIO input. So trigger, you can basically what's going to happen is you're going to trigger it, mark the time at trigger, and then once you receive a signal back at echo, you mark the time, and then ground just goes to ground. Now, keep in mind this is again 5 volts. So the only main the only issue that we're going to have here is that the HCS4 is 5 volts. The Pi is 3.3. And so if this sends back a high signal back to the GPIO input pin on the Raspberry Pi, we might have some problems because uh that's going to be too much for the Pi and you could damage your Pi. So obviously we don't want to do that. So the items that we're going to need are the following. We're going to need uh two male to female uh jumper wires. And they're going to you're going to want them to be of decent length because it's going to wire from our pie to the breadboard. The breadboard is basically I'm going to use the breadboard. You don't have to, but I'm going to use the breadboard to stick the sensor into because the breadboard will hold up our sensor quite nicely. So, the breadboard will just go right on top of our car. Luckily for me, my breadboard actually screws in perfectly to the board. These two screws fit perfectly. This breadboard didn't come with the kit. I just got extremely lucky. Um, otherwise, just use like double-sided tape. put double-sided tape on here and stick it on your pie or you could glue it if you wanted, but do something like that. So, you're going to need a breadboard, obviously the HCS4, two male to female jumper wires, and then two male to- male jumper wires. I ended up having to stick two together just because my only male to male jumper wire was really short. Um, so I connected it to a female to-male jumper wire and just electrical taped it up. And then I have another red one. So, two male to male, two male to female, one breadboard, one HCS4. And finally, you're going to need a um 1 kiloohm resistor. And that 1 kiloohm resistor is what we're going to add to the GPIO input. Right? So, we're going to add that to the echo. So, before it we send it back to the Pi, it goes through the resistor. And that will be enough uh so we don't overload our Pi. So, enough on that. How are we going to wire this? So, obviously, uh, the first thing we have is this ground. So, you're going to want to put the ground in. And let me go ahead and actually sense or center my sensor as best as poss. So, there. And if you don't know much about breadboards, the we're not going to use any of these sides, but the the way a breadboard works is it's like this usually. You can see it's numbered or lettered on the sides. And basically, it works horizontally. So, anything you connect like this, like we're connecting, you know, perpendicular, but anything here, like this whole row is all connected. You can't see it because we've got a back on here, but they're they're all connected. So, basically, it's it's an easy way to make a circuit without soldering or soldering. Um, so anything we put in line here will be a part of this, but it won't interfere with these. But, of course, you could make the connection if you wanted. Um, we're just going to choose not to. So anyway um if we look back at our front VCC is over here. So this is where the power is. So for me I try to denote power um with red. Then we have trig is the next thing and followed by echo. So trig will make it orange. So we'll plug that one. can actually uh I'm going to plug them in at the very end here just so they're all kind of uniform with each other just in case you wanted to throw something else into that uh line. So red and then we'll plug the orange for trigger. You can use any colors you want. Maybe maybe I'll plug it in. I can't get it to go in. There we go. Right. So initially I had wired this up wrong and I decided I better re-upload it since um it's fairly it's a fairly big deal if you wire it up wrong and break your pie. Um, I've probably put in at least a few hours uh with it wired up wrong and didn't have any problems, but it is possible to to um to break your pie this way or the way I had done it. Anyways, so I figured I better go over it. And special thanks to the sticky ou and Andrew Bagot for pointing it out. Um, pretty stupid mistake. Anyway, uh, so what I had done was basically the way the breadboard works again is you have these these holes, right? And they're all connected horizontally and underneath them, the way that they work is there's just a metal track. And so when you plug into the hole, it connects everything. But what'll happen is what I had done is I had connected this yellow pin on the same side. So on the same track and used the resistor thinking that's going to, you know, resist the current or whatever and that would solve our problem. But what was going to happen or what did happen and luckily I didn't get any trouble for myself was it would the current is going to take the path of least resistance which would just be the track underneath. So the resistor was really doing nothing besides directing the current away from it basically. So that was worthless. So instead what you have to do is we can leave all the other connections like we've got. But we use the resistor to bridge the gap here because this gap uh the tracks don't follow under it. So, we can use the resistor to bridge the gap and then plug our yellow input uh jumper wire into this track over here using the resistor. So, the only path possible is now through the resistor. Uh but still a pretty important um thing that you have to take into consideration and uh not plug things in if you're going to use a resistor in the same track. So, anyways, uh that's that and we'll continue on with the rest of the video. All right. So, here's the car. Uh, go ahead and just, you know, delicately take your pie out and your Hbridge out. So, take pie like so. And the H bridge like so. I guess I'll have to kind of move the battery pack a little bit to get this all to come out for us. and just move it over there. Okay, to get the input pins on. So, once we've got that out, the next thing we want to do is we want to begin um plugging in the GPIO input pins um to our Pi. And then also, we want to plug in what we need to our Hbridgeidge. The Hbridge plugs we're going to plug into that 5volt. And then we're also gonna plug in to the ground on the Hbridgeidge. Um, so yeah. So then, so now that we've got those on here, let me figure out how I'm gonna stick my I guess I'll put the breadboard here. You've already seen the breadboard, so you'll just see the wires coming off the screen, I suppose. And maybe I can move it up a little bit. There we go. Okay, so first we want to plug in, let's go ahead and plug in the input and output pins on our Raspberry Pi. Okay, so let's begin. So, first we're going to plug in the orange wire. And the orange wire is corresponding to our trigger. And we're going to plug that into GPIO pin 18 or pin 12 on the Pi. And that is basically two pin gap. So, it's the third pin down from our ground on our Pi. So, right here. And I'll hold it up like this. So, you've got a gap of two pins. And then we plug in our orange. And that's for the trigger. So, that's going to be an output pin. And then the next one we have is our yellow pin. And that's our input pin. And that's going to be for echo. And that one gets plugged in basically with a gap below GPI pin 18. And then you've got GPI 23. So that one goes right here. If I can get it on there. There we go. Okay. So it should look like um like this. See if I can get it to focus. There. So we have here our 5volt with one gap. Then you've got ground. Then you've got two pins. Then you've got your orange. And this is for uh the trigger. One more pin. Then you've got the yellow for echo. So those are in now. And now we have to connect all of this uh to the Hbridgeidge because we're going to take our power from the Hbridgeidge because we've already got uh our GPIO pin is already being exhausted. So we might as well just use the same thing it's going to. So for the power here, so my red cord, we're going to plug this right into the VCC on our Hbridgeidge. So for me that was we used a green wire for that. So um let's see maybe I can shift this stuff make it easier to see. Okay. So that would be uh if you had your your thing facing me that's going to be right here right next to ground. And then after that, we're going to plug our ground into this one. So, let's go ahead and do that. I'm just going to loosen this one up a little bit. And we'll take our uh VCC, which is 5VT, and we're plugging that into the 5volt slot. Basically, we're sharing the slot with um the 5volt power that we're grabbing from the Pi. Anyways, screw that on. Screw it fairly firmly. You don't want to kill it. Give every give both of a decent tug. Make sure they're in there good. Mine are. So, now we're ready to plug in the ground. And that will be the black wire here. So, again, just kind of unscrew your ground a little bit. That's the middle one. And then fit your um your other ground in there. There we go. Okay, once you've got that one in there, give them both a nice uh tug. Make sure they're both in there. What sometimes when you're sticking two in there, um one might be snug, but the other one might not be. So, just make sure that they're in there good. So, now we've got everything hooked up and I'm going to kind of put everything back into my car. No real reason to leave everything strewn out like this. And just for anybody who is curious, the way that I've been doing it is uh I take the battery pack and I slide that all the way to the back because that will um it fits nicely underneath these. Like these won't hit your batteries. So I slide the battery pack all the way into the back sometimes. Apparently I can't today. There we go. Okay. So that's all the way in the back. And then I try my best to just kind of stuff both of these in here. Uh, this one's it's just a little bit harder to do the PI and the Hbridgeidge, but um, usually I can make it work. The only problem I've been having so far is with the Pi. Uh, sometimes I lose the signal from the Edamax. Well, which makes sense because we're encasing this into a metal box basically. So, you could could put your Pi on top. I like it when it's nice and clean. So, I just want to put it on there uh in the inside. And my goal is really autonomy, not necessarily remote control. So, it just kind of works out. But you can put the pie on top. I mean, there's nothing horribly wrong with that. Um, and we will be putting the breadboard on top at least. So, anyway, um, I'm gonna pack these in here real quick and then I'll, uh, resume once I get everything in the car. All right. So, once you've got, um, everything put back together, uh, like I said, my breadboard just so happened to luckily fit two screw holes here, so that worked out. Um, so we were able to get that on there. Got everything wired up. I'm just threading the wires through uh the back here. And then also the power to the anchor is here. And that's pretty much it as far as wiring it up goes. Uh just make sure you haven't forgot your one kiloohm resistor for the GPIO input pin so we don't uh break our Pi. And other than that, we are ready to begin to code this. So, we're going to continue on in the next video with setting up the pins and uh taking some test measurements, making sure our sensor works, and then from there, we'll actually start building this into the autonomy of the Raspberry Pi car. So, that's what you guys have to look forward to. If you have any questions or comments, feel free to leave them below. As always, thanks for watching. Thanks for all the support and subscriptions, and until next time.
Original Description
Basic Raspberry Pi Robot Car Playlist: http://www.youtube.com/watch?v=LlFkybEQFFA&list=PLQVvvaa0QuDeJlgD1RX9_49tMLUxvIxF4&feature=share
In this python tutorial video, we take our raspberry pi robot car, and we add the HC SR04 ultrasonic distance sensor to it. The idea of the distance sensor is to relay some waves out, time how long it takes for them to come back, and, then, using the known constant of the speed of sound, we can derive the distance of the object that the sound bounced off of.
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
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: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
25 Best AI Tools in 2026 to Boost Productivity, Create Content & Make Money Online
Medium · Startup
25 Best AI Tools in 2026 to Boost Productivity, Create Content & Make Money Online
Medium · ChatGPT
Email Automation with Python and AI: Build Smarter Workflows That Save Hours Every Week
Medium · AI
Copy And Paste
Medium · AI
🎓
Tutor Explanation
DeepCamp AI