Raspberry Pi LESSON 24: Using a PIR Motion Sensor with the Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com and we're here today with episode number 24. in our incredible new tutorial series where you're teaching your raspberry pi boss what i'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners no needed and as you're pouring your coffee as always i want to give a shout out to our friends over at sun founder sun founder is actually sponsoring this most excellent set of raspberry pi tutorials and in this class we are using the most excellent the most wonderful sun founder raphael raspberry pie kit guys it is a great kit it's a great value most of you already have it if you don't look down in the description there is a link over to amazon and you can pick one up and your life in my life will be much easier if we are working on identical hardware but enough of this shameless self-promotion let's talk about what i am going to teach you today and i'm going to teach you a new component out of the sun founder kit and the component that i am going to be learning you about that's what i used to i used to have a student that would come into class every day and he would say learn me learn me like you know kind of a kind of a funny story but anyway i digress i'm going to learn you guys today and what we're going to learn is we're going to learn about p i motion sensor what does pir stand for you ask passive infrared motion sensor and it's a very simple little component and the way the component works let's go ahead and get it out so i can we can be looking at it while i am talking about it so let me switch on over here to the overhead view and so what i need you to do is get out your kits and i better get my large noggin out of your way and i better get a little more illumination here there that looks good what we're going to do is we're going to dig down into the second level of this man do you guys enjoy opening your little kid as much as i do and just see all the wonderful little electronic goodness in here and fun little things that we can work with for the next year but i need you to find this component okay and what this is is it's the one that looks like it kind of has a plastic dome so it looks like that that is your pir motion sensor and then also we're going to need three wires and so i have got a convenient and i'm going to connect directly to the raspberry pi and so we're going to need the female to female wires and those are not suitable let's see i should have those these things all out and ready to go but let me see if i can find three those have the nail end and more professional operation would have had these wires already picked out but it's like here's that's not them am i just absolutely going crazy yeah i need female to female so ah i have two there it is i knew there was an orange one in there but it was hiding from me okay so let's get this all put back together in a nice organized fashion we'll get this back in we will close the lid and now we'll talk a little bit about the pir sensor and you can see it here and what this does is it's basically got a fernell lens on it so there's an infrared sensor there's a furnace lens and that lens means that it is looking out over a wild a wide field of view and what it's detecting is infrared but you can imagine what the challenge is is that if you are just looking for an infrared signal imagine you're pointing it at the room and the sun shines in the window and it warms the spot up on the wall well is that an intruder or is that a warm spot on the wall or similarly if you just made a motion sensor and maybe there's a little breeze and your curtain is moving well is that a moving curtain or is that an intruder the really cool thing about the pir sensors is they are fundamentally an infrared sensor but what they're looking for is changes in the infrared profile and so to trigger this sensor it's a simple on off trigger it's either it's detected something or it's not so it's output is a one or a zero so it's really easy to use but it's very smart in that it triggers on two conditions and these two conditions must both be true it must see motion and it must see heat a heated object motion so an object that is warmer than the background infrared signal and it is moving and if you get those two conditions it triggers so if you had a dog walk through the room the dog is warm and the dog is moving so it triggers but other moving objects shouldn't trigger it and other just warm spots like if you put a cup of hot coffee in front of it if the coffee was still it shouldn't trigger so that's kind of what we're using and i think this is one of the slickest components in here because this is very valuable for using in other projects or you know as part of a project so imagine if you will that we wanted to have a camera that recorded when it sensed motion well we would use this and we've got the little camera and we will be getting to the camera in the raspberry pi uh you know in the raspberry pi kit we will be getting to that camera as soon as we possibly can but imagine that we hooked the sensor and the camera to the raspberry pi and then we let it sit and anytime it saw this trigger it would record let's say 30 seconds of video so you could have a pretty slick little security camera that you built from scratch so that's kind of the direction that we're going there that is the direction and so let's go ahead and hook this thing up let me uh let me show you let's see here let me kind of show you what we're dealing with here if i can find my mouse cursor there it is okay so this is what we're dealing with and uh let me get this where you can see it and so this is looking at you're looking in the schematic of the sensor upside down with the pins turned towards you and so what can we see when we look at the schematic up there well first of all you see that there's a couple of potentiometers and i haven't played around too much with the potentiometers but the delay potentiometer if it's all the way to the left the delay is slow and if it's all the way to the right the delay is large and so you can play around with the impact of a slow or large delay i've got it set pretty much to the left so if it sees something it fires if it doesn't see something it turns off it doesn't try to fire and hold or it doesn't have to or it doesn't try to wait before it triggers and this one you can kind of tune the distance if you want to ignore things further out you can play with that potentiometer as well but what we are really interested in are three these three pins you can see the leftmost pin is vcc and that is even though the raspberry pi is a 3.3 volt device the sensor wants 5 volts and so we are going to take that leftmost pin vcc and then i'm going to connect it up here to the upper right with the usb pointing towards me the upper right should be vcc okay and because i am a careful type i want to confirm and it's the 5 volt vcc i want to confirm where the ground is the ground should be pin six so it's going to be two four six i'm going to skip one and then i will be in the ground and so you can see that i am now going to ah i connected that up wrong so red wire red wire to the left pin when you're holding it upside down okay and now right wire or the next wire is going to go to the right pin which is ground and that's going to be the third pin down pin two four six and so now i've got this thing powered up and now what i need is i need the center pin the center pin is my output signal so i'm going to use my orange wire there okay you see my orange wire and i am going to go to board pin 12. now you notice when i'm using the sunfounder breakout board on a breadboard i use the bcm numbering system because the pins are labeled in bcm but when i'm going just right off the pi i use the board system because you can count and i believe we are going to use pin 12 and so i come over to the right column and i'm going to go pin 2 4 6 8 10 12. this one should be 12 and then just to check and make sure i have skipped two pins between ground and 12. and so let's see if that is right if i come up i was at ground okay i was at ground and then i skipped two pins i skipped eight and ten and then i'm in twelve and so that does look impact right and i think we have this thing hooked up and so we can get the schematic out of the way and we can come back over here and what i want to do is i want to sort of point it not directly at me and this would be great if we kind of 3d printed a little mount for it but we're just going to have to use the old bend the wires technique here and that would be pretty good because then i could come in but then i want to put it over here so you can see me kind of interacting with it a little bit a little bit better okay so i think that that is hooked up and i think that we are ready to start coding [Music] all right so let me come over here i hate this ugly thing of wires there i really do i hate that ugly thing of wires but we're going to ignore the disarray of the wires and we're going to come over here and what i need you to do is fire up our old friend mr thony so we go to programming come down to thony and then uh boom fresh new program just waiting to be written and then i'm gonna go i always like to save my program when i start so i don't forget to do it at least it saves the name and so i'm going to call this i'm going to call this p i r dash motion dot p y like that okay and now boom fresh new program just waiting to be written and then we're going to jump in here and get this thing going and i think you guys are going to be pleased how easy it is to do this okay how easy it is to do this i'm trying to do a little adjustment here in my studio there that's good okay so we're going to be talking to our old friend mr gpio pin 12 so we're going to have to import rpi.gpios [Music] gpio and then we are going to need a delay so i'm going to import time i just have one delay and so one thing you can do i think i've shown you this before but if you say from from from time import sleep i think if we do it like that if i'm remembering right now all to sleep all i got to do is say sleep instead of time dot sleep and so i kind of like doing that then our our motion pin where did we hook it we hooked it to pin 12 but then we got to tell it what mode we're using what board numbering mode we're using and so i need to do a gpio.set mode and what mode am i going to set it's gpio dot what dot b o a r d like that because we were using the numbering scheme from the board not from the bcm system okay we're moving right along here now we need to set that gpio dot set up and what are we going to set up we're going to set up the motion pin and that is going to be a gpio dot in because we're going to read from that pin okay we're going to read from that pen now the sensor needs a little time to normalize and look at the room and figure out what's going on and so it's good for us to give it a 10 second delay and then when you're firing up the program you want to not be moving around because it's trying to get a reading of the room and so you want to sort of not have something warm and moving in its field of view so it can get normalized so i think probably a time dot sleep of 10 seconds sometimes i've read people that recommend 30 seconds or a minute but we're going to live dangerously here and it's going to be kind of boring if you got to sit and wait for the program sit and watch me wait for the program for a minute so we're just going to do 10 seconds and now we need to go ahead and set up our try and i'll come back and write the program later but let's go ahead and do the accept accept part while we're still thinking and we're going to accept out on a key key keyboard interrupt a keyboard interrupt like that did i spell that r not right no i did not keyboard interrupt and so what that means is it's going to be up in the the try part until it sees control c and then it will hop down and execute this clause which is just to clean up and release the gpio pin so the next guy that uses it or the next time you run the program it's starting with uh free gpio pins and then i want to see that i actually got down here so i'm going to print gpio good to go like that close it close it and now we're ready to come up and write the program well you know that we're going to want to keep looking at the room keep looking at the room keep looking at the room so we're going to create a loop while true when it's true true true is always true so this is an infinite loop until it sees the control c then what do we want to do in this in this loop it's pretty simple we want to look for motion and so we create a variable motion and that is going to be equal to gpio dot input and where do i want to get my input from motion pin that's pretty easy right and then we're going to come down here and i'm going to print motion like that and then let's see if my little trick up there let's see if my little trick works up there and i'm just going to say sleep for let's say 0.1 seconds let's see if that's a good amount of time to sleep could it really be that easy i ask you can we do a high-tech intruder detection system with like what is it 14 lines of code i hope so how many mistakes did i make in here how many mistakes did i make okay let's see i will need you all to hold your breath okay we'll need you all hold all to hold your breath denied time what okay uh what did i do i tried to get fancy and it didn't work so i don't remember how to do that so we're just going to say import time like that the old-fashioned way and i'll figure out how to do it next time and i did time dot sleep here right i did time dot sleep instead of just doing sleep and then i'll come down here and i will do time not sleep i shouldn't try to do something without reviewing it but i'll figure it out next time we are going to come up here and the real reason was one of you didn't hold your breath so i need whoever it was to hold their breath this time it's in the 10 second delay oh man okay all right so what we're going to look at now is can you can see down there that it is not detecting motion and let me come over here and see if i can switch over to this view and if you look in the upper left corner you can see the readout and then i am over here about 90 degrees to the side and so i am out of the field of view of the sensor and so if i move over here because i'm right at the well it's almost seeing me over here so it's getting pretty far out there that it can actually detect you so it's got a pretty wide range of view here but what i'm going to do is i'm going to come in and you see as i bring my hand in it sees it okay and so as i bring my hand in it's seeing it but watch this okay now it doesn't see it now watch this i'm going to bring my hand in and i'm going to try to hold my hand very still okay and so it sees it but then now i've just become part of what it expects but now can i remove my hand without it detecting me no i can't okay let me try it again you can actually have a lot of fun with this so let me get it where you can see it here so it it sees my hand it triggered and it alerted that it had detected something but now it's just accepting it it's not triggering on it but let's see could i remove my hand without triggering it well no i didn't do a very good job even my thumb moving so let's see if i move very slowly no i don't seem to be able to move slow enough for it to you know for it to not detect and so this is really a pretty amazing sensor and it's pretty it's pretty amazing it's pretty incredible how hard it is to fool it and it's also pretty incredible how immune it is to false detections or false positives and so it's really a really really cool sensor and what you can imagine is this this was a quick lesson and this was an easy lesson but you've now gotten a very significant new tool in your tool kit and you can use this as part of all types of different projects like imagine you could have a system where if someone is in the room if someone is in the room you turn the lights on and then when the person leaves the room you turn the lights off and i think in most cases well i just said that but is that really true i guess what i can just see is is there a you know is there what you would want to see is is there enough motion with just a normal person sitting that the light would stay on i guess if you went to sleep it would probably go off but if you're moving at all it would probably stay on and you might be able to play with that delay that i think the delay will go up to 300 seconds and so perhaps it only goes off if it hasn't seen motion in 300 seconds there's one other thing that i didn't tell you and you can play around with the different uh the different configurations here but do you see this jumper over here i didn't tell you about this okay do you see in the upper right there is a jumper and you can do the low or you can do the high that is you can put the jumper on the inside two pins or the outside two pins and that sort of affects how it triggers and resets and i played around with it and i have it set on the inside ones you can try inside and outside and see which triggering mode whether you want it to trigger or you know reset and and it's it's kind of a little subtle what it does but play around with those two trigger modes and see which one you like better okay so where i see going with this is i see using this to trigger the raspberry pi camera when we get to the camera lessons and we could create like a little a little security camera using you know nothing more than what's in your what's in your raspberry pi i bowed up there i love that boat i've been on that boat mr chevy mr chevy drives that boat captain chevy is technically what we would be calling him and he's a great guy and uh sometimes we need when we need to ride somewhere mr chevy captain chevy will come down to our dock and pick us up so yeah we get to see the boat but i digress so what i want to do is i want to be able to have that component in our tool kit for future lessons what your homework is is to incorporate this into some sort of project and what and then what i need you to do is post what your idea was to youtube and show what you've done with the proximity sensor and what you've learned today then down in the comments below put a link over to your video and then your video in the description put a link back to this video so that anybody watching it will know uh will know what you're talking about and kind of like if you not good at coming up with your own ideas at least connect to the rgb led and then if it detects something turn the rgb led red for like intruder alert and if it's all clear turn it yellow okay i mean turn it green all clear red intruder alert so you're going to control the color of the led based on whether you've seen an intruder or not and then you could also even get fancy with your with your color schemes you could have green is all clear you could have red presently an intruder you know you know active intruder with red and then yellow could be all clear now but we did see something during the time that you were absent so you could sort of create different scenarios of the light indicating not just what's happening now but what had happened in the uh in the earlier times and so you know you can play around with that or if you want to get more sophisticated you can you can try to do something more sophisticated guys i really hope that you're also taking the 3d printing the fusion 360 3d printing class that i'm doing on this channel because you can see really quickly we're going to want to start building units that we would like print an enclosure or print a case for so if you take what i'm learning if you take what i'm teaching you in this series of lessons and you add that 3d printing in that fusion 360. it's going to get to some pretty powerful things that we can do oh look there's the fisherman going by is it just me or do you do you guys love to see the fishermen as well i'm gonna watch them go back by there they are but uh so hope you'll take the fusion 360 class also i hope you're having as much fun taking these lessons as i am making them i am really loving both of these classes that i'm doing right now and if you enjoy it give me a thumbs up don't forget to subscribe to the channel and most importantly share this with other people particularly if you know some young people out there try to get them involved maybe hook them up with some hardware because you know seriously the world needs more people building things and doing engineering and fewer people sitting around watching silly cat videos paul mcquarter with toptechboy.com i'll talk to you guys next week
Info
Channel: Paul McWhorter
Views: 9,584
Rating: undefined out of 5
Keywords:
Id: lLc4KM8LZnY
Channel Id: undefined
Length: 24min 28sec (1468 seconds)
Published: Thu Aug 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.