Motion-activated Servo Motors with Arduino and PIR Sensor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone and welcome to part two of our motion activate all the things with a p sensor last time we started with LEDs and we created a variety of patterns that are motion activated so today we are going to look at servos I have my P sensor right here I already have it connected and the connections are really simple it's just three power signal and ground so when it detects a different an infrared radiation walking by this Fresno lens here a high signal will come down this wire and into the Arduino and then we can do with that information whatever we want mm most of these sensors have your time delay and your sensitivity this adjusts for distance so it can detect typically from 3 to 7 m 9 to 21 ft here's your time delay and if you set it all the way counterclockwise like I have it it stays high for the shortest amount of time it's about like 3ish seconds and if you turn it all the way clockwise then it'll stay on for about 3ish minutes it depends on your sensor and then finally a lot of people overlook this feature right here which is our jumper and it can be in two positions right now it's in the L position meaning single trigger so if I have this set for say 2 minutes and I walk by it'll trigger it'll go high for two minutes during those two minutes I can do a whole song and dance and it's going to lock out it's not going to read anything after the two minutes It'll reset and then we're ready to song and dance again if you put it in the H position and it's just a matter of like pulling this little thing out and then just kind of repositioning it h position is repeat trigger so that means say we have it set for around two-ish minutes I walk by the two-minute counter starts and it stays High I walk by again wherever it was in that counter another two minutes gets added and another and another until it's totally not sensing motion for 2 minutes and it'll go back to low so it really depends on your project what you're trying to accomplish where to put these jumpers I always like to try both and heck you know see what works best let's get this guy connected here and if you don't remember or not sure these things usually pop open VCC signal and ground because this requires so very little power I'm going to hook this up to the 5vt of the Arduino board and I'm just going to I mean I guess I should use the power rail but it's so convenient right here and as Bob Ross says everybody needs a little friend a wire clouds Birds trees they all need friends the signal wire I'm going to put in pin 12 I think that's where we had it the last time so we're just going to keep it there here I have Ye Old micro servo if you are only powering one you can go ahead and just hook it up to your board I always go the separate power route because inevitably I always start a project and it has a tendency of getting bigger and bigger and bigger and then I got to move wires around and things like that so I'm going to already anticipate that this project is going to get bigger so I'm connecting the ground and the power to the the breadboard here and the signal wire I'm going to put into you can choose any pen I'm going to choose pin n now if you're going to use the servo library in the Arduino code you can connect it to any pin it doesn't have to be a pwm pin with the squiggly the pulse with modulation pin now if you're not going to use the servo library then yes definitely connect your Servo to one of these pwm pins so he's all connected up and finally we are just going to add the power when connecting external power you always want to make sure that all your components share a common ground going to the Arduino say this wasn't here your Servo would not work all right to the code everybody first thing I like to do is to Define my pins I kind of just stick with the beginner friendly int for integer the P sensor I'll just call it pure pin and that's hooked up to pin 12 that's the one thing I remembered for the night we can add the servo stuff in later let's just check the pier we need a way to read the pin and have a variable to store whether it's high or low so I'm going to call it motion status I think that's what we called it last time motion status and I'm going to set that to zero at first because a lot of times when you turn on or you upload the code to the Arduino that Pier sensor is calibrating so it'll just falsely go off so it's important that if you have stuff connected to it like we have Had A Silly String shooter that it just doesn't accidentally go off let's uh create another variable where we'll track the state change and I'm going to get into more details about this later on we kind of went into a lot of depth with this in the other tutorial so you can always follow that first and then jump back you know over here join us let's move on to the setup and one of the first things I like to do or don't like to do cuz I usually don't do it I just wing it a lot of the times but it's a good idea to set up your serial monitor it's almost like if you don't set it up then your code is just going to bomb you know but if you do set it up then it's like H they love to like make you spend hours trying to find out what's going on so we have our serial begin and let's set up our pin modes so we have to tell the Arduino how you want that PIN to act so the pure pin is connected to pin 12 12 and we want that to be an input because that peer sensor is going to be sending information inputting it into the Arduino and we're going to do stuff with that information and you'll notice here the pin mode is whatever pin is you're talking about and whether or not you want it to be an input or an output you can hardcode the pins I don't like to because inevitably you're always going to have to move stuff around as you add so that's one of the reasons I like to put all my pins as variables if I have to move it to pin 10 I have only one place to change it versus everything else in the loop what is the first thing we want to do we want to read that motion sensor because we want to know what's going on like should we activate things or not so let's get the motion status and we do that by digital reading the peer pin so we're going to know if it's high or low after that if theemotion status is equal to high what do we want to happen well we're not going to activate the servo just yet we just want some kind of message that says hey the motion sensor has gone off for reading and tracking motion change I like to use a Motion state so that way you just know once when it goes from low to high and then from high to low so only when the change happens not like the entire time that it's high so in order to get that to happen if the pier State and remember we made is a variable for that is low which it is so up here we set our Pier I call it peer status we'll call it Pier state to zero which is low and so when it comes down here if the motion is high well we had already previously set it to low so we need to toggle this now to high and it doesn't mean that it's toggling the pier centor to high because it's already reading high that is toggling our variable which is we're going to use later on to control motion and all that so we're going to toggle that too high but before we do that let's before I forget let's just serial print the fact that motion has been detected and then we're going to take that Pier State and we are actually going to set it to high all right so we covered everything that if the Pier sensor sends out a high signal but what if it sends out a low signal or else you know if the signal is low uh what do we want to happen well we want to print that the motion has ended so we're going to do kind of the same thing where if the pier state is equal to high because before it was high now it has gone low so we need to update our state change label but before we do that let's do the get the serial monitor thing out of the way print line okay so motion ended all right and at this point we want to toggle our state change indicator to low so it matches what's going on with the pin oh and another important thing in the setup we talked about sensors needing to calibrate so I always like to add a delay just to kind of pause everything to give it time now for a p sensor a good delay is anywhere from 30 seconds to a minute now I don't want us waiting around like for it to you know calibrate every time we want to run examples but to help you guys remember we'll just put in a second so we're going to upload the code you know the rule everybody cringe side eye squint oh it's hanging oh okay when you know when it hangs it's like oh no it's going to turn red airs all right so I'm going to pop up the serial Monitor and I'm going to back up slowly oh it detected so that that is is it calibrating right now and so that's why you want to set it for that 30 seconds minimum okay so you see it only prints the one time and I have it set to the three-ish second delay so sometimes I get a little too you know antsy with the motion so we have our Pier sensor working correctly now let's tie in the servo and have it do some kind of action let's go Servo pin and where did I put it pin n right we're just going to go for it pin n oh and when you're doing a Servo very important we got to add the servo Library let's include a library and we're going to go to Servo and up Pops the code for us now somewhere in here maybe I'll kind of go over here we have to create a special name for our server where we're going to use this name to refer to things so I am going to call it and you see it turned color so it it's ready for that name you can call it anything you want so since this is like a haunted house attraction I'm going to call it scary Servo so that's the name that we are going to reference from now on going through the code we also want to set up our Servo so our Servo is connected to pin 9 but the Arduino doesn't know that and yes we did like you know specify it up here but for the library it wants to know where this this whole thing is so we are going to put it uh Servo we're going to attach it or ours is called scary Servo and we are going to attach it to pin n but instead of hardcoding it I'm going to go Servo pin that we had set up up above just like we have a delay it's a good idea to start our Servo zeroed out or whatever starting position you want to do your Servo so I'm going to go scary Servo do right and let's write it to angle zero so everything is zeroed out as we start we give a little time for the uh motion sensor to kind of calibrate and let's go here where do we put this code to make this Servo move there's two places where you can put it so one place is here and this is your option one to activate things activate things or you can put it down here which is your option two to act acate things we're going to go through a couple examples and you're going to see exactly the differences it makes whether you put your movements in this very first option where so long as the pin is high the movement will keep repeating itself over and over again now if you know Arduino it goes down the code super fast and then Loops back up and then Loops back up so here if you blinking an LED it will continue blinking until it reads Low now now what if you want to only blink the LED like twice well it's just going to keep repeating that blink so it's more difficult to do so let's start with the place where 90% of the people put their code which is here and then we'll take out option two and we'll deal with option two later and so let's activate the servo so if the motion status is high what do we want to do well we want this scary scary Servo to turn to 90° say it's like a jaw of a scary skeleton you know so when it turns to 90° it goes and here you can connect a sound like a scream or a laugh or something like that and the jaw opens to 90° here we're going to skip to the else so what if there's no motion what do we want to happen and here it's the same thing here we have option one for adding motion or you can add it within this if statement which is your option to so let's stick with option one just like we did up at the top and what we're going to do is take scary Servo and we are going to close the jaw so it's going to go back to zero I'm going to upload it all right everybody get ready I can't believe or not oh I said this the last time I said I can't believe we're it's like uploading with no problems and then it just got jacked up in the end we're like looking for brackets and it was like a Manhunt for the brackets good so we have like our nice scary skeleton laughter Let's uh do a Servo sweep do we need to set up any more variables ooh one for position so we can track the position of the angle of the servo so we'll call it angle ooh so when you see something light up it means that it's usually a keyword to do something so when I'm setting up a variable and I see something light up I pick something else we'll call it position so I want to call it POS for short so the position let's start the servo at zero I like to kind of zero everything out and we're still working with scary Servo and I don't think there's anything here for us to do so let's get into this Loop business and let's stick with option one so where this thing is here we're going to go ahead and get rid of that and do a sweeping type of motion if the motion is high how do we sweep now we can write code like you saw scary Servo do right 0 degrees scary Servo do right one degree two degrees what if we want to go to 180 that's 180 lines of code I don't have time for that a for Loop is great for anytime you have to do a repetitive motion blinking a led a certain number of times or lighting 10 LEDs sequentially saying same thing for position if we need to move to a certain number of positions it'll help us count four and we're referring to the position uh position is equal to zero so the first Clause is what's your starting point we're going to start our position at zero and then we want to move it to position 180 we're going to go with the full way so so long as the position is equal or less than or equal to 80 what do we want to do well we want to continue adding to that position whoop and this is just fancy for uh we kind of went through this the last time uh position position plus one so that's all this is just kind of a short hand for writing something like this so how are we going to accomplish this motion we're going to take our scary servo and we're going to write it by the position and we're going to give it a delay of 7 seconds so that way it kind of moves pretty quickly here so if you've never worked with a for Loop what is this doing it's taking our initial position here it's going to take it up to 180 of an increment of one at a time so it's going to write whatever the position is and do it every 7 milliseconds so it'll start at zero it'll write position 0 then it's going to go back up here and say okay is 0o less than or equal to 180 it is all right so we're going to add one to it all right so increase it by one and take seven milliseconds to do to do that and it's going to go up again and this is uh ignored it goes to here is one less than or equal to 180 why sweetly so it's going to add another one making it two and it's going to keep doing that and doing that until it tries to go to 181 and it's going to say is 181 less than or equal to 180 it is not so it's going to jump out of this whole thing and go on to the next thing so let's rate that uh next thing so at some point the servo now is at 180 we need to bring this Servo back down to zero so it does a full sweep like that so to do that it's basically taking the code and we're starting at position 180 now cuz that's kind of like where we ended and how do we want to modify it so long as the position is greater than or equal to zero what do we want to do we want to take that position and we want to subtract one and again that's fancy for position equals position minus one and how are we going to do that we're going to do it very much in the same way so we're going to take our scary Servo and write the position and we also want to take 7 milliseconds per increment you know the rule people I'm not oh I'm not going to say anything cuz I'm going to jinx it if you're real close it doesn't sense all that well so I'm trying to stretch my arm there we go if I stretch my arm out of range we have it set to the shortest time what happens when I start to increase the time interval there we go and a little goes a long way like all of a sudden you're stuck in like the time delay of Doom so I'm going to move this back and you can see see how it's going back and forth if you wanted Just One Sweep well so long as that pin is high because we have our block of code in that section it just keeps checking so quickly and it's like oh wait it's still high it's still high it's still high so repeat repeat repeat say for your project you only want that sweep to happen one time well option one is to turn that little knob to the time delay that magically gets your animation it just syncs up with your animation that's a lot of trial and error the way I prefer to do it is to use the state change variable so we're going to go back up here and here it's going to read the pin and if the motion status is high rather than put this block of code here I'm just going to take this cut and paste and this is where like the problems always start when you start cutting and pasting things and here I'm going to put it here and I will tell you why the wire is reading high so the Arduino knows that the wire is bringing in a high well previously we knew that it was low because there was no motion so our state toggle is set to low and it's going to PO it's going to print motion detected so let's go ahead and turn our toggle to high this is a singular motion so now it's going to carry out this line of code and carry out this line of code so it'll do it one time because we're only toggling it one time and so long as this is high this will stay toggled to high one time so what happens when we want no motion what's the no motion motion that we want technically we can leave this here so long as it reads Low it'll keep re-executing this over and over again like set it to zero set it to zero set it to zero for good measure and for consistency I'm going to put this down here as well and basically in the else that means that that wire is reading low which means it's switch from high to low let's take our state which is high and let's switch it to low a singular action and so it'll write one time that to zero before looping back and taking another reading of our Pure pin so let's see if that works better and that is working as expected we expect it to turn once because we have the lowest setting that's all it's got time for is one turn what happens if we are to increase the time setting all right it just went off and look it only turned the one time it was locked in motion detected for a while because we turned up that time delay so it's like locking out for longer but that Servo only turned the one time which is what we want what if you have like a jaw that needs to create different articulations so now you have to like animate a whole sequence instead of the sweep which is all this code over here and I'm hoping I'm removing the right amount of brackets say motion is detected what do we want this like a skeleton to do scary servo WR we'll make them go to 90° you know he's going to say something and we're going to hold it there for maybe 300 milliseconds and then we are going to go to zero and we're going to hold that for I'm just going random here folks 100 milliseconds and then maybe we'll pick a third position he'll go to 180 so I imagine this is a in that you're trying to time your Servo to now of course there's much easier ways to tie servos to audio but that's for another Workshop we're going to pretend that the phrase is very short so you can manually who you can manually kind of do this all right yep say we were to move this entire block of code to right there what would happen is if the time delay say we set it to a two-minute time delay that sequence would repeat repeat repeat repeat and you only want the skeleton to say welcome victims like one time you don't want to like scare them away I had a blast we've covered motion activated LEDs now servos so the next is going to be Motors and then an MP3 player and then finally we're going to put all this together and orchestrate misfit toys type of animatronic this is shot live so there's probably things that I forgot to mention and I'm going to watch this back and go oh man so there will be a tutorial Link in the description that you can follow along it's going to have all of the code that examples that we have done so you can copy and paste modify to your whim and of course go back onto the community and post it so I invite you to join the community link for that is below share your projects I love to see them and of course we have our community Zoom builds every week and this is a chance where I bust out my projects you guys bust out your projects and it's via Zoom so we can see each other face to face talk to each other kind of show where we're at where we're getting stuck and kind of use that team mentality to help us along so until next time guys bye
Info
Channel: Rachel De Barros
Views: 23,725
Rating: undefined out of 5
Keywords:
Id: PEg4bMFyHeM
Channel Id: undefined
Length: 25min 20sec (1520 seconds)
Published: Sat Feb 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.