Raspberry Pi Pico W LESSON 73: Control a RGB LED Using a Dictionary in MicroPython

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 73 in our incredible new tutorial Series where you are unleashing the power of your Raspberry 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 hopefully most of you guys 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 kit 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 what I'm going to do is I'm going to show you my homework solution to the homework that I gave you in lesson number 72 but I must first ask were you successful if you were successful in completing the homework 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 hopefully most of you guys were able to do it because it's really a straight forward extension of things that you've already learned the lesson in 72 was that how to use the dictionary data structure and then I gave you a practical homework assignment that you were to create a dictionary and use the data in the dictionary to control the color of an LED hopefully you guys are posting your homework to YouTube When you do the homework assignment make a short one or two minute video post it to YouTube in the description of your video link back to the lesson on my channel then in a comment on my video leave a link over to your homework solution so I can hop over there and take a look at it and I look at every single homework assignment that you guys post just a note please post it as a normal YouTube video Don't unload upload it as a short because if you upload it as a short is very hard for me to look at so just do a normal video also don't limit the comments if you say it's made for kids it won't let people leave comments on it and a lot of times people want to look at your homework they want to make comments so make it not made for kids which of course this is not made for little children it's made for you know us guys that are working uh working with the kit and then we can leave comments on it okay but enough of this introductory Banner let's get over here and let's look at my solution to the homework assignment I'll come over here to our view of the circuit it's really pretty straightforward I have an RGB LED hooked up to the Raspberry Pi Pico W the way I did it this this leg is the r leg and then we have next over the long WR leg which is the uh ground pin and then this is the G and this is the B so this is R ground GB ground of course comes down to a ground rail the r goes through a 330 Ohm resistor it goes through a 330 Ohm resistor to GPI pin 14 and then ground goes to ground G the green pin goes to gpio pin 13 and the blue blue pin the last one over here goes through a 330 Ohm resistor over to the uh over to the uh GPO pin 12 so RGB are GPO pins 14 13 and 12 okay pretty straightforward little circuit there also notice that I'm really trying to get us to do nice neat compact builds and you see I like using these Bojack straight jumpers and then I've started trimming the the long legs off of the resistors so they'll sit down on the board nice and compact so you guys as you are maturing and we're starting to build more complicated circuits moving forward you need to start trying to get neater and more compact builds okay so that is our circuit let's come over here and let's see if we can start coding the first thing I'm going to do is create the dictionary now the colors we wanted to do were red green blue cyan magenta yellow orange and then white and then off okay so let's see if I could create that dictionary RGB is going to be equal to the what the dictionary and then what's my first key is the string red and then red is going to be what it is going to be the list or the array 255 comma 0 comma 0 like that and then a comma and you can put these on multiple lines I just continue on the next line by hitting enter and then I have green and then green is going to be the list 0 comma 255 comma 0 like that and then our comma and then we go to belue like that close the string and then the colon and then blue would be 0 comma 0 comma 250 five like that close the list put a comma and then we have San and that is going to be cyan is going to be zero on the red and 255 comma 255 so we're mixing green and blue to get cyan okay comma and then cyan magenta like that colon and then what's the value that would be cyan would be red which would be 255 comma zero on the green and 255 on the blue okay and then yellow okay I played around with this and the green is really very strong so I've got to turn that down so I can't just do digital wres I'm going to have to do the analog rights which means we're going to have to do the pwm but I'm going to set this to I'm going to set it 255 and to get that yellow I wouldn't put 255 on the green I'm going to cut it all the way back to 63 because I played around with this a little bit and zero and that makes a pretty good yellow on my particular LED and then orange orange like that is going to be I'm going to cut the the green back even further it's going to be 255 comma 32 cut it in half and zero okay yours might vary a little bit then I'm going to have white and the white is going to be full on on all of them 255 comma 255 comma 255 okay like that and then oh I have not been putting my commas in there I've been a very bad boy does not count as an error if you catch it before you run it so we'll just pretend that didn't happen Okay and then off is going to be 0 comma 0 comma 0 like that okay and now I've got to close the dictionary I've done a lot of crazy topping here so I'm just going to go ahead and print RG GB RGB just to make sure that I didn't have an error there so let's come up here and let's run this thing and look at that we get our dictionary here let me make this where you can see it a little bit better okay so I've got my dictionary there that looks really good uh let's just look and make sure it's behaving like we would want so let's do give me RGB of red like that let's make sure that this is operating like we expect and let's see what we get here and we get 25500 which would be red so that's looking really good it looks like everything's working I can take that print statement out at that point and now I've got to import my Library so I'm going to say from machine import pin and I'm going to have to do pwm so I'll import the pwm and then uh those are the things I need to import now let's set up our pins we said red pin was pin 14 green pin was pin 13 and then blue pin blue pin was 12 okay that looks good now let's go ahead and set the pins up for pwm so the red LED will be my object for that channel and we're going to do pwm the method that we imported from machine and that is going to be the method pin which we imported from machine of what red pin like that okay and I've got to close the pen and I've got to close the pwm green LED is equal to pwm of pin of green pin green pin like that and then blue LED is equal to pwm of pin of blue pin do you see when you name your variables kind of consistently it just makes it a whole lot easier to program so try to be consistent in your naming convention and you can just kind of of get through this really really easy now we need to go ahead and turn those things on so I'm going to say red led. frequency and we've done this before let's set the frequency of the pwm signal to 1,000 uh which would be 1,000 Hertz okay and then the red LED d. Dot and that's going to be an unsigned 16bit int and we will set that to zero so it'll be off initially let's go ahead and run this to make sure that I didn't put in any mistakes there I did Duty what is that Duty unsigned ah unsigned 16 didn't get my U okay that's why I like to check things that looks good now I'm going to copy this and then I'm going to paste it twice and now I can just come in and say red red green green and blue blue so now I have set up the pwm I've set the duty cycle to zero and so all of the LED should be off if I run this okay I run it there's no errors and that looks good good okay now what I need to do is in my main while loop while true okay while true I'm going to come in just for good practice I'm going to set up a keyboard interrupt because that's something we learned how to do last time where we're going to put the while loop inside of a try and this will allow us to do a control C to exit the program this isn't really using threading or dual core so if you just hit the stop button or if you rerun it works fine but I think it's good for us to get practice of doing the stuff that we had done before and then what we can do is we can do an accept and then keyboard interrupt how does that not like that except keyboard interrupt it's not recognizing that what did I do wrong uh what did I do wrong there let me go back and look at one of my other programs that I did earlier I'm not sure what I am not doing right there I'm just going to look at this and see if I can figure out key accept keyboard interrupt I'm going to copy that I'm I'm not sure it looks like I what I tried to do there but over here it didn't like it I lost my program how do you like that I was writing this program and then I lost [Music] it okay here it is this is where I was doing it okay sorry sorry for that little uh now I did a [Music] try and I must have misspelled something here let me let me paste what I copied from the other ones you guys probably see it okay I'll come back over here and I will copy this and for some reason it's not recognizing it you see I'm expecting keyboard interrupt to change colors there it is keyboard inup I forgot the T okay so you guys I'm sure saw that but there now we have it so now when we get a contrl c what we could do is we could come in and make sure that on the keyboard interrupt we're going to turn everything off okay so we're going to go ahead and exit cleanly so I'm going to get this line of code I'm going to put it here and I'm going to get this line of code so this when we when we kill the program what it's going to do it's going to turn all the LEDs off and then I'm going to print program terminated just to make sure that I get down there okay and now just to make sure this works up here in our main Loop I'm just going to say pass and I'm just going to make sure that this runs without an error okay runs it looks good now if I put a control C it turns everything off and it says program terminated so that looks good now what I'm going to do here though is is in the while loop I'm am going to say my color is equal to input what color do you desire like that colon space close the string close the input okay now I'm going to say my color is equal to my color. lower so no matter how they enter it it's going to change it all to lowercase so if uh my dictionary is based on red all lowercase and they type it in red all uppercase it'll still work and then I'm going to say if my color is not in RGB so if my color which is going to be the key so if RGB does not have a key of my color that I entered then what I'm going to do is I'm going to say print my color comma and then add to that the string start with the space is not a valid color please try again like that close the string close the print okay now on the other hand if my if my color is in is in RGB then what do I want to do I want to make color which will be a function we're going to Define my color all right now to do RGB using pwm it's not like on the Arduino just a simple analog right you've got to go in and you've got to do the pwm calculation and all of that and so I want to do that in its own function so that I set up that function and I really don't have to worry about it I really don't have to worry about it so what I can do is I can come here and I can say Define make color and then what's it going to get it's going to get my color okay I'm just going to call that MC just showing you we don't have to use the same we don't have to use the same variable there and so then what I'm going to do is I'm going to make Global RGB now why did I do this because this function needs to know the dictionary so I need to make the dictionary Global so that this function will know what it is and both the main program and the function are working with the same RGB okay now what I can do is I can say red Val comma green Val comma blue Val is equal to RG RGB RG be of what my color MC like that although it should be like that my color I think okay now let's run this make sure that I haven't introduced an error okay what color do you desire let say red okay and it it's working it's not turning the LEDs on yet we can come over here and look at this cuz we'll be turning it on in a minute but it's running now just to make sure that it's getting things let's say I'm going to print my color the whole thing and then I'm going to uh or that's the one that I want and let me print RGB like that the whole dictionary and then I'm going to print let's just print red Val to make sure that it is getting what I want there okay so let's try this okay and then what color do I desire red and then when I do that I get the whole dictionary and then I get red now I don't think these things are naturally Global I think you have to make them Global and let's run it without making it Global and see what happens okay what I want is red okay look at that I guess I guess the dictionaries seem to be automatically the dictionaries are Auto automatically Global where the list aren't so I don't have to make it Global there learn something new there all right I learned something new it it seems to make the dictionaries Global even though the list are not Global okay now that worked okay so now I've got a red value a green value and a blue value now to turn those things on what I have to do is set the duty cycle so red l. Duty uncore unsigned 16 bit and then what do I have to do well first of all I know that this command wants an INT and I'm going to be doing some math that won't give me an INT so I've got to make sure that what's inside here I turn to an INT and how does the duty cycle work 100% duty cycle is 65,500 35 and so everything needs to be scaled by 65,535 so 65,535 would be a 100% duty cycle that's full on and zero would be a 0% duty cycle and everything needs to scale between there and so what I would do is 65 535 divided by 255 * red Val okay and now I close the int I close the duty cycle so what does this mean if I have a red B of 255 that would be 255 ided 255 * 65535 that would apply 65535 which would be 100% duty cycle okay now I've got to set the other one so I'm going to take this copy it and then I'm going to paste it only this time it's going to be green g re n and then this would be the green Val the green Val and then I need to still do blue so I'll come here and do a blue that's why I gave you yellow and orange because that forced you to do pwm and not just a simple red val. value okay you couldn't just do a value one or zero you had to go in and do this which reviews things that we've learned earlier on okay now let's take this and let's run this okay what color do you desire this time we're going to keep all eyes on the LED and I'm going to say I want red ah that did not ah I see what it was this should be blue v okay this should be a blue v like that so let's try running it again you see it kind of made it magenta it looks like okay so now what am I going to do I'm ah I need to come down here make sure that we're down here so I'm going to say red boom green Boom blue boom okay now San now what if I do menta like that magenta boom I got a magenta what if I do a fushia I get a fushia is not a valid color try again red green blue cyan magenta yellow and yellow whoa that is a really good yellow and then orange that is a good orange and then what I'm going to do is white that's a pretty good white and then I'm going to say off boom look at that okay so guys what you've seen here is do you see the power of this all the heavy work is done up here in this this is all of the heavy work right that's all the heavy work and then the other heavy work is done in this make color now as we're coming down here and writing the program it's all very nice so let's go ahead now and see make sure that our cont uh let me let me go down here again let's turn it back to magenta okay magenta and now if I do a contrl c to exit the program what it does is it says the program program is terminated and it came down and it turned everything off and so that again sort of shows you the nice thing about a control C and using this try accept it allows you to kind of clean things up as you are exiting the program okay now what I'm going to do is I'm going to show you how you could kind of take this one step further probably shouldn't do this because I haven't really uh practiced it but what I'm going to do is I'm going to come over here and this I am going to get this and I'm going to get my dictionary and I'm going to come over here and I am going to paste my dictionary right here and now I'm going to save this as a library and so I'm going to do a save as okay and in fact let me view my files so I'm going to come here and I'm going to view my files and so I'll look at view I'll look at my files and I want to make a library in this Li folder okay I want to make a library in this lib folder so I doubleclick on it so I'm in that folder and now what I'm going to do I am going to save as on the Raspberry Pi and notice I'm in the lib folder I'm going to save this as RGB dopy now I've got to say don't name your library the same thing that you name your uh dictionary you kind of want to use different names because sometimes you can get unexpected results and so I want to do that now what do I do I have this Library rgbp ins inside of lib and now if I come back over here what I can do is this Library I can take it out of the main program and I can say from RGB not RGB Pi just RGB import Star so it's going to import everything out of RGB and it's going to import it and then I think it should know what RGB is so now let's run this okay what color do I desire and I'm going to say red boom green Boom blue cyan magenta yellow orange and then we're going to say white and we're going to say off boom okay do you see how now now what we do is we have a library we have a library that contains the dictionary and then all we have to do is import that dictionary now if we ever want to do RGB stuff again if we want to do RGB stuff again what we can do is we can uh we can just import the library now I showed you in an earlier lesson how to create these libraries and so what your homework assignment is your homework assignment is to edit that program okay edit that program where you also put make color inside of the library and then you don't even have to Define it here okay but you've got to sort of see that you remember what we learned from our earlier lessons youve got to take this and make this part of the library and then when it does the uh when it does the uh calling of make color it is calling it as a function inside of that uh inside of that Library so go ahead and do that as a homework assignment always just a remember reminder when you do your homework Post Your solution to YouTube in the comments down below include a link over to your homework assignment okay include a link over to your homework assignment and then in your homework assignment reference back to this video so people will see what you're what you're working on and I look at every single video every single homework assignment that you guys post okay guys I hope you're having as much fun taking these lessons as I am making them as always I want to give a shout out to you guys who are standing with me on patreon your support and your encouragement by supporting my patreon is what keeps these great lessons coming you can also help me by giving me a thumbs up and leaving a comment on the video if you haven't already subscribed to the channel when you do ring that Bell so you'll get notifications 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 do.com I will talk to you guys [Music] later
Info
Channel: Paul McWhorter
Views: 1,589
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: 1CHcjlaBvGY
Channel Id: undefined
Length: 30min 23sec (1823 seconds)
Published: Tue Jun 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.