Arduino Tutorial 38: Using a Tilt Switch Cut Off With a DC Motor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 38 in our new our improved our legendary Arduino tutorial series what we're going to learn today is we are going to learn how to incorporate a tilt switch into a DC motor controller where if you have something like a fan and it tilts over you can cut the motor off without injuring someone or starting a fire or damaging equipment so this is a follow-on to lesson number 37 which we showed you how to hook up a DC motor controller and today we will put be putting the most excellent tilt switch on it so I need you to pour yourself a large tall mug of iced coffee I need you to get out you are most excellent a Lego super starter kit what you don't have one look in the description down below we have a length that will hook you up 35 bucks you get a boatload of components and then as we go through these lessons you're using exactly the same hardware I am which might make it a little bit easier if you are just learning ok I also need you to go back and do lesson 37 if you haven't done lesson thirty-seven yet because lesson 37 really has the first part of this project let me get out of your way and for that we use the Arduino we use the power supply from the kit we use the l293d motor controller and we use a little motor with the fan and we got it all hooked up in working if you want the code and the bond out diagram for that you can go to the most excellent top tech boy comm website and then when you go to this website you can look for our dueƱo tutorial number 37 and on this we have kind of a picture of what we hooked up we've got the video describing how we hooked it up we have the connection dive around the schematic of how we hooked it up and then we have the code that we used and so you can go there and get caught up with where we are either just read this or watch the video but today we will in fact be moving on and what we need to do is we need to have all those things up and running and then we need to incorporate this tilt switch the tilt switch is in your a Lego kit let me get it where you can see it there the tilt switch is in your ela go kit and so I'm going to put it over here out of the way at about column 54 the left pin and what do I need to do I need to take column 50 and I need to go to a control pin I'm going to use pin number 2 as my control pin and then I need to ground the other legs so the left leg my left leg on the tilt switch is connected to the left pin the left hand of the tilt switch is connected to pin 2 on the Arduino and then the right pin of the tilt switch is going to be connected to our ground rail down here and in Lesson number 37 we showed you how to hook up that ground rail but we hook it to that ground rail and then I kind of messed that up when I stopped it I was trying in the last lesson to see how slow we could run it so we have it on a super slow barely running level ok so let's go over to our coding view and our coding view let me find a suitable window oh that looks like a very nice window and the code that I have up here in this window is the code that we finished with in Lesson number 37 and so you can go back and you can get this off of the lesson 37 on the website or you can go back and watch video tutorial lesson number 37 but we're gonna continue here you can see in the old video in the old program we had a speed pin which was pin 5 that controls the speed of the motor we had 2 Direction kenster one which was pin 4 door 2 which was three and we set a motor speed of 90 because we're trying to run it a really really slow and now today we are gonna have another pin and so we are gonna have another thing that we need to declare and this was going to be tilt pin and we put tilt tilt pin we're at - so tilt pin it's gonna be pin - if we have another pin we need to do another pin mode and so this is going to be pin mode and this is going did you guys do your homework let me know if you did your homework and then let me know did you do it the way I'm doing it or did you do it a different way did anybody actually do the homework or you guys just copied me what's wrong with copying me at some point I'm not gonna be here forever at some point someone's gonna need to know how to actually do this so pin mode the pin that we ordered was added was tilt pin and so we come down to our pin modes and put tilt pin and it is an output all right and we got our pin mode done okay so here I'm gonna take this I'll just leave this like this what this was the analog speed of 255 it's hard to start these things at slow speed so I give it a little kick to get it going and then if I give it a little kit kick it will run slow like at the 90 at the 90 level which we put in there and 90 as a pulse width modulation level that you're putting in go back and watch the earlier video if you don't understand that okay so what do we want to do I am going to take that out in fact so let's take that analog right of speed 10 255 that's the kick let's take it out let's take this delay out as well and it becomes a little clearer you're just setting the direction with Derwin low and are too high if you want to go the other direction put dyrone high in door too low and it will switch the direction and now we probably need to put a higher speed so that it can start by itself so for the speed of a hundred now we're kind of back to just the nominal setup now what we need to do is we need to read the position or we need to come up here okay this tilt can actually we're reading from it that was a mistake it is an input we are reading from the tilt pin and then what we want is this is a simple switch and I showed you in earlier lessons how you can use a simple switch by using an internal pull-up resistor on pin 2 and the way you do that is you set the pin 2 or the tilt pin to an input and then you do a digital right to tilt pin and you do a high so this is kind of tricky you're saying the pin is an input but then you write to it but what that does is it puts a pull-up resistor between the high and the pin and then what that means is if you hook this switch to it if the switch is closed then you're gonna see ground if the switch is open you're gonna see high so when it's just sitting there you're gonna be reading a zero when the switch is closed as in this position here if that doesn't make sense go back and watch the earlier lessons but in summary the trick is to do a pin mode of that pin as an input and then set it high and now we have the pull-up resistor in line with it now what we need is we need a value to read into so we'll say tilt vowel and we're not going to put a value in it because we will read that and then we come down here and we say tilt vowel is equal to analog read of what tilt pin okay if you are in the upright position you should read a 1 if you're in the Tilted over position you should read a 0 if you're upright you want the thing to keep running and so what you will say is is that if you are in the upright position if you are in the upright position if tilt thou is equal to is equal equal to zero meaning you are in the upright position then what do you want to do well you just can sit in you to say analog right of speed pin is MCM speed and that's whatever you set above and since I'm doing it in the if statement I am NOT going to put it up there because I'm doing it in the if statement and then this curly closes that one now what's the other case if tilt Val equal equal one then what do you want to do well if it's one it means it's turned over and then you will do an analogue right of speed pin and what do you want to write it you want to write it as a zero you could have also done a digital right low in that case so if it's up you turn it on if it's over you turn it off and then you just continue to loop like that so let's turn this and see what happens also notice on the speed let's go ahead and turn the speed at 255 so we don't mess with this slow start business or trying to operate it slow speed so let's turn this thing on and see what happens downloading Oh denied what is wrong here why did this go wrong oh we said speed pen and speed pen what did I do wrong used an upper case did you guys catch that that was part of the problem the other part of the problem is everyone did not hold their breath so this time everyone hold their breath as we download Oh still doesn't like it ah I did the same thing twice ok did you yell at me both time now every last one of you hold your breath that's the problem boom look at this running full speed on the fan look at that giddy up giddy up we are running full speed on the fan but now the real question is what we want we wanted if we turn this for it to turn off like if this was turning over okay and when we turn over it does not stop what did we do wrong okay well that's good we'll figure out what's going on so it doesn't seem to be doing what we wanted it to it's running full speed or here so let's see are we getting a good value on tilt valve so to know that let's print tilt valves so serial dot print L in a tilt bow and that should show us whether this is behaving as we were expecting it to so let me download that okay it downloads and now I need to show you a window where you can see where you can see the output of the serial monitor and that looks absolutely crazy let me call that up and see 9600 no one ending did I start the serial oh look at that I put an extra zero in there that's crazy wrong baud rate 9600 was but it was supposed to be so now let's come over okay so this is interesting I do a analog right oh oh oh did you see this this should be a digital read digital read I don't know why I put analog read digital read because I want to know is it honor is it off and so I will put this again and now we're reading zero and we're running full speed okay so let's go back over to this view so we got a zero at full speed and now I'm going to turn it tilt it and look at that it stops because it turned over back up right it starts again it turns over again it stops boom did you see that this thing works we have incorporated a tilt switch into our project let's come over here and watch it on this one where I think that you can see both of them if I move it up see see off of that digital read we're reading zero when it's upright on the tilt switch and then when I turn it I'm running into those wires when I turn it okay you see it gets a 1 it gets a 1 when it's turned and then it turns off and then it comes back to 0 I need that in there a little bit better ok stops turns on stops turns on boom that is working like a charm ok guys were you able to do that assignment on your own is anyone out there doing their homework is anyone out there doing their homework ok let me know alright this has been a fun little project let me go ahead and give you your homework for next week your homework for next week is going to be to use this motor controller again we're gonna use the motor controller in this basic setup we're going to take the tilt switch out because we're not going to do that anymore and what I want you to do is get out your most excellent joystick I want you to get out your joystick okay and remember we have had several lessons on this motor so far have we not had we not had several lessons on this motor we have had Lesson 37 which was controlling a DC motor and then this has been less than 38 how to do the tilt switch well in Lesson number 39 what I'm going to ask you to do before lesson 39 before you go to it I want you to incorporate the joystick okay if you press the joystick forward if the joystick is just sitting there I want the motor to be still okay I want the motor to be still if it is just sitting there all right but as you move it forward I want it to go faster and faster in the positive direction when it's like that it stops so this is like fast-forward slow forward stop fast slow to the neutral but then if you pull it backwards if you pull it backwards what I want you to do is run the motor backwards according to speed so this is going to be faster and then this is going to be slower faster faster slower reverse but I want the speed to be in the vert reverse direction too so I can go slow reverse fast reversed fat slow forward fast forward in smooth over the full range key as I said full range I just don't want it off forward off backwards I want speed control in the forward and the backward direction I will give you a hint you will have to use an equation you will have to do your math okay you will have to do your math don't come in and try to do this without doing your math all right okay guys give me a comment down below let me know that you're watching these give me a thumbs up if you liked the video think about subscribing if you subscribe make sure you ring that Bell so that you'll get notifications on this channel and then if you like this share with other people this has been Palma quarter from top tech boy comma I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 31,444
Rating: undefined out of 5
Keywords: L293D, Motor Controller, Arduino, Tilt Switch
Id: tXs3Bzf7s9g
Channel Id: undefined
Length: 17min 28sec (1048 seconds)
Published: Tue Nov 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.