Trigger DC Motor with PIR Sensor & Arduino - Complete Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone and welcome to part three of this live Workshop Series where we are motion activating all the things with a p sensor and an Arduino today we are going to tackle DC motors so by the end you're going to know how to wire it up along with the p sensor and we're going to go through a couple code examples that you can then take modify to your whim for your own projects let's start with our P sensor and for that Dawn ye eyeballs everybody if you got mature eyes go ahead and get them set up we have our time delay on the left if you turn it clockwise it increases the time that this remains High I have it set to all the way low so it's like 3ish seconds on the right we have our sensitivity how far does it sense anywhere between 3 and 7 m I have it short range at about 3 MERS cuz you know like I'm right here like sense me we have our jumper or trigger mode selector here and it's in the L position which means single trigger mode so that way a motion happens and I have this time delay set to about 3 seconds that means it's locked out for 3 seconds and then it resets and the other position is just removing this jumper and jumping it over to this pin that's the H position meaning repeat trigger so if I had this set say for 2 minutes I walk by the two-minute countdown begins I walk by again the two-minute counter restarts and it keeps restarting definitely a better application of the trigger Jumper in the H position if you're using this for a room light or a bathroom light where you don't want it to keep turning off so we're going to leave it in the single trigger mode that way we can trigger things kind of quickly then we have our pins so VCC on the right our signal pin in the middle this sends the signal whether high or low to our Arduino and then ground on the left and if you can't remember you can cheat I do this all the time you pop off its Noggin that's why all the electronics take off running they're like oh she's taking things apart run save yourselves this guy is like it's too late for me we have VCC out and ground right there the first thing I'm going to do is basically connect our positive rail to the five volt pin of the Arduino and then I'm going to tap this little guy in here so he can be all happy and he only requires 5 volts so it's a really easy hookup our signal wire I think we've been using pin number 12 of the Arduino so we're going to stick with that it's been our lucky pin I have a positive coming here but I have no negative so I should probably hook that up I'm going to put this in the ground right here which is right close to the 5 Vols they can be like little buds right there they're BFFs so we have our power and ground hooked up we have our P sensor here hooked up now let's hook up the motor unfortunately it's not a good idea to hook up your DC motor no matter how small and no matter how little current you think it draws directly to the Arduino because on a best case scenario you will fry whatever pin you're using worst case scenario you will brick ify your board there so you can it as some outdoor decor a nice paper weight instead we need an intermediary and this is our motor driver and it's got a bunch of connections we're going to run through them here real quick but it's very important to pick the motor driver that contains the voltage and amp requirements to run your motor so this one is a pretty beefy one to run a windshield wiper motor I know a lot of you prop enthusiasts building homemade props this is like the go-to motor they're super cheap they have a lot of torque so that's why I'm using this as our example no I have him hooked here into his slot this can control two Motors so I can certainly connect another one there and you can see the black for ground yellow is for our high-speed wire this is a five wire motor and if you need to figure out what your wires do on your windshield wiper motor I got a whole video for that too so the orientation of your wires here largely usually doesn't matter in one configuration this will turn either clockwise or counterclockwise if you flip it then the Turnage will change as well accordingly it needs its own power and that's where I have this hookup right here I have this kind of like brick style 12vt 8 amp power supply to run a motor like this you need a minimum of 5 amps and you want to match up the volts exactly to your scenario and what this module does is is take power from the separate power supply shoots it down and Powers your motor it has nothing to do with the connections we're going to be making to the Arduino it separates the power thus not harming this Arduino so I don't know he might walk out of here today we'll see you can see two vertical rows of pins this vertical row is for the other motor slot that we don't have a motor hooked up into so we're going to concentrate on this first vertical row we have our five volt so the motor needs power we took care of that but it turns out this Board needs power too and it's not using this power it needs its own kind of power well the Arduino can also provide 5 volts so why don't we tap him in here to the breadboard where we can suck the 5 volts out of here into our little motor module there so I have a jumper here and he's got a female end and a male end so prepare the power suckage there we go and he I'm actually going to move maybe these guys up here make sure not to reverse the the polarity there and he's going to go right there thus taking power and powering his own self so the next one is called Ena now this is a speed pin how fast do you want your motor to turn now if you want to control speed on your motor it's important to pick a pin on the Arduino that has the squiggly the pulse width modulation pin and I'm going to put him on six hopefully some of you guys are remembering these pins next two are called in1 and in2 those basically control the direction of your motor so if you send 5 volts to in1 it'll turn One Direction and of course that also has to do with how this is wir so we're going to figure this out in the code and in two makes it turn the other direction so those do not have to be pwm pins they can be regular old pins let's go seven and eight so in one is in seven and in two let's put that into eight oh and this has one more little pin left and that is our ground because it is super important that every component that you hook up the P sensor the motor or servo whatever it is you're using they all have to share a common ground with the Arduino so they're all kind of talking to each other so that's one of the first things to check if you get to your code and something's not working make sure you have that Common Ground here we have our sketch and the first thing I want to do is before we get started let's just make sure that P sensor work first things first let's set up our pins as variables so we have the P pin and we connected that to pin 12 there's a lot of different ways that you can set up your pins up here as variables I'm using int for integer it's kind of a nice beginner friendly way to do it so the first thing we want to set up after our pin is a variable to track the motion status is the P sensor high or is it low so we need a way to know we're going to start it at zero which is the same thing as low and then I'm going to set up kind of a label for us to track the state of the P sensor so the motion status will constantly read that pin is it high is it low and it's constantly updating but we just want to know when the state changes we don't need to know every millisecond whether it's high or low just tell me when it goes low and just tell me when it goes high so next why don't we set up our pin modes so we're going to forget the motor for now we'll come back to that and we want to set up our P pin or Pier pin as an input because it is going to be receiving information is the PIN high or low has motion happened and probably before this which is the step I always Skip and it's not a good idea to do so but sometimes I'm flying through this uh type of stuff so is the serial dot begin and that allows us to use our serial Monitor to Monitor whether or not this is high or low for the set up I like to add a delay before anything gets started your P sensor needs to calibrate and this takes about 30 to 60 seconds so if you have it hooked up to a motor or like a silly string shooter the way we had at one point it could just start going off so you want to make sure that we already set the state to low and we're starting with a motion status of low and just keep it that way for 30 to 60 seconds now because we want to like go through these experiments rather quickly here I'm going to put 2 seconds as a placeholder and hopefully that will help you guys remember to put some kind of delay here so now we are at the Loop and let's go ahead and first thing we want to do we want to read the pin is motion going on or is it not so for the motion status we want to put either a high or a low inside this variable so we're going to do that by digital reading the p IR pin what do we do with that information so whether it is high or low it's going to store that value in here so let's do something with that info and so if the motion status is equal to high not equal high but is equal to high what do we want to happen so right now our P state is toggled to low so let's Nest a if statement if you will so if the P State or the pier state is equal to low what do we want to happen and it is equal to low because we set it equal to low right here so right off the bat if motion happens it's going to jump in this if statement right here so if the motion status is high that signal coming from the pin is high jump down here oh and this happens to be true so let's execute the code Within These brackets now so we want to go ahead and print serial print the fact that motion has been detected all right and then we want to flip our P state to high so this is our own label that we're using to toggle it high or low it's tied to what's going on with the pin so as soon as the pin reads High our label toggles to high as well but it's a way to toggle it one time versus like hey I'm still reading High I'm still reading High I'm so you're like okay I got it I got it it's got to tell me once once it takes another reading what if it's not high anymore so or else if it's not high what do we want to happen well first things first let's address our P little label situation here so if the P state is equal to high which it would be because the sensor was just high so this is going to be high and so in that case we want to serial print the fact that motion has ended oh and I got way too many quotation marks going on there let's get rid of them and we also want to toggle our Pier State back to low so that way it's ready for another round so what's going to happen it's going to go right back up here take another reading and if it's still low there's no motion it's going to jump down here and it's going to keep our P State toggled low and then if it goes up and it reads that it is high then it's going to jump into this if statement rather than going to down here oh this actually needs to be like this oh now we have missing brackets I'm going to pop open the serial Monitor and let's see if we have our motion sensor working and that I wired it up correctly nice so our P sensor is working I say that we hook up our motor so we have our Ena pin and you can call it anything you want remember the ENA pin is your speed pin so you can call it speed pin if you want uh sometimes I like to just stick with what's labeled on the component so that way it makes it easier for me to go find it and so I believe that was six yes that was pin six the in one pin then was hooked up to seven CU I remember we went in order the in two pin then was hooked up to eight so I think that's all the pins that we need and then here we probably need a couple more pin modes because unlike the P sensor where we're grabbing information from it we are going to be outputting information to all those pins on the motor to tell it to do stuff we're going to boss it around a little bit so the ENA pin is going to be an output and then the in1 pin is also going to be an output and then what else do we have here the in pin as well an output all right so I think that's it we're going to leave our delay as our little placeholder there for the P sensor to calibrate and the rest works much the same so so I say let's just get this thing to move turn in a Direction so as we recall from the last time we can either put code to activate components here this is our option one or we can put code to activate components here which is our option two so let's go with option one first and then we'll explore option two so I'm going to get rid of that if the signal coming from the pin is high what do we want to do we want to digital write our in1 pin high so let's turn in one high and turn it in that direction so if we're going to do in one pin high it's good measure to then be very specific and say that into pin then is going to be low you can't turn the motor in both directions at the same time with a lot of motor modules that means break it'll turn on the brake so it's always good to specify one high and one low you can get away with not but sometimes there is some confusion so now we put the car in drive but nobody's stepping on the gas so now we got to step on that gas and we are going to do that by analog writing that speed pin so that was the pin and here you don't go high or low because it's not either like running for full speed or not R running at all much like LEDs you want to pick a value anywhere from 0 to 255 255 obviously meaning lead foot pedal to the metal so you can pretty much guess which speed I am going to pick right there heck yeah pedal to the metal people and if there is no motion we want the motor to stop and same thing here we can put the code for option one for putting our code or option two for putting our code and you'll find that I like option two much better for most applications than option one but because we did option one up top let's do option one here and keep everything kind of like nice so we're going to digital write the in two or the in1 pin low because we had it high here so we need to turn power to that PIN off and also we want to do the same to in two so that way the car is in neutral it's not going anywhere and we are doing all the misspellings today it's like we're collecting them all in this stream there we go so we've stopped everything now do we need to put a speed here I could put 255 but the car is a neutral it's just going to sit there stopped it's not going to do anything so we don't really need to put a speed here all right here we go let's see what happens oh can someone tell me why the motor is not turning yeah maybe I just learned this last week like there is still no Wi-fi power so that's a thing that I keep thinking has been invented but it has not here we go all right and you can see it moves for just as long that we have that time delay set all right and what if we turn up the time delay now it's going to keep going I put it into the time delay of Doom where it's going to take forever to turn off let's do that there we go let's now try and make sure that we have the opposite direction also configured correctly so here if the motion goes High I'm going to swap and do low here and and I'm going to swap and do high here so theoretically it's going to spin in the opposite direction at full speed people yeah yeah even touching it it's like don't touch me but yes guys look it's turning now the opposite direction next before you start any animation with a motor is to find out how low can you take the speed before the motor just stops working one would assume that a speed of zero means no movement which is true and one two three four 10 you know the motor starts to move really really slowly so why don't we take it down to 10 and see what happens can you guys hear that it makes a really kind of high pissed so why is it not turning the motor will always get 12 volts it just depends on how many times it's getting 12 volts per time interval so if you don't choose 255 and you choose a lower number say halfway then this will only pulse 12 volts half of the time so that means it's going to travel half as fast but it gets to a point where it's pulsing so very little times per time interval that it's not even enough to turn this thing so you'll write all this code orchestrate all this beautiful movement and find out that oh my motor doesn't turn below 7 and each motor is different so let's find out what our bottom is so if we remember correctly way back when we were working with this motor I think it was like 70 I think 70 was enough to get it moving oh it's so slow yep you see how slow that is and we have it set to the smallest time delay so the objective is to find a number where it reliably works say you only want to turn in that one direction at full speed for maybe a short amount of time say you know half a second so you would think that the motor is going to turn for half a second stop and then take another reading so let's see what happens seems a little long for half a second because remember this is about 3 seconds because we have our code Block in option one this keeps taking so many readings per second that it's like oh it's still high it's still high keep it turning keep it turning but say we want to only turn it for half a second what happens when I put the time delay up it will turn it in the direction for half a second and immediately be reading again and keep turning it so each half a second turn is all being added together for in creating this continuous turn situation so instead of putting all this business where the pin is constantly reading high and it's based on the absolute current state right now now now of the pen let's take this and we're going to cut it and this is where the problems start when you start copying and pasting and we're going to put it in this if statement here which was our option two and my indentations aren't quite right but we're just kind of copy and pasting here real quick so now what should happen is we're going to get get a high reading from the P pin once I wave my hand on it and because we have just uploaded the code the motion the P state is set to low so this is going to be true as well so it's going to say motion detected because the p pin is reading high so first thing we're going to do let's flip this switch on to high for our P State label and once that happens this is going to execute and it won't execute again until it reads Low and then it takes another reading for high so here we're going to go digital right the in one pin low and digital write I suppose you can only digital write the in pin there we go so let's see what happens now so here it is all the way down and you can see how it turns much quicker now so even when we had this set all the way down short time delay it still wasn't it was still going beyond the half a second so let's turn this up just a little bit not like the way I've been doing it yeah so now no matter what time delay I'm setting that thing is only turning exactly as we told it to it's one of the big advantages of using option two over option one if you're trying to Orr something specific the option two location is the better one to go and you'll notice that we forgot to put this in option two for good measure this should go down here as well so both are in the option two position you walk into a haunted house a spider that rolls down from the ceiling scares you and then rolls back up so how would we do that here let's make him roll and I'm going turn him back the other direction and we're going to get we're going to make him go that direction for like two seconds right and then we're going to stop the motor the spider has come from the ceiling we want to stop the motor and we want to pause it there for how long it's going to like scare people and stuff like that so we'll pause him for like 3 seconds obviously you need more than 3 seconds but for demo purposes then we want to retract him back into the ceiling so he's going to go back into the ceiling and we're going to reverse what we had so in pin was high now it's going to go low and the in two pin now is going to go high and we want him to retract also at full speed and so the ENA pin our speed pin we're going to pedal to the metal that whole thing all right and how long do we want him to take to get back maybe the same amount of time maybe like two seconds something like that and then maybe we want to stop it stop the motor so the I'm just going to copy and paste there we go and then we can else the motor is stopped in the option two position so no motion the spider stays tucked up hidden ready to to get you so let me go ahead and upload this it stops it's scaring everybody oh and then it rotates back all right and so it does it one time no matter how you have your time delay set so what's left to do well why not accelerate the motor maybe you want your zombie to start slow and then really go after the person and when you walk through haunted houses a lot of times you see things reset behind you sometimes a jerky motion back and forth like that can start to wear out your linkages so sometimes it's best for the motor and your linkages to start and just kind of ramp up that speed and then ramp the speed back down we're going to use our option two area and if motion is detected what do we want to happen well first we need to pick a Direction so this thing is either going to be in drive or in Reverse so let's digital WR the in one pin High that's kind of what we've been sticking with and then the other pen low and how fast oh we can't put a speed a we will have to create a variable so let's go back up here and set up some kind of variable so we'll call it M Speed motor speed we're going to start at zero and this is another good thing to do in addition to setting zeroing out any sensors that you have any Motors or servos it's good to kind of zero it out or as a Servo put it in the starting position that you want before anything happens let's set up a for Loop which is a great way to iterate through a whole bunch of numbers one by one so for we called it m spe where do we want this speed to start if it senses motion we want it to start at 70 because remember up at the top we set it to zero when this sketch start it's going to be stopped until it senses motion now if we did zero here it's going to go zero 1 2 3 until it gets to 70 so that motor is going to stopped for quite some time so M spe we're going to start it at 70 and so long as the M speed is less than or equal to 255 which is full speed what do we want to happen well we want to take that M speed and we want to add five to it yeah let's increment by five versus one so we're not sitting here like one or 70 71 72 it'll go 70 75 80 85 and that'll give us a nice smooth acceleration analog right and we're going to write to the speed pin whatever the motor speed happens to be how fast do we want the jumps to happen let's start it at 250 and then we can kind of finesse it you know from there our motor is not going at 255 and so we want to keep it at 255 for the duration of the high so let's keep this going at 255 or no let's keep it going at the M Speed which is going to be 255 what happens if the motion sensor now doesn't sense motion this thing is going at full speed so if it stops sensing motion the way we had it before it's just going to stop like that why don't we decelerate it down a little bit why don't we reverse the motor too so let's go digital right yeah I'm adding all kinds of fanciness so we had in1 was high here let's add in one pin low here and digital right our in two pin high so we're reversing Direction it is now in Reverse but we got to step on that gas but we will do so slowly as if for a nice deceleration so again with our for Loop we are going to start our M Speed because from when it was high it was 255 so we're going to start it at 255 and so long as this M speed is greater than or equal to not zero let's keep it let's do 50 cuz 50 I don't think that motor will be turning the motor speed we want to subtract five at a time so again we want to analog right the ENA pin by the motor speed and how fast do we want to decelerate I'm thinking pretty fast we want to retract our thing and get it ready for the next scare so why don't we do 50 millisecs per decreased interval because I set a speed here below what the thing can even do it's probably good it's going to stop on its own but if you want for good measure once it reaches 50 you can just turn them off by going digital right the in one pin low and digital WR the in two pin low because if you remember even though it's not turning at 50 it makes that annoying e sound so we don't want any of that if we just turn it off okay here it goes it's accelerating so slowly this is the most like boring zombie now it's decelerating cuz it Doesn't sense motion he came out you first of all like Grandpa zombie like hold on kids get off my lawn with his Cane that was the slowest acceleration I've ever seen so instead why don't we make him accelerate a little bit faster we'll change this to 150 instead of that why don't we put in like just 255 let's put in a hard number here and we have this the slowest time delay here in the code after it reaches 255 I'm just going to hold it for 255 so long as our Pier state is Taco to high if we only want it now to turn it for a select amount of time let's say we want to turn it for 3 seconds and that's not 3 seconds that is 3 seconds let's see what [Music] happens it's accelerating Grandpa zombie coming for you get off his lawn okay it's holding it and then it's decelerating we went through a couple code examples so hopefully you guys can mix and match a lot of the code I did build upon it expand upon it and heck then share it with us these are shot live I will probably come up with more cool code ideas so that's why I always write a detail tutorial which the link will be in the description it's going to have wiring diagrams so if you saw this Ras nness and you were like I don't know if I can follow that I'll have a nice pered up wiring diagram for you guys along with all the Cod examples that we went through so you can copy and paste modify and do what you will with it next we will be toggling MP3 players because what is the thing missing from the prop we got its eyes we got servos for its arms and perhaps a jaw we have motors for it to come in and out at you well it needs to make a sound cuz silent props are not as scary so it's got to like growl or or get off my lawn if it's our old grandpa zombie so we are are going to be tying an MP3 player to our Arduino and motion activated I had a blast I hope you guys did too and I invite you to join our community we get together via Zoom we also shoot these live you can join all of these live workshops chat in real time code along with me wire along with me so I invite you to join link for that is below as well guys have a great Spooktacular night bye by
Info
Channel: Rachel De Barros
Views: 12,179
Rating: undefined out of 5
Keywords:
Id: HXxeaelKVj0
Channel Id: undefined
Length: 35min 29sec (2129 seconds)
Published: Fri Mar 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.