Raspberry Pi LESSON 13: Set Color of RGB LED with Push Buttons

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 13 in our incredible new tutorial series where you're teaching your raspberry pi who's 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 as always i want to give a shout out to our friends over at sun founder sun founder is actually sponsoring this incredible series of video lessons and in these lessons we are using the most excellent sun founder ultimate raspberry pi kit hopefully most of you guys already have your gear but if you don't have your gear yet look down in the description there is a link over to amazon where you can pick up a kit and believe me your life in my life will be much easier if we are working on identical hardware but enough of this shameless advertising let's talk about what i am going to teach you today and what we're going to do is we're going to look at my solution to the homework assignment that i gave you in lesson number 12. and what that homework assignment was was to take the circuit that we had in lesson number 12 where we were controlling the rgb led with three buttons one button was on and off for red one button was on and off for green and one button was on and off for blue where we could turn it red we could turn it green or we could turn it blue well what your homework was was to go away and instead of just having red on off green on off blue on off to be able to adjust the red and the green and the blue values to mix colors to get that rgb led exactly the color you want it any color not just rgb but mixed colors to get the entire rainbow the rainbow okay and that's what the homework assignment was oh look we got a fisherman going by i know how you guys love to see the fisherman so little indigenous fisherman trying to earn a living out there you know i can actually go flag the fishermen down when i want a nice fresh caught tilapia i just go down wave they run over and i get a deal on a fish but i digress how many of you guys were successful with the homework if you were successful leave a comment down below i am legend and if you were not successful leave a comment down below i folded up like a cheap walmart lawn chair okay i'm hoping most of you guys got this homework because it really was it really was a straightforward combination of methods and techniques that we already learned in the other lessons now i'm kind of deliberately doing these lessons so that i create a new assignment that pulls together different things that you learn from old assignments so that you sort of learn how to put things together and then also you're reviewing and gaining more expertise with some of the techniques and methods that we learned earlier and so i hope that actually makes sense yeah there goes our there goes our friendly fisherman i find it very peaceful and calming just as i'm working here to look out and see those guys out on the on the river uh catching their fish and providing for their families through good honest work but again i digress so let's jump in and let's talk about what we're going to do today let's go to our overhead view and this is going to show if i get out of your way this is going to show the circuit that we ended up with last time and i think that i will probably you guys can go back and watch the last video if you want to if if you haven't seen it to figure out what we're doing but i'll show you the schematic right here and the schematic is we have an rgb led the red channel is the leg of the led on the left the long leg is there the second the second pin that i have hooked to ground but the r channel is going through a current limiting resistor 330 ohms and it is going to pin 37 the lower left pin is pin 39 that is what we're doing for ground you can see from pin 39 we are hopping over and creating a ground rail and then the pin above that is 37 that is our red channel then to the right of the ground over here we have our green channel and through a 330 ohm resistor we are hooking to pin 35 and then the blue channel the one on the right through a 330 ohm resistor is going to pin let's see that's 39 37 uh 35 to pin 33 and i'm just going to glance over here to make sure that i'm saying that right yeah it's it's 37 35 33 rr3 channels and so that is how we're controlling the led this is like the third lesson that we have done this in and so you guys ought to kind of understand that by now and then over here we have the red button we have the green button and we have the blue button from the most excellent sun founder kit and guys just know that there those are those big push buttons and the little colored covers are sort of separate so you've got to get those from a different little bag if you haven't put this together already and so then what we have is we are not using circuit pull up resistors because we're using the internal pull-up resistors on the raspberry pi and so we have the red channel going to the most excellent the red button going to the most excellent pin 11 and then the green button is going to pin 13 and the blue button is going to pin uh 2 pin 15. so it's 11 13 and 15 on those buttons and then the right leg of the buttons are all hooked to our ground rail and so that should be everything that you know need to know in order to get your circuit set up like mine now i'm going to actually move this up here so we are going to be able to see the raspberry pi see our circuit view and then also we will need to come over here and we will need to be able to see the code view okay what is our goal our goal is to be able to mix colors and so if i press the red button once it should just barely come on red and then every time i press it it should get brighter and brighter and brighter but i can't go above 100 in fact i can't go above 99 and so if it gets more than 99 we circle back around so i don't have a red up and a red down button i have one button it's the red button it goes up until it gets to 99 or if it gets more than 99 then it circles back around and starts back at a little under one and i'll explain that i'll explain that in a minute as far as how how i'm thinking about it okay so i will need you to fire up a most excellent terminal window and then what we are going to do is we are going to nano and we are going to nano down into our python folder and then i'm just going to call it dim rgb dot py and hopefully there's not a program there okay good fresh new python program just waiting for you to write it now i'm going to try to be logical in the way that i name my pins and because of that i'm going to be able to go through pretty quickly and put some thought into your variable names guys giving good variable names is one of the best programming practices that you can learn and so watch how i'm doing these variables you guys watch how i'm doing these variables because i'm naming them quite deliberately and quite analogously so that i can code quickly well if we're going to play with those gpio pins we're going to need to import our old friend uppercase r uppercase p little case i lowercase i dot gpio we'll import that as gpio you can import it as anything you want but gpio makes kind of sense from time import sleep because we will need to have some delays in here let's go ahead and set up our delay time dt as 0.1 that would be a 10th of a second let's go ahead and set up our rgb pins the pins going to our rgb leds so there's going to be our pin and if you remember that was pin 37 there is g pin and that was pin 35 and there was blue pin and that was pin 33 okay now we also had the buttons the red button our butt that is going to be equal to pin 11 if you remember there was g but for g button and that was pin 13 and then rgb but and that is 15. also do you see how i like to kind of use sets of those gpio pins that are adjacent in that way it's kind of easier to keep track of those numbers so we've got those all set up now we've got to keep track is the button is the red button up or is it down so that is going to be the r button state and that will set to one because with an internal pull-up resistor if we're not pressing it it is in one so we're just assuming uh that it's natural that when you start to program nothing has been pushed so we're just going to start with it in state one and then r but state old is going to be equal to one because right what is a button push when it's down when it's zero and then it goes for a one so we're looking for a transition from zero to one so i've got a no button state which is where it is now and then i have to know the button old state which is where it was before and so i need to do one quick little video management thing here if you would be so kind as to let me have a second okay sometimes i gotta refresh the camera because it gets a little tired so that should be we should be good to go there so now if i have an r button state i'm also going to need a g but state and that's going to be equal to a 1 and then a g but state old and that's going to be equal to a 1. it is typical to use an equal sign instead of a closed bracket and so we will go with the standard dare and now rgb but state equal 1 and then b but state old equal 1. so do you see how fast you can go r but g but b but it's rgb it's rgb and then i'm using similar names for the different channels and so it just it just really kind of helps it go along quickly there when we do it that way and so i hope you guys will learn how to name your variables well and to sort of keep track of things well all right so now we have those uh we have those button states what we are also going to need to know is what is our uh i don't think we need to know i don't think we need to know the state of the leds maybe i'm just thinking we're just going to keep track of now we don't need to know the led state because we're keeping track of the duty cycle because we're going to be using pwm guys we're ready to start setting up our gpi opens gpio dot set mode and that is going to be gpio dot board that is using the physical pin numbers okay now we're going to need to do gpio dot set up and what are we going to set up we are going to set up our uh our but no no no we're going to set up our r pin and that is going to be a gpio dot out like that so that is setting the red led pin to an output then gpio dot setup and then the g pin gpio dot out like that and can you guess g p i o dot set up and then the b pin is a g p i o dot n no it's an out as well like that just making sure you guys are paying attention [Music] all right so we got those set up and next we are now ready we are now ready to set up those input buttons we're going to set up our buttons now and remember we've got to set those pull-up resistors when we do that those internal pull-up resistors because i didn't put pull-up resistors in our circuit here so we're going to jump in and we are going to say p i o dot set up and this is the red button the r but and that is going to be gpio dot n and now we got to set the resistor so it's going to be pull up down is going to be equal to gpio dot pull up down underscore up like that we're putting in a pull up resistor gpio dots set up whose turn is it it's g but's turn and it's gpio dot n pull up down is equal to g p i o dot pull up down and it will be a pull up resistor and now we will do a gpio man that might have been behind my head and if it was i'm sorry let me go ahead get down here okay gpio dot set up and whose turn is it be buttons turn and gpio dot in pull up down pull up down is equal to gpio dot pull up down and this one will be a down just kidding it will be enough as well just want to make sure you guys are awake all right so we got those done now we need to go ahead and set up our pwm on each one of those so i'm going to say my pwm for the red channel is going to be a gpio.pwm [Music] and which pin is that hook to it's the red pin and we got to think is it the red pin yes we're sending pwm to the led and the led was using the red pen green pin blue pen we just got to keep track of the difference between the buttons and the pins on the led okay and so that we're going to set at 100 hertz and then my pwmg is equal to gpio dot pwm and that's the g pin and that'll be a hundred my pwm b is equal to gpio dot pwm and that is going to be red green blue pin and that's a hundred and you guys see my mistake it doesn't count as a mistake if you change it before you run it and so it's as if that little thing never even happened there okay i'm also just kind of glancing up do you see the other nice thing about the consistent naming where i have here the r the g and the b everything else should be identical so i can just scan along here and i can see that things are looking good and so i'm just kind of scanning my code and it all looks good so far to me maybe you guys have seen something but it looks good to me now let's go ahead ahead and let's start those pwm channels and when i start them we have learned that you can use a zero duty cycle which is like constantly off and so i'm gonna go ahead and i'm gonna let's just say d c d c r the duty cycle for the red i'm going to set at 0.9 and then the duty cycle of the blue i'm going to set equal to 0.9 and the duty cycle of the blue blue is equal to 0.9 remember i said you should always go rgb rgb rgb so i need to go rgb and not go r uh rb g it's rgb like that okay now why did i set it to 0.9 because the way i'm going to do this remember these colors don't respond linearly and so we don't want to step the color value linearly by like adding 10 to the value every time we want to do it exponentially or multiplicatively and the thing is if i do a multiply and if i start with my duty cycle at zero and then i come in the next time because i press the button and double it it's not going to work so i set it at 0.9 and then if i double it it'll come on so i don't want it to start on i want it to start at zero and so this is how i will do it and this will make sense i think when i do it the second time but i'm doing it the first time here just so that you can kind of make sure that you understand it so i'm going to do my pwm red is dot start and where am i going to start it at the integer value of dc r okay so if dcr is 0.9 and i take the integer value of that it makes it a 0 and now i am applying a 0. but doing it this way now if i come in and i double dcr what happens i end up with 1.8 the integer value of that is one and it'll come on dim so you see there's a method to my madness here okay so now we'll have my p p w m g dot rgb start start and then the end of what duty cycle of green my pwm blue dot start and that's going to be the end of what d c the duty cycle of blue close close all right so this should then everything should come on off right everything should come on with the duty cycle of zero the led should be off when everything starts all right so now i think we should be ready to do our try and accept and so we'll start here with a nice try and then we'll go ahead and take care of our bookkeeping by doing the accept and so what we want is when we have a keyboard interrupt which is the control c when you get a control c you want to jump out of try and come down to accept and what do we want to do down here in the accept we want to do a gpio dot clean up and then i want to put a print statement in here [Music] i'll put a blank line and then i'm going to print and then i'm going to say gpio good good to go indicating that the gpio have been cleaned up and then i'll do another print just like that i like to put that print statement so that when i kill the program i can actually see that i have cleaned up the gpio pins because you saw in the last lesson like from a previous work i was doing i'd left one of those pwm signals on and then when i came in and wrote a new program and i activated that pin it came up in pwm it was like crazy what was happening and so we really want to make sure that we are getting our pins cleaned up hopefully you guys know to do that okay so we got the tries the accepts done let's come in and now let's write the real this is where the real action happens and so we need to start thinking about what we would want to do here and what you know is what you know is we are going to need a loop right we are going to need a loop in here and so let me come down here and so we're going to do a while true when is true true true is always true we've created an infinite loop it'll loop in that while true until when until ctrl c then it jumps down into the accept now what do we want to do here we want to read the three buttons and so i'm going to say r but state is equal to g not ru r but state is equal to gpio dot input and we read it from the r button not the rpin our button and we got to be careful with our case now we want to read all three so g g but state equal gpio dot input g but and again you see because i'm writing these variable names the same i can very clearly see there was a mistake there but it's not a mistake because i caught it before i ran it and then also here this jumps out at you as a mistake so rg rg whose turn is it mr b but state the b button state is gpio dot input and that is the b button so now i've read all of the button states let's go ahead and print them out just so i can kind of see visually it'll help us debug if we need if we need it and so what i'm going to do is just say print button button state and then i'm going to print what i'm going to print r but state g but state b but state [Music] man [Music] i think that if i come down okay you can you can see that i'm trying to stay out of your way so now i print those values just because it'll help us debug sync where are each of the buttons and so that's kind of i think that's kind of a useful thing to do so we print those r but state g but state b but state and now we print all those out now we want to see has the red key been pressed which means last time it was a zero and this time it's a one so if r but state equal equal one and what are but state old equal equals zero that means we have gotten a actionable button press so what do we do in that case well in that case what do we want to do we want the duty cycle for red to increase so i'm going to say duty cycle red is equal to what was that multiplier that we did the math for with the logarithms that seemed to make it step to about 99 in about uh it went to 99 in about 10 steps i do believe that that number was something like 1.58 so we're going to say dcr is equal to dcr times 1.58 and this multiplier you can play with do you want it to go to full red color in 10 steps in 5 steps and 20 steps well you would affect that by what number you make this the larger you make this the quicker it will get to full value the smaller the more button pushes it'll take to get to full value i hope that makes sense okay now what we are going to do is just to see if we got into this if statement we'll say red channel [Music] registered like that just indicating that yeah we saw a button push now one thing we got to do is if i'm at like 80 and i multiply by 1.6 and i'm greater than 100 you don't want to have a duty cycle greater than 100 actually you don't want one greater than 99 so i'm going to say if dc r is greater than 99 what do we want to do we want to loop back around and we want to go back to what dc r is going to be equal to 0.9 so we sort of circle back and then this of course is a top row so if dcr is greater than 99 then we want to make dcr what we want to make it 0.9 so it'll circle back around i hope that makes sense it really should i think i think it's pretty clear okay now what is the one thing that we want to do here while we're still in this we now need to change the duty cycle so my pwm red dot what do we want to do we want a dot change duty cycle like that change to the cycle like that and then what do we want to change it to the end of [Music] dc duty cycle r close the end clock close the change duty cycle all right so what does this mean this means i start at 0.9 what is the integer value of 0.9 it's 0 the led starts what it starts off first time i press it it's 0.9 uh actually i want that closer to 1 so i'm going to say 0.99 okay and that i think will get us closer to where we want to end up okay so 99 is still going to go to zero okay but now dcr times 1.58 should take us sort of like where we were in the last lesson okay so dcr is equal to 0.99 integer of 0.99 is 0. so if we get to you know dc r is 90 we multiply by 1.58 that's more than 99 it takes it back to 0.99 which will write as a zero and so the led will get brighter brighter brighter brighter and then it'll go off and then it'll get brighter brighter brighter and it'll go off so i think that should work right now what's the one thing that we've got to make sure a little bookkeeping that we do here and i'm going to go ahead what i'm going to do is i'm going to run this and see if it's working on red because if it's working on red then it'll be easy to do green and blue but what i need to do is i need to do a little bit of bookkeeping right i need to do a little bit of bookkeeping and that is i need to i need to come in and i need to say are but are but old are but state old is equal to r but but state like that and then g but state old is equal to g but state and then b but state old [Music] something is wrong there there it is you see that extra e showed up because the equal wasn't in the right thing b but state so now i'm just glancing over this rgb rgb but state all the way down but state old all the way down and so that all looks good and now i'm going to go ahead and just so i can keep track of things i'm going to print the dc the dc duty cycle for red the duty cycle for green and the duty cycle for blue and what this will just be as as we're watching the led do its show we can kind of see what the numbers are that we're actually putting on it so i think this will be i think this will be helpful and then don't forget that we always need to have a little bit of a sleep in here of dt which we already set up so that it's not just running too quickly okay i think that this might work for the red led now i think it might work for the red button let's see if we if we have the red button working then doing the green and the blue will be easy so i'm going to control x yes enter and then we are going to come over here and we are going to what we are going to python 3 this thing and then everyone hold their breath what is that python 3 not found ah no uppercase p and python that scared me okay everybody hold their breath the problem was one of you guys didn't hold your breath so i need whoever you were and i think you know who you are you need to hold your breath what is that oh start misspelled start in line 42. so hopefully you guys saw that so let's look in our pwm starts how did i not catch that [Music] okay control x yes enter everyone hold your breath please no more messing around with me hold your breath this channel is already in use okay so uh what would this be what would this be this would be that the last time we ran it it crashed after we set up those things so it's saying all of these things are already in use okay but now it also looks like there is still a uh there is still a problem in line 42. so let's go back and look at that again and what am i doing wrong here i thought i just fixed that my pwm b is start it that sure looks right doesn't it that sure looks right and i do set it up here my pwmb my pwm rgb our pin those sure look right let's go ahead and try it we'll get to the bottom of it don't worry python3 hold your breath [Music] now what is this nonsense uh channel already in use channel already in use we understand all of that line 42 my pwmb sir i misspelled it dog done it how could i have done that and i thought that we had finished that i thought that we had fixed that but apparently i cannot spell start apparently i cannot spell start okay take that you aggravating thing all right now this time we're gonna really run it hold your breath okay boom at least it is running now what's this thing the button state is one one one meaning that there are no buttons being pushed and then the duty cycles is 0.1.99 which is going to end to a zero and the led is off and so we are going to press this red button hold your breath okay we're going to let it out boom look at that shazam we are now at a led we're now at a duty cycle of 1.5 which is going to write at a 1 and so that is the dimmest possible value that we could have on that led it's one part out of a hundred and so you can see it is barely lit and then if i press the button again it goes to 2.47 that would write as a two and you can see it got a little brighter and now a little bit brighter at 3.9 that would write as a 3 and then that will write as a 6 and then you can see it's getting brighter and brighter with a 9 a 15 and then a 24 and then a 38 a 60 and then a 95 and so that 95 is close enough to 100 and so i kind of like that whatever that 1.58 multiplier that we used and that looks pretty good now what do you expect if i press the button again somebody tell me what do you expect if i press the button again shazam look at that who's the magic man i'm your magic man look at that it's circled back around and then i press it again and boom there it is back at very very dim okay i it might just be me but i think we have the red channel doing exactly what we want it to we just showed the red channel who's boss okay so let's go in let's get those other two channels working really quickly so we'll nano into this not too quickly because we don't want to start making stupid mistakes but now we're going to come in and we should be able to put in that was not what i wanted to do we should be able to put in two more if statements okay two more little if uh clauses okay so now if and for some reason my copy and paste doesn't work and so we're just gonna have to old school this but if g but state if g but state equal equal one and g but state old equal equals zero then what do we have we have a g button push and so in this case the duty cycle of green should be equal to the duty cycle of green times our magic number 1.58 okay and then we're going to print print [Music] green channel registered like that that we got that button push and then what do we need to do we need to say if the duty cycle of green is greater than 99 what are we going to want to do then we are going to want to if the duty cycle of green is greater than 99 we're going to want to circle back to duty cycle of green is equal to 0.99 like that is that right i'm just checking my work here okay that looks good now what do we need to do we need to see my pw m of green my pwm green is going to be change duty cycle and then what are we going to change the duty cycle to the we're going to change it to the the int of dc what g okay i'm just glancing over my work here i think that looks good is that all we got to do i think that's all we got to do now we're going to come real quickly here b but state if that is equal equal 1 and man i haven't had an error up here did you guys see that i said b where that should have been g so g g and now b b but state old and let's get that right p but state old and if that's equal equals zero then we got a few things that we will need to do one is dc b is equal to dcb times the magic number 1.58 that looks good now in the case that dc b is greater than 99 what will we need to do there we will need to make c b equal to 0.999r and then we will need to go ahead and my pwm b dot change duty cycle change duty cycle to what int of dc b could it really be that easy okay control x yes enter and now i need everyone to hold their breath [Music] okay at least it's running now i want to be methodical so i'm going to step through the channels one at a time so i'm going to start here we expect red to work but okay it came on 1.5 2 3 6 9 15 24 38 60 95 is good and shazam it is back to 0.99 which writes as a zero now the moment of truth we're gonna press the green button one time and boom look at that green came on very dim and the green value is 1.5 writes is a one and then we're going to bring the green up and look at that look at the green coming on up look at that coming up to a 95 and then boom led is off we're gonna step through the blue and the blue is dimly on and then we're gonna step it on up the lovely blue up to a 95 and then boom back to zero okay so because each one of those runs individually we feel very confident now and i feel that you really need to get my full facial expression so we're going to come back to a little bit larger view so you can see the sense of excitement on my face as this is actually going to work now so i'm going to turn red on one click you can see a little bit of red now if i turn blue on one click what do i have i kind of have a magenta red and blue make magenta and so i'm going to bring the red on up okay and you can see that it's still kind of a nice purple we're kind of going in that that purple magenta space there and now let's go ahead and bring the blue up some more and we'll bring the blue up to match that and you can see that we have just a lovely a lovely magenta on here i actually wish that you could see it live well if you're doing the project you can but it's just this overhead camera the studio camera does not pick up the color of the led very well the led is bright and it just sort of blows out the blows out the camera but let's come on up let's see if we just bring in a whole lot of blue okay and you can see that here we've got an interesting color of blue that's a blue with just a little bit of red which makes it just a hint of purple and then we're going to go back down and then we'll turn the red all the way up okay and i went past it and we want to turn the red all the way up and back to that so now we're off now let's kind of play with the yellow values so i can turn on the red and then i turn on the green and i get a little bit of a yellow but what you can see is there's a lot more green than red so it's like that green led is is is kind of overpowering the red so i've got to bring the red up and now i've got a beautiful yellow okay i've got a beautiful yellow more red is bringing it more to orange and then if i keep bringing it up it's turning into a very reddish orange and then i could add a little more green to it and mix it uh mix it that way but now let's go all the way and turn those off that one's off let's turn green off and now let's play with the aquas okay the aquas would be a little bit of green okay and a little bit of blue and again the green seems stronger than the blue and so i'm going to bring the blue on up and wow that green is really sort of very powerful compared to the other colors and even there i'm seeing a lot of okay now i'm starting to get more of an aqua okay i'm starting to get more of an aqua and it helps if that blue was actually working so let's try it again okay the green is overpowering it but yeah now i am getting a beautiful aqua and i can bring in more green and i can bring in more blue and i have an absolutely lovely aqua and i just wish that you could see it in person as beautiful as it is to my eyes here okay guys that is pretty darn exciting to me and i hope you all were successful let me know if you were successful also i hope that you guys are posting your homework to youtube every homework that you post to youtube i look at every single one of them and then i try to leave comments on i just like to see that there's people learning and there's people actually doing uh actually doing their homework okay now you guys can play around with this i'm not giving you a formal homework assignment for next week but what i do need is i do need you guys to i do need you guys to uh let me come back over here and what i need you guys to do is i need you guys to leave this circuit hooked up and keep a copy of your code because next week we're going to start with this circuit and with your code and i'm going to show you some other kind of cool things that you can do with the raspberry pi but i don't want to have to like recode this and re-hook this up so your homework assignment for next week play around with this show your friends impress the ladies or if you're a lady impressed your men friends with the amazing things that you built here and then we will start with this next week this will be our starting point for the work that we are doing in what will be the epic the epic uh lesson number 14 which shall come what's that birdie's gonna die boom those are called kingfishers and they they dive down into the water and come up with a fish i like watching that too 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 helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet look down in the description link over to my patreon account hop on over there and hook a brother up also you can help me by leaving a like down below and by leaving a comment if you like it and comment that helps me with the old youtube juice and that means that youtube will show this video to more people and that's important because the world needs more people doing engineering and coding and fewer people sitting around watching silly cat videos paul mcquarter with toptechboy.com remember resist the metaverse i will talk to you guys later [Music]
Info
Channel: Paul McWhorter
Views: 8,220
Rating: undefined out of 5
Keywords:
Id: UpkPVeAdcNQ
Channel Id: undefined
Length: 46min 39sec (2799 seconds)
Published: Thu Jun 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.