Raspberry Pi LESSON 8: Using a Button as a Toggle Switch for LED Control

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with top tech boy.com and we're here today with episode number eight in our incredible new tutorial series where you're teaching your raspberry pi boss what i'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee at all as always i want to give a shout out to our friends over at sun founder sun founder is actually the sponsor of this series of video lessons and in these lessons we are using the most excellent the most excellent sun founder ultimate raspberry pi starter kit and guys seriously your life and my life are going to be a whole lot easier if we are working on identical hardware great kit i'm having a lot of fun with it you guys that already have it hopefully you're having fun with it if you don't have your gear yet look down in the description there is a link over to amazon where you can pick one of these up but enough of the shameless advertising let's talk about what i am going to teach you today and what i'm going to teach you is i'm going to show you my solution to the homework assignment which i gave you in lesson number seven and what that homework assignment was was to create a toggle switch based on a button where you could toggle an led on or off and i think i can come over here and show you what the assignment was i have mine already working and then shortly i will show you how i actually did it but you can see i have the switch if i have the button if i press the button the led comes on and if i press the button again the led goes off now this is not the button itself is just an on off button you're taking that on off button and you're turning it into a saw a toggle switch so how many of you guys were successful if you were successful leave a comment down below i am uber legend and if you are not successful leave a comment i fold it up like a cheap walmart lawn chair now like i told you in the homework assignment this is really very straightforward but it's something that until you get your mind around it it is just seems like it is impossible it is just a terribly terribly hard thing so like you can either do it the easy way by thinking or you can do it the impossible way okay so as always what i'm trying to do in this series of lessons is i'm trying to get you to start thinking like an engineer okay and how do engineers think they get it working on paper first they they kind of they kind of work it out on paper and then after working it out on paper then the coding is really easy but the mistake people make is they just jump in they jump in and they start coding and trying to make it work in python before they really have it scoped out have a strategy scoped out on paper does that make sense well i hope some of you guys were able to get this figured out but i'm going to jump in and what we're going to do is we're just going to kind of talk through this thing and we're going to talk through this thing with our sketch pad here now just thinking about it the first thing is i'm going to think in terms of i have an led okay i've got an led like this and then i have a push button switch over here and this switch is either not pressed or it is pressed so the switch is either open or closed and then the led the led is either on or the led is off okay so the led is either on or off and the button is either pushed or it's up okay if it's up it's in a one if it's pushed it's in a zero because right we're using a pull up resistor you should understand that now okay it sounds like now what is it that's going to make what is it that's going to make the led switch okay what is going to make the led switch and i tried to in the homework i tried to kind of emphasize this now watch this okay if i hold the button down and then i let the button up it switches if i hold the button down and i let the button up it switches so if the button is down that's a zero and if the button is up it's a one so when do i want to switch the led when the value on the led goes from zero or from the when the value on the switch when the value on the button goes from a zero which is pushed a zero which is pushed to a one so when the button goes from a zero to a one i wanna flip the led well if i'm going to flip the led i need to keep track of what i need to keep track of the led state okay the led state the led state is a variable and that variable is either going to be a one or a zero okay it's a one if it's on and it's a zero if it's off okay so if i need to know is the led on i just say print led state if it's a one the led is on if it's a zero the led is off and so do you see that that is a pretty simple concept you've got to keep track of is the led on or off and you keep track of that in led state simple enough right now also you're also going to need what you are going to need a button state you're going to need a button state so this is one variable which is led state now this is a second variable button state now what is button button state is a zero if it's down if it's been pushed and it is a one if it is up okay two variables led state button state now when do i want to switch led state when button state goes from 0 to 1. so i need to not only know where button state is right now i need to know where button state was the last time through the loop so what i need is i need another variable which is button state old okay that is the last time through the loop what was the button state well it could be a zero or it could be a one if it was down or up last time so when do i want to switch i want to switch if i want to switch if what if button state is a what if button state is a one and if button old okay no let's see if if button state is a one which means it's up and if the last time if button old was a zero so when do i switch i switch if button state presently is a one and last time button state was a zero so let's go back and let's look at this and maybe this is easier to you guys than it is for some people but i'm just being really deliberate in explaining this right now it's up so button state is one i press it down button state is zero nothing happens button state is zero when it comes up now button state goes from zero to 1 and you switch the led the led state flips okay so in order to make this thing work it's really really really simple what do you need it's really really really simple what do you need you need an led state that you keep track of you need the present button state that you keep track of and you need the old button state that you keep track of simple as that okay let's see if we can code this thing up so i guess i should jump in and just remind you of what our circuit is this is the circuit that we are using we are using the push button switch and the left leg of the push button switch is hooked to pin 40 which is the lower right gpio pin and then the right leg of the button switch is connected to ground which is pin 39 on on the raspberry pi and then the led the long leg we are going to gpio pin 38 then we're going to our through our 330 ohm resistor and then ground goes back to pin let me just look to make sure i don't tell you wrong that ground we're using pin 34 there we're using uh we're using raspberry pi pen 34. okay so now let's come over here and let's see if i can give you a code view that is not very good there i think this is the one that you want here and then i'm going to need to take a second here all right so that looks good so we are going to clear this start up at the top all right so now we are going to start coding so what i'm going to do is i'm going to create a new program down in my python directory and it's going to be called toggle led dot py because we're going to toggle that led on and off all right so now let's get busy we're going to need to have a delay in there so from time we are going to import sleep like that okay then we're going to use the gpio pin so we're going to import rp little i dot gpio and we're going to import those as gpio now i'm going to need a delay in there so i'm going to set my delay to let's say 0.1 that's usually a pretty good one we have our n pin which was the lower right gpio pin that's our input pin that we have hooked to the switch that was pin 40. we have an out pin which was the one right above it which was gpio pin 38. now we need to do a gpio.set mode and what are we going to set we're going to set it to gpio.voard [Music] saying we're using the physical numbering system on the gpio pins okay now we've got to be a little careful here we're going to do a gpio dot set up and what are we going to set up we are going to set up our out pin and we're going to set it up as a gpio dot out that's pretty straightforward now this is your money line of code here this sets up the internal pull-up resistor so that's going to be on the end pin and it's going to be a gpio dot in dot in and now i've got to tell it pull [Music] up down it's the we're going to use the pull up down pull up down okay and that's going to be equal to g p i o dot pull up down pud and then what is it going to be we're going to use a pull up resistor and that way we can just use the switch the toggle switch or the uh push button switch by itself we're going to use the push push button switch by itself without the external pull-up resistor because we're doing it internally this time okay hopefully that all makes sense now okay so that looks good now we've got to set up those variables remember we said we were going to keep track of the led state well the led is going to start it's going to be off so we're going to set that to a 0 0 is going to be off so the led state starts out as 0 off and then the button the button state it is up and in the up position in the up position with a pull up resistor that is what that is a one value and then we also have to give it a button state old well since we haven't done it it says we haven't pressed it when it runs it's going to start out as a 1 and last time it would have been a 1 as well so we're just going to start them out both as one to initialize those variables now we're going to do our try and that will allow us to exit cleanly remember we're going to do a try and accept i'm going to spell accept right this time how do we accept we accept on key board [Music] interrupt if i can spell and that colon does not go there okay [Music] it'll be like that and then what do i put here what do i put here i'm going to put gpio dot clean up like that and then i'm going to say gpio i'm going to say print gpio good to go and that just tells me that they were in fact cleaned up because sometimes if your program crashes you haven't actually you haven't actually cleaned them up okay so if you hit the control c it's going to jump down here to the accept but if you haven't you're going to be here and you're going to have a while loop so i'm going to say while true when is true true true is always true so this is going to be an infinite loop what do i need to do i need to read the what button state i need to say where is the button so the button state is going to be a very simple gpio dot input and i'm going to read from the in pin so now i will know has the button been pressed or has the button not been pressed and so as i'm just sitting here it's going to be reading a one a one a one a one a one a one a one because i haven't pressed it okay and then we can even just for fun we can say print button state just so we can see what it is print spelling is very important in python [Music] like that okay so i'm going to print it out now when would we want to switch we would want to switch if button state equal equal 1 which means it's presently up and button state old button state old equal equal what zero so i'm saying if it's presently up and last time through it was down if it's presently up and last time through it was down then what do i want to do i want to i want to switch the led so how would i do that well i would say my led state my led state lowercase s led state it is it started out as a one it started out as a zero the led is off now i want the led to be on so i want to switch it so led state is equal to not led state okay led state is now not led state which means if it was a one it's going to become a zero if it was a zero it's going to become a one now you might cry foul and say well you haven't taught us not yet yeah i haven't but what you could do here is you could put an if statement if l inside of the if button state you could now put in a an if statement if led state is equal to one then led state equals zero or if led state equals zero then led state equal once you could put two if statements and you could do the same thing so you had the resources to make this work with what i've taught you so far okay you have the resources to make this work based on what i've taught you so far let's make sure you can see what i'm doing here okay yeah now you can see it so led state is equal to not led state or you could put the two if statements in there all right then what do you need to be sure and do now that i have the led state i need the gpio dot output and where do i output it i'd output it to the out pin and then what do i output to the out pin out a led state it'll output a zero or a one turn it on or off does that make sense now this is your money line of code here what do you have to do you have to pass forward what your present button state is so i've got to set it up for the next time through the button old value button old state is equal to what button state do you see what this is this is like dropping a breadcrumb because when i get to the top of the loop i'm going to need to know what button state was last time well what was button state last time it was button state old and so i've gotta set button state old to button state as i'm getting ready to go through again if that doesn't make sense pause the video and think about it because that's kind of a key concept to make this whole thing work buttons button i think i did that wrong button state old i did it wrong sorry it doesn't count as a mistake if you catch it before you run it button state bold like that button state old is equal to button state could it really be that easy how many mistakes are you guys yelling at me about here how many mistakes have you already caught me making and how many mistakes are you yelling at me about all right let's do a control x let's do a y and then a enter okay and now we're going to come up and instead of nano we are going to pi we are going to python three it i need everyone to hold their breath oh dee died line two how could i i'm gonna make a mistake in line two let's look at that [Music] import uh pr that's my old dyslexia keep kicking in were you guys yelling at me about that control x why enter okay hold breath the reason it didn't work is one of you guys out there and i think you know who you are did not hold your breath and that made my code mess up i need everyone to hold their breath oh line eight okay what is that pull up down gpio pull up down up gpio dot in that looks good input oh wet up oops that's a bad one nano okay what was that where on earth was that you guys were probably yelling at me over that one control x yes enter really everyone hold your breath okay what did i forget right off the bat i see i forgot my delays so i'm going to go back and i'm going to go in and i'm going to nano it and hopefully you guys saw so where would be a good time to do the delay down at the bottom of the loop i think down at the bottom of the loop after i've done this i can just say i can do it do a sleep and i want to sleep by delay and i want to sleep by delay why because that was what i set up at the top control x y enter and now we're going to python three it hold your breath okay now is that what we expect well the button is not pressed the button is not pressed the led is off and it's reading a one that looks good okay all eyes on the led i press the button i hold it it's reading a what a zero moment of truth shazam who's your magic man uh-huh me look at that toggle on uh-huh let's see if we can toggle on we'll hold it down and let it up toggle off look at that works pretty good so we turn our lot on we go about our business we are ready to leave we come in and we toggle it off that is pretty darn good okay now i want you to think about you could have done this a different way i switched on where like if i hold the button down it doesn't come on until i what until the button up you could also do this switch on what switch on button down you could also do it that way so let's see if we could do that so how would we do that how would we do on the how would we do the switcheroo on the button down instead of the button up well you would do that it all comes down to this that would mean if i am presently if i am presently down which is a zero if i am presently down which is a zero and i was formally formerly up which was a 1. so if i go from a 1 to a 0 i want to switch in that condition so i'm going to control x to say y and then enter and then let's up up to python 3 it and then we're going to hit and enter okay it is off now watch this i come in i press down and it goes on okay now watch this i press down and it goes off and then i let up okay see you can do it on whichever transition you want who is the master of the toggles which you are you know how to do that now and guys this is really important because lots of things like imagine you wanted to turn a motor on with the toggle switch you would want to come in press the switch have the motor come on you would want to come in later and press the switch and have the motor go off and so turning things on and off with these very simple switches you've got to understand this toggle concept and so hopefully hopefully that makes sense for you guys uh let's see i think i need to switch back over here let's see if i can get it all right so we'll switch back over here to the river view if you look above my head i know you guys like to see the fisherman there's one right over there you can see we have a guy working on bananas we have a guy working on bananas right there it looks like there he is and so i know you guys like to look for your fisherman friend in the background as we're doing these lessons but man i hope you guys are having as much fun taking these lessons as i am making the lessons now what's your homework assignment is uh for this lesson is if you are not able to do this on your own at least go back and do it now without watching me go back and code the whole program up without looking at what i did so that you can say okay it's in your head you understand how to do it and at least now you can do it at least now you can do it by yourself you guys as always i want to give a shout out to all of you who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming if you're not helping out yet look down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up also as always if you like this video be sure to give us a thumbs up leave a comment down below both of those things help us with the old youtube juice which allows youtube to show this video to more people also need you to share this with your friends because the world needs more people doing engineering and coding and fewer people sitting around watching silly cat videos paul mcquarter with toptechboy.com i will talk to you guys later [Music]
Info
Channel: Paul McWhorter
Views: 16,273
Rating: undefined out of 5
Keywords:
Id: yL5BNA_Ex6s
Channel Id: undefined
Length: 26min 27sec (1587 seconds)
Published: Thu Apr 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.