How to Use a PIR Sensor with Arduino: Wiring & Code for Motion-activated Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone and Welcome to our live Workshop series on how to motion activate all the things using this P sensor especially if you're building motorized props cool Miniatures that animate as well as animatronics when somebody walks by it comes alive here's your typical P sensor they also come much smaller so you can kind of sneak it into a forehead of a prop or you can hide it a little bit better there's a cone of detection that it has that is about 100ish degrees it really depends on the one you buy they work by detecting infrared radiation creatures you know they all emit heat in the form of infrared radiation so it's not necessarily detecting movement but differences in infrared radiation or that body heat so I'm going to pop this little lens off it's consists of two main parts this Fresno lens right here here which helps to form a cone with which it can like gather this infrared information and then focuses it on the sensor so here you have your pyroelectric sensor it consists of two electrodes one on the right and one on the left well if a hot object walks by hits this electrode first so now it detects a differential because this electrode has not been hit then it continues and hits this electrode then it's like uhoh so we had a difference in heat here it sends a high signal to a relay perhaps to control a motor or a microcontroller where you can orchestrate far more advanced movements the connections are super easy on this so VCC is our power so we'll remember that the power is always on the right side as we look at it and then we have our out this is where it's going to send a high signal or no signal at all depending on whether or not it detects motion and that's our middle and then our ground is going to be on the left a lot of these guys will have two different potentiometers one will adjust the time delay so once this thing goes on how long do you want it to stay high for typically it's anywhere from 3 seconds to about 3ish minutes now some of them are different you have to read your data sheet so if you turn it all the way counterclockwise it decreases the time delay turning it clockwise increases the time delay that means that it stays high for longer this one right here controls the sensitivity so it can detect anywhere from 3 to 7 m which translates to 9 to 21 ft so if you turn it counterclockwise then it'll detect closer up and then if you want it to detect further out then you turn it clockwise to increase that detection range so we're not going to mess with this one too much because we're working in close quarters like I'm right here and then the other feature that a lot of people don't really explore is this jumper right here there's two different positions and good luck reading the labels on this thing the L is labeled right here the H is labeled right there next to this pin so this is in the L position so what does that mean once motion is detected I have this turned to the shortest possible so it's going to stay locked for 3 seconds that means more motion can happen it's not going to read it it's going to lock out that motion until that 3 seconds passes and then it'll reset and then it'll be able to detect the motion again now we're in the H position and that means repeat trigger so say we have this again set for 2 minutes let's say so I walk by the two-minute timer counts down but then I walk by wherever it is in the countdown it's going to start the countdown again and start the countdown again and start the so this you can keep keep this bathroom light on so long as it detects some kind of movement it'll keep adding to that timer for your project you might find the single trigger works the best for other projects the H position might work better so I'm going to go ahead and put it back on the L the single trigger so that way we can trigger it multiple times it'll reset and we can try out different things a lot faster one of the beauty of these things is that it requires very little voltage so we're going to go right off of the Arduino 5 vol pin which I'm it's just like right there and so now let's connect up the signal pin going to put this guy in the middle and we'll do yellow like caution the signal is coming or not coming and I'm going to put this into pin 12 so let's all try and remember pin 12 and finally the ground I guess I could have made a common rail but I'm just going to jump this to the ground over here just to make things kind of simple just test this thing did I wire it right and I'm going to start by setting up my pins as variables you can hardcode them in the code but inevitably inevitably your projects will get bigger and then sometimes you got to jump one pin to another because you change your mind and then you got to find that pin and change it all on your code so I like to put my pins all up here so that way I can change them in one place cuz I already know that's coming our sensor we'll call it peer pin yeah it sounds pretty descriptive right so I had him hooked up to pin 12 of the Arduino so we need a way to track this motion status I'm going to call it motion status you can call it movement status whichever you want and so this I'm going to set to low or zero to start so that way the sensor when it starts up it sometimes will go crazy and it's got to like calibrate for anywhere from 30 seconds to 60 seconds so it's a good idea to kind of give it space to C calibrate we have to tell the Arduino is what we have hooked up to pin 12 is it going to act as an input or an output what mood is it in because it's a sensor it's going to be giving us information it's going to be giving the Arduino information with which we will do as we please for this it'll be the pure pin and we're going to turn that into an input so the pin mode of the pure pin is going to act as an input and we are going to use our serial monitor so we're going to do serial. begin and we're just going to use our regular old 9600 so let's start with the fun stuff in the loop so what's the first thing we want to do once we get into the loop well we want to read the sensor like what's happening is somebody passing in front of the sensor we don't know so we're going to find out so we want to get that motion status and we get it by digital reading the pure pin after you read the peer pin well we got to do something with that information so in our case we're going to find out if it's even rating if the motion status is equal to high what do we want to do we want to serial print the phrase motion detected right intruder alert another thing that we can do here is just add a delay of 30 seconds or 60 seconds we can do like maybe a second for good measure you just want to give it a rest as it calibrates oh but we also wanted to print when the motion is not detected so or else we just wanted to print I don't know like motion ended it's not very climatic right let's upload this code everyone cringe that took a little longer than than what I wanted that to take now you saw the blue LED flash real quick and that is because it's connected to pin 13 which is also part of the built-in LED of the Arduino so anytime code is uploading you'll see it do its little flashy dance let's bust out the serial monitor which I'll kind of put oh so you can see how fast it's reading and see you saw a motion detected that's it calibrating like trying to figure things out it's figuring out its environment but now it looks pretty steady so you can see motion detected and I have it turned all the way quick you know so maybe it'll last maybe 3 seconds is there a way that we can get it to print when motion begins one time and then this is when the motion stopped and then if it does it again motion started motion ended just got to tell me once you don't got to keep nagging me here rather than reading it during the ACT itive output whether it's high or low why don't we read it only when the state changes so in order to do that we're just going to rearrange this code a little bit here we probably need another variable up here because here we're tracking the motion status is it high or is it low but then we need to take this information of whether it's high or low so we can track the state change basically so let's come up with another variable and I'm going to call it the pier State the state of the pier sensor and we're going to start it off low so it doesn't go berserk depending on what you have hooked up to it so I'm going to go down here and see what we can change now normally this is kind of the way you would do it and here you would activate components so whatever components that you want to activate as soon as it goes High boom you activate those components and same thing here this is where you would put the code to deactivate components deactivate components how do we just activate it when it senses a state change rather than during the state change so that way we don't get so crazy with the serial monitor let's instead if the pier state is equal too low we now want a Serial I'm going to just move this guy over here cut and paste him over here I will shrink him in a little bit and so then I'm want to go down here I'm going to write this out and then I will explain it to me to you and to myself as well you know so what's happening here is if motion status is high a lot of people like to activate their components as soon as it goes high and you saw how fast that reads so it's going to say it's still high it's still high it's still high so it'll keep reactivating what you have there sometimes it's easier to do that on a state change so here it's basically like okay if the pier State the current state is low which it was before turning High you want to print motion detected and swap the state indicator to high so you're not swapping the the signal to high that's already been done here all you're doing is just saying okay the variable we're using for tracking this update that to high update it from low to high so then here we're going to do something similar rather than having it read we're going to do if the pier state is equal to high because it's about to go low it's about to go low people so what do we want to do well now is when we want to read take a reading I'm going to put this over here and because it has changed States from high to low we want to update our tracker you know the way we're tracking this thing and we're going to update it to low starting from the top we have set up a new variable to help us track when the change happens cuz that's all we're interested in not what the state is but when the state changes in the loop we're going to take a reading and we're going to do that by reading the peer pin which is pin 12 if the motion status is High the pier State because at the very beginning of this we set it to low well we know that's going to be low so if the pier state is low which is true motion detected and update the pier state to be high say the movement has passed and now everything has gone back to normal it's quiet because our Pier State the state of change was set to high in the previous code it's going to be true that it's high so that means print that the motion has ended because we're going to update that state to low a change in state has happened okay motion detected motion ended see how it's not like printing over and over again just once and when it ends so let's see if we can get the LED to come on with the motion detection and then come off with the lack of motion detection just like we did with the sensor let's go ahead and set up variable for our LED and that was connected to pin 13 oh we got one more pin mode to set up why don't we set up the pin mode of the LED so whatever is connected to the LED pin which is pin 13 we want to make that an output because we are going to be outputting voltage to it to turn it on when necessary and outputting no voltage to turn it off when not necessary let's get into this Loop so first thing we want to do is grab the motion status has something walked by our sensor and we're going to do that by digital reading the pier pin which is pin number 12 so if the motion status is high if that reading is high well what do we want to happen we are going to digital write the LED pin hi we're going to turn it on or else that means that the pier pin is low we're going to Let's deactivate it let's turn it off so digital will write the LED pin low I think I'm too close I'm actually going to hold it up here and wave my arm way further out yeah that's much more detectable I'm going to try and increase that time delay by just turning this thing clockwise we don't even have to adjust the code this potentiometer is so sensitive you'll just turn it a tiny bit and then it's like okay enjoy waiting 10 minutes now or something ridiculous all right so that's a lot longer and you can see the change is immediate we don't we're not controlling the timing through the code quite yet any day this is what I mean by you change it just a tiny bit and now it's like stuck on say here we only want the LED to be high for let's do it real short 50 milliseconds so we're going to add in a delay of 50 m seconds and then we want to turn it off so what happens here theoretically as soon as it detects that high coming in from The Wire it's going to turn our LED on and hold it there for 50 milliseconds and then it's going to turn it off for the remainder of the time well that's strange it's not we want it only on for 50 milliseconds what's happening here so so no matter what it seems like it's disobeying my code and I can keep this on for maybe if we change the setting here I've increased the time delay and it keeps it on longer so it's like totally not doing what I want it to do and why is that this is flying by so fast that it's reading that pure pin multiple times per second so what happens is it reads it and it's like okay I got a hold it for 50 seconds and then it immediately reads it again it's still high because of the time delay and it holds it for another 50 seconds so it's just stringing them all together to make it look like one solid it's staying on even though we have like this low so why don't we increase the low so maybe we'll add a delay anytime you use a delay it's called a blocking piece of code meaning during these 50 seconds it's doing nothing else than carrying out the previous line or the previous situation so here why don't we lock it out for like 1 second and see if that makes a difference all right so that's working a lot better it is just doing a quick blink and it holds for that 1 millisecond and you can see it blinks generally twice because the time delay is set for about 3 seconds so it can get two blinks in there whatever code that you put in here right after it senses the high is only going to last so long as that time delay unless you start to use really long delays which now you're not reading your sensor as often and that might be Troublesome for what you're trying to do it may not let's try doing it according to the state change it's changed from low to high why don't we put this business in here and now this will actually allow us say you want this to only blink remember we were trying to get it to Blink just one time and not multiple times and then stop let's see if that will do it so it only blinks the one time whereas before when we had it during the current state as long as it was reading High it just kept it on on on on the whole time so now if you just need one thing to happen putting it according to the change State making that variable allows you to do it and this is where you have to experiment whether you want things to happen when the state changes versus for however long the state is on I guess for good measure move him to here so those are two ways of getting action to happen with a pier sensor you can do it as soon as it turns high and so long as it's high the thing keeps repeating what it's doing or if you want sometimes a little bit more control you can do it according to a state change why don't we keep the light solid when the pier is low meaning there is no motion you don't want to walk into an attraction and everything's off unless you're going to do a huge scare so why don't we scroll to if the pier sensor is low so what do we want to happen to this pin we want it to be high so we just want it a nice solid blue as people open the door and they see like a a blue glow and then let's go back up to if the motion status ends up being high so what do we want to happen let me go ahead and code this or remove this so we can have the lights blinking which we kind of already done so why don't we get it to like blink 10 times or a certain number of times so let's do that with a for Loop we could do digital right high and digital right low with delays and copy that 10 times that would be like such the rachal thing to do but let's use a four Loop and four Loops are great for anytime you need repetitive motion so of course we have the one LED so we're dealing with a oneeyed pirate I'm going to declare a variable right now you can declare them up top or you can declare them in the middle of a function so let's call it blinks and we're going to start at one blink and and so long as the number of blinks is less than or equal to 10 blinks what are we going to do and this is blinks we are going to take our blinks and we're going to increase by one blinks Plus+ all it is is short code for blinks equals blinks + one that that's all it's short for so it saves us some room right so long as this condition is true where we're going to start the blinks at one and then it's going to go all the way up to 10 blinks what do we want to happen so we want it to digital write the LED pin high and then we want to delay let's blink it every half a second delay 500 and then we're going to turn it off and we're going to do a delay for half a second so there it's blinking because I set it off with my hand let's see if it'll return to the solid and did I goof the code it is counting to 10 and then turning off rather than doing our nice little glow oh my bad I accidentally forgot to delete this line of code all right it's them brackets every time oh here we got more bracket issues so I usually like to start with that one's matched up and you'll see once you get on the same line as a bracket it'll highlight the bracket that it belongs to which is this guy here so that belongs to the else this belongs to the loop okay so I think we got extra brackets now down here 2 3 4 five 6 7 8 9 10 and on there we go so this is when it senses no motion we were cursed by brackets finally if you got an LED we blinked it we got to fade it right how about when it's off maybe the LED will fade on and off off and then when motion is detected it's going to freak out and it's going to like blink really fast we're not going to Blink it any certain amount of times so why don't we get be careful with the brackets okay so I'm going to get rid of this for Loop right here and because we're only blinking this so long as that time delay is in existence well I might as well put the code here in this if statement situation so we're just going to Blink it real fast so the LED pin we're going to Blink it high and I kind of liked the 50-second delay it was kind of like the freakout delay and then we're going to digital write the LED pin low and then we're going to hold that for 50 seconds all right so as soon as the signal comes in hot we're going to just start blinking just blink it until it goes low here we're also going to fade it out so long as it's low so we don't really I'm not going to do this on the state change I'm going to do it here and you can experiment with both kind of see what happens to get it to fade well we don't want to use digital right because digital right is either on or off we want nuances in between so we are going to use analog right and that way we can use values of 0 to 255 zero being off and 255 being the brightest so we're going to analog write the LED pin by oh we don't want to put a number here right we want a variable so it can move like blinks we want it to move through some numbers so let's call it brightness let's scroll back up to the top and set some more variables so let's do brightness and we're going to start it at the very beginning of the sketch off and then if we're going to increment we need an amount to increment it by you can increment it by five to cycle through 0 to 255 really quick now the bigger your increments the more choppy that fade is going to look so why don't we do like a I'll call it fade factor a fade factor of one so that way it's it's only going to go up or down by a fade factor of one so let's go back down to our Loop when it reads Low we are going to write this LED pin by the brightness which starts at zero because we said so at the top and so this brightness we're going to increase it I mean I suppose we could do this you know by one every time but what if you want to change that fade factor and you want to play around with different numbers it's easier to do it if you make a a variable so by the fade factor which is one at some point you're going to get to 255 and it's going to loop back around 256 257 258 well we need to cap it somehow right so that way it knows once it gets to 255 it needs to start scaling back down so let's go ahead and do that so if the brightness is less than or equal to zero or and the double sticks that's your or or the brightness is or it tries to go above or equal to 255 well what do we want to happen we want the fade factor to turn negative so it turns into a negative one there we go and we maybe might do a delay of 30 so that way it doesn't Fade up or brighten up so quickly and fade down that your eyes can't even see it because you saw how fast that Arduino Loops so it'll be like bright out bright out you know you won't even be able to see a fade effect if we don't just kind of slow it down and so you see that we have delays going on up here we have some delays going on down here if you start adding components this is going to become a problem if you just want to do some simple animated things you can stick with the delays for the future lessons we're going to be moving away from the delays everyone cringe big time oh it's blinking I totally know why I goofed so why is this thing blinking and it's not doing the beautiful fade well it turns out that it is connected to pin 13 which is a regular onoff pin if you want to tap into the fading effect you have to to put it into a pwm pin you see some of these pins and they have like squiggles on them those are your pwm pins I need to move this little guy over here and let's move him to pin 10 and now he's totally off and the reason is we still have in the code as him being on pin 13 so let's swap him to pin 10 in the code thank goodness we set up our variables up here because it's just as simple as that so let's see if that works oh look it's fading down I guess because I'm incrementing by one and so every 30 milliseconds you got to wait for it to cycle 255 times maybe the fade Factor can be five so the fade Factor rather than adding one now it's going to be adding five so let's see if we can speed up this fade it's supposed to be Ambiance but this is a little too Ambiance like before people come in they're going to be like wow you're going to put them to sleep so now it's fading in and out a little bit faster because it's jumping by factor of five freaking out as it should and then it goes back to the Ambiance and then it's freaking out and then going back to the Ambiance we now know how to do a bunch of different LED variations using delays and we were able to do it in a way that didn't really trip up the if you come up with more elaborate LED patterns using delays it starts to make the sensor less sensitive we want a very sensitive sensor next time we are going to start adding on more components not only the LEDs but we're going to be adding servos Motors MP3 players and we're all going to get them to talk and do different things at the same time all controlled by our motion sensor hit the link for the full tutorial where all the code will be there for you to copy and paste modify to your needs and then I invite you to join our community where you can post this code we have a live build session every month where we join each other via Zoom work on our projects ask questions see you next time guys bye
Info
Channel: Rachel De Barros
Views: 22,100
Rating: undefined out of 5
Keywords:
Id: -jPw4BWou5s
Channel Id: undefined
Length: 30min 17sec (1817 seconds)
Published: Fri Feb 16 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.