Arduino Tutorial 36: Understanding How to Use Tilt Switches in Your Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 36 in our legendary most exceptional new and improved Arduino tutorial series so I will need you to pour yourself a nice strong cup of iced coffee that straight strong black coffee poured over ice nothing else is needed and it is quite refreshing what I also need you to do is get out your a Lego super starter kit what you don't have one check out the description down below click on the link you can get this for 35 bucks on Amazon you get a boat you get the Arduino and a boatload of components and seriously it is kind of helpful if you're using exactly the same hardware that I am so as you're learning you're on everything exactly the same as I have it much more likely that things will work for a new person who is learning what we are going to learn today is we are going to learn to use a tilt switch a tilt switch is a switch that switches from on to off as it tilts and that allows you to see something like let's say that something is turned upside down or turned over so let's say if you had a robot if the robot flipped over you need to know that the robot needs to know that it's on its side and that way you could cut the motors off because let's say that if it turned over on its side and all of a sudden started getting wound around something something could be damaged or imagined on your car that if your car flipped upside down might be nice to shut off that gas tank just saying you know just saying it might be nice so there's a lot of things in real engineering projects where you need to know roughly and simply what the orientation is and so for today's most exceptional project you will need you will need first of all for me to get out of your way and then you will need your Arduino Uno you will need your breadboard if you look in that a Lego kit you will see a component that looks like this this is the tilt switch you will need a red LED and yes a green LED as you know we normally only use red and yellow LEDs green LEDs are saved strictly for special occasions today is my birthday yes today is my birthday so in honor of my birthday you will be allowed today only to use the green LED you will also need two current limiting resistors okay let me move all of this nonsense out of the way and let's just talk very simply about how the tilt sensor works so here you can see the tilt sensor again okay I can get my hand out of the way it's kind of like a can with two leads in it so let's look at what that actually is doing you've got two leads like this and then you've got a can like this and then very simply there is a little metal ball in that can so if I am in this orientation the switch is what the switch is closed it is on okay if I tilt it over to the side a little bit like this what is going to happen that ball is going to roll this way and then the switch will be off so we go on from this orientation and off in that orientation it's pretty simple right so some of the good things about this is these sensors are really really cheap also they're very very easy to implement that if you go back and look at our earlier lesson on I believe it was lesson 34 where we were talking about how to do a simple on/off switch in your project you would use the same approach but instead of having a push button this one's on or off state depends on very simply whether the device is tilted or not so they're cheap and they're very easy to implement if I were to say what would be the downside of them you can imagine that on this if you start Bob even if it's in this orientation if you start getting vibration that this can start moving up and down and opening and closing so you've got to keep that in mind if you're going to do something like put this in the car you've got to think a little bit about the effects of vibration but that is beyond the scope of what we are going to talk about today because today we are going to see if we can get this thing hooked up and oh my goodness did I lose my switch already okay getting all my little components back over here and I have found the switch quite a relief okay so to put this together you just plug the switch into your breadboard and just make sure that the two legs of the switch are in different columns because you don't want the two legs of the switch to be connected together and then one leg of the switch I'm going to hook to GND over here on the Arduino so it is connected to GND I always like to use a black wire for GND when I can but as you know I've just got kind of a pile of wires here so I can't always use the right color and then to power I like to use a red when I can so that other leg that other leg of your tilt switch will be connected to pin 2 and I have no idea what that little noise was that we just heard all right so now we have a switch now if you remember lesson 34 we showed you in less than 30 for how to incorporate a switch by pulling a little trick where we can put an internal pull-up resistor using a series of commands we're going to use to that little trick because that way we don't have to add another resistor and so let's just go ahead and switch over to our code view and let's start coding this thing okay so first up I have what I have a pin what do I have I have and we'll make it an integer variable I have a tilt in and that was pin what that was pin to all right then I'm also going to read from this so I'm going to but tilt Val I'll call it you can call these anything you want but make him mean something so if someone looks at your code it sort of makes sense so the tilt Val I'm not going to put a value in because I will read that later now we will start doing a little trickery here we have to do a pen mode on pin two which is what tilt in okay and it's a what input because we're going to read from it now this is the trick that we showed you in 24 that we can pull in a pull-up resistor and power it if we do a digital right of tilt pin and we want to make it high okay if this does not make sense to you go back and look at those earlier let's lessons on buttons and pull-up resistors and all of that but I will move on on this one we always like to turn our serial monitor serial dot begin the trusty 9600 baud rate okay now the codes going to be pretty simple down here what do we want to do well tilt tilt Val is equal to is equal to digital read of what tilt pin okay and then what do I want to do I want a serial dot print out in tilt pin because I want to see what that value is right so what do we expect in the up position the switch is closed and for this configuration from lesson number 34 a closed switch is what it is a zero and then when we tilt it it will be an open switch and then we will expect a 1 ok well let's come over here let's call up our serial monitor let's see where is my serial monitor view that is a pretty good one and let's hold our breath as we download this code not happy not happy why is it not happy what is all this nonsense okay let's look here okay there all right come back over here boom okay it downloaded and now I'm reading too well wait a minute that switch right it's got to be either 0 or 1 0 or 1 how could I possibly be reading a 2 okay tell me debug the code I'll give you a hint I deliberately put an error in there to see if you were paying attention pause it and leave a comment down below telling me that you caught my error as I made it okay and why do I put this error in because I teach and I watch students doing coding and I see certain mistakes that they make okay what did we do yes this was our mistake we did the digital read of tilt pin into tilt vowel okay but then what did we print we printed not tilt Val we printed tilt pin and tilt pin as always - that's the pin we don't want to print the pin we want to print the value coming from that so what should we have done we should have said tilt Val right now let's try downloading that boom okay now let's look okay we're getting a zero which is what we expect for a closed right that's what we expect for a closed switch and now let me get further out of your way and now let me begin to tilt this okay you see as I'm tilting it I'm about horizontal but the ball is still downhill and then as I go this way giddyup did you see that giddy up let's watch this we get horizontal we tilt a little bit more and it goes one saying that this thing has been tilted this thing is rolling over boom okay now this is your homework I want you to give a visual display because if this was out being used somewhere you don't have the serial monitor you don't have someone sitting there looking at it so what I want you to do is build a visual indicator of tilt and so if you are in the upright position I want the green LED to be on everything is good but if you're tilting like this I don't want the red LED to come on that's your homework pause the video go do it by yourself and then once you are done come back and we will finish it together okay boom alright we are back and let's go back up to the larger view and let's do this thing very quickly this is going to be easy we have our two LEDs so I will start with the red one okay I'm going to put the the left my left leg the left leg of the red LED into column 35 and then the other one one over and then I'm going to get a little wire here and I'm going to go from there - let's say pin I'm gonna get a little bit longer wire and I am going to go from the 35 over to pin seven okay so I've got pin seven connected to what is the left leg or the long leg of the LED and then I'm going to come down here and the short leg is going to go to the current-limiting resistor short leg is going to the current limbing limiting resistor and then notice the other leg of the current living wrist limiting resistor is jumping over this trench and so that will not be connected this lower leg will not be connected to the LED which is what we want then I will put in the green LED and the green LED will go in column 40 the long leg okay call them 40 into the long leg I will get another wire into the long leg of the green LED and I will put that in our dueƱo pin 6 this will also need a current limiting resistor so I will again hook the current limiting resistor to the short leg current limiting resistor to the short leg of the LED the green LED again jump across that trench alright see do you see do you see let's see okay it's trying to focus the short leg of the green LED is hooked to the current limiting resistor the current limiting resistor jumps across the trench and now tell me what the only thing is that I still need to do in the circuit I need to ground these two LEDs this short the bottom leg of the LED and you guys I've gone through so many of these you should be able to sort of follow along if you don't go back and look at my early lesson about how to hook up an LED and then I will come to a GND I do have 3G and ds3 grounds on the Arduino and what that means is is that I can do all of these I can do all of these with the grounds on the Arduino I don't have to hook up another rail make a ground rail so then I will come to the bottom leg of the green LEDs current-limiting resistor and I will also bring that over to G and D okay so I used all three of my GM d's but that's ok because that is all I'm going to need now we need to come back over here in code okay we're going to come back over here in code and we will go to a nice code view here and so what do I need to do this software I need some more variables right I have now a red pin for the red LED and the red LED was connected to pin seven okay and then I have a green LED again or green pin bring ten again in honor of my birthday today which is equal to pin six we said I need to get those all the way in pin 6 okay good ten six and now I'm not gonna be doing any reading there okay I will need to do my pin modes and so I need to add a few pin modes here I'm gonna do pin mode red red n is a what it's an output okay n mode green pen isn't output all right now those are set up I still am setting up that switch and now what do I need to do well I can still print that out if I need to for debugging but now I need an if statement alright if what did it read in the normal position do you remember what it read in the normal position the switch is closed and so it reads zero so if tilt now oh look at that getting happy little birthday wishes from everyone here wonderful people are wishing me a happy birthday so if tilt valve and I probably should put quotes if tilt now equal zero then what do I want to do I was going to put that mistake in there but I won't put you through that torture what do you have to do to equals if I did this it would sell set to tilt bail to zero and it would never work right what do you have to do in a conditional equal equal if tilt vowel equal equal zero then what do I want I want to digital right okay if it's up it should be green and so green pin and I want to set that to what hi now in this position what do I also have to make sure that I do did you tall right red and low because you got to remember if you go to the tilt condition red is going to come on when you come back to the untilled position you got to remember to go off go back and turn the red one off so you've got to clean up what might be going on there because you've got to remember this thing loops okay now what's the other case if tilt Val equal equal one what does that mean that means it's tilted and in that case what do I want to do well I want to kind of do the opposite so I'm going to come here and I'm going to copy these things control C and then I'm going to come here and I'm going to control V and then I'm gonna clean up my spacing and then in this case you want to turn the green off so I'll make it low you want to turn the red high so I get time does that make sense I think this should work so now let's download this mmm okay and look at that what happened down here can you see what happened the green LED came on and man in person that is a beautiful beautiful LED that is a beautiful LED but it kind of it kind of doesn't show real well on the camera it doesn't show real well on the camera because it's too bright and it doesn't pick it up so now let's slowly start tilting let's slowly start tilting and then let's see what happens as we tilt boom what happened you're getting the red warning light tilt tilt tilt tilt ok we're clean giddyup did you see that boom this thing works so now what you have is you have an ability to incorporate a tilt sensor into your project ok did you guys make it to the end of this did you guys make it to the end of this video host wonder if anybody makes it to the end of the video so I'm going to give you a little clue here how you can let me know that you've made it to the end of the video I will show you this is a picture of my granddaughter and she's on her way to see me this was taken on the other side of the world in an airport is she's so excited because she's getting on the airplane to come and see me so if you've made it all the way to the end of the video let me know by typing Gavriel Gabriella that's her name in the comments that'll let me know that there's at least someone who makes it to the end of these videos like I got these videos that are 15 minutes long you know what the average view duration on my videos is three minutes and these are 30 to 50 minute videos average person watch watches three minutes if you're watching let me know typing Gabriella give me a comment on were you able to do this project by yourself were you able to do the homework by yourself is anybody actually doing their homework are they just copying me what happens when I'm not here you got to know how to do this stuff on your own think about giving us a thumbs up think about subscribing make sure you ring the bell so you get notifications and then let's also share this with other people okay appreciate your time today palma quarter from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 40,857
Rating: 4.9867988 out of 5
Keywords: Tilt Switch, Arduino, STEM, Tutorial
Id: SYDuRyweeLA
Channel Id: undefined
Length: 20min 57sec (1257 seconds)
Published: Tue Nov 12 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.