Raspberry Pi Pico W LESSON 74: Create a MicroPython Class for Controlling RGB LED

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy dcom and we're here today with episode number 34 in our incredible new tutorial Series where you are unleashing the power of your Rasberry Pi Pico W what I will 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 as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the Kepler kit for Raspberry Pi Pico W now most of you guys probably already have your gear but if you don't look down in the description there is a link over to Amazon and you can hop on over there and pick your kid up and believe me your life and my life are going to be a whole lot simpler if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and what I'm going to do is I'm going to show you my solution to the homework assignment that I gave you in lesson number 73 but I must first ask were you successful if you were successful leave a comment down below I Am Legend double chest bump and if you were not successful leave a comment down below I folded up like a cheap Walmart lawn chair okay so what was the homework assignment in lesson number 73 let me get out of your way have another little sip of my goj juu and then come over and what your assignment was was that you were to create a micropython library that allows you to interact with the LED and you would include the colors in a dictionary in the micropython library and then you would do have a function in there or it's you know in this case we're going to have a class and a method inside that library that allows you to interact with the LED so that really all your heavy lifting is done in the library okay all your heavy lifting is done in the library and then in the uh then in the main program it's really simple you just create an LED object and then you interact with that led object and that led object that led object via the library has all the custo has all the uh has all the color values inside of a dictionary hopefully that makes sense but let's jump in and let's see if we can get started first of all as we had last week we have the RGB LED this is the ground leg the long leg is one over from the left so you have that long leg oriented towards your left then the very left is going to be R and then the ground and then the G and the B the r is connected through a 330 Ohm resistor to GPI pin 14 then the G comes down here to the ground rail or the ground comes down to the ground Rail and then the G goes through a 3 30 Ohm resistor over to pin 13 GPI pin 13 and then the blue goes through a 330 Ohm resistor over to GPO pin 12 so RGB are pins 14 13 and 12 and then that long leg is connected to the ground rail okay pretty easy to set up and so we're going to jump over here and get ready to code so we're going to start by creating our library so we're going to come over here and we're going to open that library that we had before and that was in lib and then that was RGB okay and when we started it it was simply we just put last week we just put the dictionary we just put the dictionary in the library but now we're going to create a what we're going to create an LED library that does the LED functions in it so in that case we're going to have to do what we're going to have to create an LED class okay and that class then will become the core of our library now we we've been talking about object-oriented programming and really what our goal here was was to practice the things that we learned in the last few lessons how to use dictionaries how to do object-oriented programming using classes and then to put that class in a library that we save so this is bringing together and practicing all the things that we've learned in the last three or four lessons now I'm going to keep this RGB business because I don't want to type it in again but what am I going to need I'm going to need a class to serve as the sort of core of the library and I'm just going to call it my RGB like that L that is the class and then for a class we always need a what we are going to need an anit function so it's underscore uncore an AIT method I should say because a function in a class is called a method it is the init okay and then what is the init going to be well we're going to need self which will be the name when we create an RGB LED object and then what does this a knit really needs to know it needs to know where you have hooked up I can come over here I think where you have hooked up your RGB L and so what we are going to need is we're going to need to know where the red pin is where the green pin is and where the blue pin is like that okay now we're going to come in here and we're going to set everything up well first of all what we're going to have is we are going to include this RGB LED this RGB dictionary but now that's going to need to be a self RGB okay because it's going to need to be indexed by the object name so it might be my cool LED and then self would be my cool LED it'll be assigned that but you type it just like I've done it here so self RGB LED that looks good and then I have now got the dictionary I've got the dictionary in there and also since I'm going to set up the LED inside the class which will be our library I need to from machine import pin and pwm so lot of the stuff that we did last time inside the main program we're now going to be doing inside the library and so we will set it up like that and now I'm just going to run it to make sure I'm going to run it to make sure it's not going to do anything but at least it shouldn't crash okay didn't crash that looks good so I've imported the methods that I need and I've set up the library okay now I wonder if I could even here just do print no I can't because I'm not calling it yet so this isn't going to do anything until I initiate it so now let's go ahead and let's set up let's set up those pins so I would normally do like red LED is equal to pwm of what of pin of what R which will be passed to it like that but if I did this then this red LED would not be known down in the methods so I need to do a self. red LED and therefore red LED will be known in the methods that I find that I set down below and if this is completely baffling you go back and watch those class uh you know the videos that I did on micropython classes okay now I've done the red I need to do self. green LED is equal to P WM of what pin of what G the G Pin that I will pass it self. blue LED is equal to pwm and that is going to be pin of B for the blue pen okay and then here you can see that I didn't I'm gonna just see if I get an error because sometimes it uh yeah you see it doesn't find these errors because I'm not actually initiating the function so we got to be kind of careful here we've got to be kind of careful because it's going to be hard for us to debug as we go along but I've got red LED green LED blue LED and those are pwm of pin R pin G Pin B so I've just taken that from our main program last time I put it inside of the library inside of the class in the library and what I've done is I've got this thing uh got this thing going and then again we've got to do self so that it will be known down in the method that we use okay so I've set up the pwm now I need to go ahead and set some values so now self. red led. frequency like that we set it at a th000 okay self. green LED do uh Duty underscore unsigned 16 like that and we're going to set it at zero all right when we set it at zero when we set it at zero that will turn it off that's a duty cycle of zero so I've got uh I've got red green and blue LED setup and this should be red okay so I set the frequency to zero I've set the frequency to a th000 and I set the duty cycle to zero now what I'm going to do is I'm going to copy this and I'm going to do the other two LEDs and so I will paste I will paste and now get my indention all right here okay so red green blue I have created the objects and then uh red LED red LED then this will be green LED and this will be green LED and then this will be blue LED and this will be blue blue LED so what does this do if I create an RGB LED object maybe I would call the name my LED and that would be equal to the class RGB LED what would I pass it I would pass it the red pen the green pen and the blue pen then it is going to set up the dictionary for that led and then it is going to initiate those LEDs as pwm signal and it's going to set their frequency and their duty cycle so I'm just setting up all of those now why are these indented differently because really this is all one line from here to here it's one line so this indentation should not matter so I've set all of those up and now they're set up in the init and so now what do I need to do I need to Define that method which before was a function I need to Define that method which will allow me to apply a color when this is called so this is going to be make color and then what is it going to need it's going to need to know who it is self and then I'm just going to put MC for my color all right now what is this going to need to do it's going to need to get a red vow a green vow and a blue vow and that's going to come from what self . RGB of what MC my color all right so what is going to happen when I create the LED object it's going to set all of this up and it now knows what the dictionary is and that dictionary is going to get passed down here because I defined it as a self. RGB self. RGB here which means down here all the methods that are inside this class will know what RGB is the dictionary but I just have to address it as self. RGB again this is review of stuff we did in that object-oriented programming lesson that I did on classes okay so now I have red green and blue that's all good and now what I need to do is I need to apply the desired color MC onto the different channels so what are those going to be self. red led. Duty underscore unsigned 16 okay and then you can go back last week we worked through all this it's an INT of 65 5 35 ided 255 times what times the red Val okay why did I not have to do self red V because it's not used anywhere else red V is just used inside of this method so red valow is red Val when you're inside this method how does it know what red V is well it gets it from self RGB which is up here defined from that dictionary okay so now I've got that and now I've just got to do the other ones and I need to close the uh Duty method and then I'll do the same thing for green l. Duty underscore unsigned 16 and then it is going to be an INT of 65 535 35 ided 255 * what green Val again no self because this isn't used anywhere but right here here okay then self. Blu led. dycor unsigned 16 int 65535 divided 255 times blue Val like that close the int close the unsign one of the most confusing things is in classes is where you have to decide do you use a self. this or self. that or do you just use the variable self. led. Duty it has to be self because it is set up up here and so this has to know what was set up up here so to share the value between the init and the make color we have to use self in front of it and self will be it will turn out to be whatever you name the object when you create create an RGB LED object okay so now I've got those done I've got those three things done and really that's all we need all make color does all that meth method does is it takes your desired color what you got from the dictionary and it uh sets your three LED channels to that okay now I think that is really all we need to do there so I'm going to say save okay and now I'm going to come over here and I'm going to start writing my program okay I'm going to start writing my program well what am I going to have to do I'm going to have to say from my library RGB import everything okay now let me just see here can I print now now I need to create let's see let's go ahead and and Define our pin so red pen we said was pin 14 and then green pen was pen 13 and then blue pin was pin 12 like that okay now I'm going to create my LED objects I'm just going to call it my LED you can call yours whatever you want what do we do we we go to RG b l e d and we pass it what red pin green pin and blue pin like that what is RGB Led rgbl Led is the class okay so I am creating an object my LED of the class RGB LED how does it know what r GB LED is I imported it from RGB okay so it's like that now this I think should run and do absolutely nothing but hopefully we won't get an error okay when I run this look at this it creates the my LED object but it doesn't get a error well let's just see if I can look at something so I'm going to say print and what am I going to print I'm going to print my L that is the object do what RGB which is the uh which is the library now let's just see if that will work look at that boom okay so that is indicating that at least we don't have any gross errors in the uh we don't have any gross errors in our library because we've been able to down here in the main program get that dictionary okay we've got the RGB dictionary and we were able to print it out down here okay so that is really really good okay so now look how simple this program becomes I'm going to do a try okay so I can exit cleanly and then I'm going to say while true and this will be our main Loop but I want to come down so I don't forget and I'm going to do the accept down here before I really finish writing all the while stuff and what I'm what am I going to do on accept well I want to accept when there's a what keyboard interupt like that and then what do I want to do okay I'm going to Define this the red led. dutt underscore 16 and that I'm going to set to zero so I'm going to turn that off and then the green led. Duty underscore 16 is going to be zero and then like that and then the blue led. Duty underscore 16 is going to be zero okay now let's look at this red LED green LED blue LED is over here on here okay now let's run this let's just put a pass here because we might have to think about that a little bit let's see what happens let me stop it okay let me run it and it looks good what happens if I do a control C okay red LED duty is not defined so what do I need to do I need to tell it that I'm working with my LED dot my LED Dot and then my LED dot like that okay now let's run it and then we will do a contrl c and it's still not understanding that so let's look over here you see I've got to get down to this particular function okay and so probably what I'm going to need to do here is I'm going to need to do a Define and then I'm going to say clear LED it's going to need to know self and then that is all it's going to need and then what I'm going to do is I'm going to take these three Duty Cycles and I am going to put them here like that and I'm going to say clear L like that and then I'm going to set all of these Duty Cycles for each one of the LED to zero okay so now I will save that that's the library that I'm building and then here what I'm just going to do is I'm going to call the method clear but it would be my LED do clear LED and then I really don't have to pass it anything because I'm just doing that and then what I'm going to do is say print program has ter termin terminated like that close the string close that now let's see what happens okay so it is running without an error and then if I put control C program has terminated okay now why did it not work the first time that I was doing it well I said red LED it knows what it is butt Duty I can't get to duty directly I have to go through what the self. red LED so you have to be working within the parameters of the class okay but now it is working so we got to kind of think through these things carefully as we're going all right now what do we do while true now this is going to be the main program let's get the color from the user my color is equal to input what color do you desire like that and then space close the string close the input and then my color equal my color. lower so we're going to change it to lowercase like that and then if my color is not in is not in RGB but how do I get to that RGB dictionary it has to be my LED e. RGB okay so if it's not in that I'm going to print my color comma you know you can use the single quote space is not a valid color please try again like that close the string close the print okay but now if my color is in my led. RGB we have a valid color on our hands and then what do we do we want to call make color of what my color now what did I do wrong here I don't know what make color is because make color is a function in the class okay rgbl L the object name is what I've got to talk to the object that I created which is my LED okay that was created here that is of a class RGB LED over here and it is the specific object of that class my LED okay now let's run this and let's see what happens what color do you desire all eyes on the LED red boom success double chest bump okay then we're going to go green blue fushia FIA is not a color okay you know what I should should probably do is if there if it's not a valid color what I should probably do is I could should say my led. clear LED so I just turn it off because it was not a valid color so let's try that let's run it okay what color do you desire red green blue and then fushia and then fushia turns it off because it's not a valid color but let's go on cyan magenta yellow yellow that worked or orange and then uh White and then off and then let's turn it back to Red boom okay that all works and now what are we going to do we can turn it off and that work let's turn it back to Red okay now the real question is what happens if I do a contrl c i do a control C to cleanly exit and it turns the LED off okay so guys I hope this makes sense in order to really make a library what do you need to do you need to create a class that class in the anit when you create an object of class RGB LED I create an object of class RGB B LED how does it know what RGB LED is cuz I imported everything from RGB and then when I create that object it does all of this in the initialization and what is all of this I Define the dictionary other people are going to want to use the dictionary so I've got to give it the self what is self self is going to be assigned my l so my led. RGB is going to be this Di AR and then my led. red LED I set up I set up the pwm on those pins so that's all done it's only done one time when I create the object but then these things that I'm creating in here like blue LED because I put self in front of it it can be accessed from my color and it can be accessed I mean from make color and it can be accessed from Clear D and the way we access it from the outside is we access it by giving it the object name the object name becomes self and then so self my led. clear LED will come down here clear LED and self is what my LED okay guys now why did I give you this homework assignment I'm not trying to be tedious but man this class thing in this Library thing it really you need to do it a few times before you're comfortable with it so I hope that some of you guys were able to do the homework assignment and if you weren't at least you've seen me do it and you've seen me do it now three or four times so I'm hoping at this point you're getting comfortable with classes I really need feedback from you guys are you starting to understand objectoriented programming are you starting to understand classes and function inside of classes are called Methods so we're learning classes and methods and we're going that next step to take the class and make it a library and so this is getting into some really more advanced programming and it's something that a lot of people struggle with but let me know Down Below in the comments are you starting to get comfortable with it so we've learned functions we've learned dictionaries we've learned classes and we've learned methods in the last few uh lessons in the last five or six lessons what are we going to do now we're going to go now and start using more complicated hardware and that more complicated Hardware is going to need us to use classes and it's going to need us to understand uh threading and it's going to need us sometime to use both cores and so we're going to need as we move forward to the more complicated Hardware we're going to need to know how to use these things and so guys if you're not clear on it I'm not giving you a homework assignment this time but if if you're not clear on it you need to go back and review those last few lessons because future lessons are going to require using both cores they're going to require classes and methods okay and so you need to understand this stuff so your homework assignment is to just get familiar with those to become comfortable with it and if you were not able to do this homework on your own at least watch the video Turn the video off and go see if you can do it on your own now without you know following me step by step by step so if you weren't successful before on the homework see if you can do it now without copying me line by line because you're going to need to do it so next week we're going to get some new hardware that new hardware will require us to use some of the more uh Advanced programming techniques that we have just learned okay guys I hope that you are having as much fun taking these lessons as I am making them I hope that you're not getting finding these things too tedious but you've really finally got to get to the point that you feel comfortable with classes as always I want to give a shout out to you guys who are standing with me on patreon you are the guys that keep these classes coming appreciate it you can also help me by giving me a thumbs up on the video and leaving me a comment down below that always adds to my my old YouTube juice and when you do that this video will be offered to more people and then if you haven't already subscribed to the channel when you do make sure you ring that Bell so you'll get notification when future lessons drop and as always share this video with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy tocom I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 1,949
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: tw-mXURNEUc
Channel Id: undefined
Length: 33min 30sec (2010 seconds)
Published: Mon Jun 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.