Raspberry Pi LESSON 16: Creating a Potentiometer Controlled Dimmable LED

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 16 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 most excellent series of raspberry pi lessons and in these videos i will be using the most excellent sun founder ultimate raspberry pi kit they might call it the davinci or something else now but if you look down in the description there is a link to the kit that i'm using hopefully most of you guys now that we're on lesson 16 already have your hardware but if you don't have your gear yet look down in the description there is a link over to amazon and you can order your equipment [Music] but enough of this 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 that i gave you in lesson number 15. first question is how many of you were successful if you were successful leave a comment down below i am legend if you are not successful leave a comment down below i fold it up like a cheap walmart lawn chair hopefully most of you guys got it because really this was a pretty straightforward lesson pre pretty straightforward homework what we were doing was just putting together things that we had learned in some of the earlier lessons i like to do that how do we learn we learn by exercising skills so okay so we learn a new skill and then what we need to do is we master it and we master it by doing it more than one time that's why i try to build homework assignments that utilize things not just from the last lesson but from lessons that are earlier but enough of this introductory discussion let's jump in and let's look at how i did this assignment what the assignment was was to create a dimmable led where you were using the adc-0834 analog to digital converter to read a number off of a potentiometer and then to use that number to control the brightness of an led okay let's see how i do that i always like to start by showing you my studio show you that i am keeping my studio neat and clean you see that i guess this book is not that my little notebook is not exactly straight we need to get that straight then down here you can see this is my build area and what we'll be doing now is we will be going to an overhead view of the build area along with the circuit that i actually used in this uh you know in this solution so first of all you can see up there is the schematic and then here is a live shot of the build a lot of this is very similar to what we had done last week we had this breakout board coming from the raspberry pi this sun founder breakout board this t-connector coming to the breadboard to make our connections easy we have the adc-0834 analog to digital converter here we have a potentiometer here we're reading off of the center leg of the potentiometer and we're coming into pin three of the a to d converter and that would be channel zero and then that was all done last week what we did this week is i added a 330 ohm current limiting resistor i connected it to an led the long leg of the led and then the other leg of the led is grounded up here to the ground rail the one thing is i connected this resistor this resistor to gp to the gpio pin 23 and that's pin 23 in the what pin 23 and the bcm numbering system now why did we go from the board numbering system to the bcm numbering system in the middle of these lessons well that's when we started using the breakout board because the breakout board is using the bcm numbering system so then it's easier for us to use that because the labels then match what our code is all right so you guys hopefully already got that built and so i will come over here to the code view i will come over to the code view if i can find it okay there it is and then i'll go ahead and scoot this out of the way a little bit so you can see what we'll be looking at in a minute we'll be looking at the potentiometer and the led but i think we're ready to start coding the other change we made sort of in the middle of these lessons we went from nano which is a simple text editor we started using thony or thani whichever way you want to say it and so what we're going to do is we're going to go ahead and fire up bonnie and then what i'm going to do is i'm going to go ahead and save an empty program and i'll just call it uh my pot dim.pymypotdem.py i am in my python folder which is good i'm in my python folder and i can get this up here and i will say okay so i think we are ready to code what's the first thing we need to do is we need to import our friend who is our friend mr rp lowercase i dot and i will import it as gpio you can import it as kitty litter box if you want it's just gpio is a little bit easier to type and a little bit more meaningful we actually had one student a mr daniel young man actually did one entire homework assignment naming his rpio live rpi library kitty litter box and you know what it worked but thought that was pretty clever so we tip our hat to daniel for that little bit of craziness okay what else do we need we need to import the library adc0834 and remember this library does not come with the raspberry pi and so if you don't have it you need to go back and look at the last video where we show you how to install that library okay then also from time import sleep why do we do it this way that way that we don't have to put time dot sleep if i just said import time i would have to do time dot sleep if i say from time import sleep then all i have to do is just sleep makes things a little bit easier now we're going to need a delay time so i'm going to go ahead and put in dt for delay time and let's just make that a tenth of a second point one now i'm going to set up my uh my led so i'll give it a red pin for the red led and i do believe we said that in the bcm numbering system that was in in fact bcm pin 23 that i had that hooked to okay then also i'm going to go ahead and set an initial duty cycle of zero that way when we start with the duty cycle of zero the led will be off if that makes sense we got that set up now we're going to need to do our gpio.set [Music] mode and what are we going to do we're going to do a gpio.what bcm because that red pen is 23 bcm 23 not board 23. okay so we got that set up we've got that set mode done now let's go ahead and turn on let's turn on our analog to digital chip and we can do that with a we can do that with an adc0834 dot what dot set up that will get that thing fired up and initialized and ready to go now we need to set up our output pin which is that red pin so we'll do a g p i o dot set up not set mode this time but set up and what do we want to set up we want to set up the red pin and we want to set it up as a gpio dot out like that that looks good doesn't it okay and now i need to go ahead and turn on my pwm because if we're going to make this a dimmable led it's going to need pwm and so what i need to do is i need to go ahead and do a my pwm i need to i need to create my pwm object so my pwm is equal to gpio.pwm [Music] and then what do i want to make a pwm my red pen my led pin and i need to give it a frequency and i like to do about a thousand hertz because that makes it so fast there's no way you can even imagine that you're seeing it blink and now we need to go ahead and we need to start the pwm so i'll say my pwm dot start and i will start it with the duty cycle which is zero in a zero duty cycle it means the led will start out being off if that makes sense now we're ready to start looping so we need to do our try and accept so we will try to do the following things and if we can't we will jump down to the except x x sept and then what do we want it to be we want it to be an accept out to keyboard interrupt so that just means that you are going to sit and you're going to loop up in the try part until what until it sees control c that's the keyboard interrupt and then it will jump down to the accept and what do we want to do there give me just a second to manage things a little bit here get my windows where i need them okay so then we'll come back down here so what do we want to do on the keyboard interrupt there's a couple of things i want to do at my pwm.stop [Music] okay i want to stop that pwm and then i want to do a gpio.com up and then what i want to do i want to print a i want to print a gpio good to go now why do i do that that way when i exit the program i want to see that gpio good to go and what that tells me is is that i've actually cleaned things up that i didn't leave in an unexpected way and leave those pins tied up i hope that makes sense now what do we do in the tri part so we've got the kind of structure the the skeleton done but what do we actually do here in the meat well we want to loop so what do we do we say while true when is true true true is always true we're creating an infinite loop and now what do i want to do i want to read the value from the potentiometer okay and so how do i do that i say potval okay potval is going to be equal to adc0834 my analog to digitalconverter.what.getresult [Music] and then i want to get the result from which channel if we look over here i'm connected to pin one two three so the center leg of the potentiometer is connected to pin 3 which we learned last week is channel 0. so if i read from channel 0 i am reading from pin 3. so we got that done now i think what we need to do is we need to change this pot valve which is a number between 0 and 255 and we want to change that into a duty cycle which is a number between 0 and 100 so we need to do a little what we need to do a little bit of math [Music] we need to do a little bit of math here so i need to switch off of this and then i think i can switch over here if you give me just a second this is the most awkward transition because the river cam and the sketch pads share the same port so i have to do a little bit more switching to get to get to this view but okay let's think about it if i read 0 on the potentiometer if i read 0 on the potentiometer that was kind of awkward see if i read 0 on the potentiometer if i read 0 on the potentiometer what duty cycle do i want well if i read a 0 i want a 0 duty cycle because i want the led to be off so if i read 0 i want to apply 0. what's the other extreme well if i read full scale i will be reading a 255 on the potentiometer and if i read a 255 on the potentiometer what duty cycle do i want well i want full on and that would be what that would be a hundred okay so i have two points and if i have two points i can make a what i can make a line but if i'm going to do a line i need to calculate the what i need to calculate the slope and so i can do that by saying m is equal to y2 minus y1 over x2 minus x1 i always like to use my parentheses because that's a very good habit and so y2 is what it's a hundred minus y1 so this is y2 and this is y1 100 minus what minus 0 over x2 which is 255 255 minus x1 which is 0 and so i am getting 100 over 255 for a slope okay and so my slope is equal to 100 divided by 255. now how do i calculate the equation of the line once i have the slope well y minus y1 is equal to m onto x minus x1 and we know most of this stuff first of all we know that this is x1 and this is y1 and so both of those are 0 so these terms are going to drop out and i'm left with y is equal to m which is 100 over 255 times what times uh x okay and guys over and over you guys say oh i don't need to use the equation of the line because if i take this and i put it over this and i do this and you tell me this little way that you do things that's going to work about a third of the time and it will work the thing you guys are doing is it will work if there is not a y-intercept if there's a y-intercept it won't work why not just do the math how long did this take i asked you how long did this take about one minute invest a minute in your life to actually do the math and do it right okay now we're not dealing with y's and x's we are starting with a what we're starting with a pot valve so that's like our x and so that's going to be here 100 divided by 255 times what's our variable we are using val as our x value and then we're turning it into we're turning it into what duty cycle or dc now i've got an equation here that i can use in code slide like i'm not going to make a mistake because i'm going to look at the equation because i did it on paper first and if there was anything i wish i could could get you guys to think like an engineer and act like an engineer and even problems i can do in my head i sit down and i do them on paper because that way i'm building that skill and i'm going to get it right i'm not going to make a mistake so now we can come back over i believe to our code view is this where did our code view go there i think that is our code view take a swig of copy i have my equation set up now so i can come back over here and what am i going to calculate i'm going to calculate my duty cycle dc and that is going to be 100 divided by 255 and then i'm going to multiply that by what pot val and that's that equation that i just came up with all right so now when i'm reading a 0 i'm going to apply a 0. when i'm reading a 255 i'm going to apply a 100 that's going to go from full off to full brightness okay and let's see i think maybe it doesn't like to apply 100 i think 99 is as high as you can go so i can just say just to be safe if dc is greater than 99 okay then make dc equal 99. that just makes sure we don't try to write something more than 100 and even 100 i think causes problems so we'll go from 0 to 99 and we make sure that we don't go over 99 with that little thing there okay now what do i need to do now i need to apply that duty cycle to the led how do i do that it's really simple my pwm what dot change dot change duty cycle okay change duty cycle and i want to change it to what to dc which we just calculated all right i also want to print it out so i'm going to print both the pot val and i'm going to print the duty cycle dc because that'll help me debug if i have some sort of problem okay now what i'm going to do is i'm going to do a sleep for how long for dt the delay time which was like a tenth of a second all right could it really be that easy i hope so i asked you how many mistakes did i make are you guys screaming do i have a worldwide audience screaming at rate me right now for all the mistakes you saw that i didn't see okay let's come up here i need everyone i do need everyone to hold their breath okay i do need everyone to hold their breath and we must get the board perfectly straight and we're going to hold our breath boom look at that okay now what you see is there at the very left it is sort of jumping between a zero value and a one value because sometimes when the potentiometer is all the way to the left it doesn't quite exactly read zero and so we can kind of understand that that's what's going on and now what we're going to do is we're going to start turning this up and this should should start getting brighter so hold your breath boom look at that shazam who's the magic man i'm your magic man look at that now we're going to go all the way up to 255 okay all the way up to 255 and you guys it kind of blows the camera out so you can't see it perfectly but in live in real life i can see very clearly that that's full brightness and now as i come down i see it very smoothly smoothly going back to zero okay going back all the way to zero there okay guys man i hope that worked for you i think that that was a fun little homework and i think it was a quick little homework and i'm hoping you guys really enjoyed it i kind of had fun with that now never fear i'm going to give you a homework assignment and the homework assignment for next week is going to take it to the next level and what i want you to do is i want you next week instead of using the red led i want you to use the rgb led okay and i want you to have three potentiometers and i want you to mix colors using the three out the three uh potentiometers so one potentiometer will set the red strength the green strength the blue strength and you should be able to mix your colors and you should be able to make that led any color you want by mixing with the three little leds okay guys as always i want to give a shout out to all of you friends 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 take a look down in the description there is a link over to my patreon account think about hopping over there and hooking a brother up seriously guys i'm getting like a lot of acceptances associated with my equipment and my gear and my studio and my software and all of that and so i really appreciate that patreon help to kind of keep everything operating at peak performance as far as my studio and my gear and my hardware goes also you can help me by giving me a thumbs up that helps me with the old youtube juice if you haven't already subscribe to the channel when you subscribe make sure that you ring that bell so you'll get future notification when the next classes are coming out and as always share this video with your friends and family share it on social media because the world needs more people doing engineering and coding and fewer people sitting around watching silly cat videos paul mcquarter resist the metaverse from toptechboy.com i will talk to you guys later [Music]
Info
Channel: Paul McWhorter
Views: 8,183
Rating: undefined out of 5
Keywords:
Id: WLWYM2MIZqk
Channel Id: undefined
Length: 22min 4sec (1324 seconds)
Published: Thu Jun 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.