Arduino Uno R4 WiFi LESSON 13: Dimmable LED Controlled by a Potentiometer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy do.com and we're here today with episode number 13 in our incredible new tutorial Series where you're learning how to think like an engineer using the Arduino Uno R4 WiFi 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 po p 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 sunfounder elite Explorer kit 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 you can hop on over there there and pick your kid up and believe me your life and my life are going to be a whole lot easier 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 12 and that homework assignment was to create a dimmable LED where you controlled the brightness of the LED using the potentiometer now before going on I must ask the question were you successful if you were successful in the homework leave the comment down below I Am Legend double chest bump if you were not successful leave a comment down below I fold it up like a cheap Walmart lawn chair okay hopefully most of you guys got this because it was really just putting together things that I'd already t T you I taught you how to take readings from the potentiometer I taught you how to take the readings you were getting to the readings that you wanted from the potentiometer I taught you how to dim an LED using the analog right command which simulates uh which uses pulse width modulation to simulate analog signals and you just kind of had to bring together all that stuff that I taught you in the last 12 lessons to do the homework assignment but never fear I'm going to do it and you can also look at my solution because I might have taken it a little bit further than what you did in your homework solution so be sure to watch this because maybe you're going to learn a couple of things even if you were successful in the homework sound good I hope it does so let me get out of your way and take a sip of coffee yes a little mid video coffee sip here then let's come over here and let's get our circuit put together so to do this this you're going to need to get in your sunfounder kit you're going to need to get your potentiometer a red LED red not blue blue LED or only for special occasions today we'll be using the red one and then you're going to need the 1000 ohm current limiting resistors and then you're going to need some wires okay so let's see if we can jump in and get this thing hooked up first thing I want you to see is plug your potentiometer in the same way I am do you see how bad Center leg I've got the center leg over there going towards the left going back towards the Arduino right you see that now we're going to plug this in and each one of the three legs is in its own column it's in its own column okay if you plugged it in like this you would have the two outside legs in the same column and it wouldn't work now we got to wire this thing up so we're going to come to the top leg we're going to come to the top leg of the potentiometer so this red wire goes in the same column as the top wire of the potentiometer and then we're going to bring that over to 5 volts okay so let's just peek there yeah you see I've got it plugged into 5 volts now the center pin that Center leg we are going to read from that leg so we're going to come to our analog in pins and I think I will use analog in pin A2 okay that looks good now we got to bring it on Home with the ground wire so we are going to come to the bottom pin the bottom pin of our potentiometer and we're going to bring that over to gnd d a convenient gnd like that I'm going to scoot it over to that closer one you can use any one but I'm just trying to keep things neat you see that 5 volts in ground and then into A2 now we're ready to hook up the LED so I'm going to come in and in this case the long leg is going to go up and so the long leg is going to go up and the short leg is going to go down now I'm going to come to the short leg and in that same column as the short leg I'm going to plug in the resistor and then the other leg of the resistor just comes down here do you guys see that I like to trim the legs off of my resistor so they they fit nice and snug and neat down uh into my breadboard you see I don't like those long I don't like those long legs so I trim them off now let's get this hooked up so I'm going to come with my red wire and I'm going to go to the same column I'm going to go to the same column as the long leg of the LED so you see long leg of the LED is up that same column that same column I put this red wire and that I'm going to take over to our friend pin 10 okay now why pin 10 because it's one of the squigglies you could use nine you could use 10 11 but because we're going to be doing analog wres we have to have the squiggly we have to use a pen that has the squiggly by now finally what we're going to do is we're going to bring things on home by coming to that other leg of the resistor and we're going to bring that back to a ground pin okay now I didn't do it here but what I could have done is I could have brought one ground pin over and created a ground rail by plugging it in here on this uh on this bottom row and then anytime I wanted a ground I could just hop over to that but with only two I thought ah okay that is not really needed so uh let's see that looks pretty good I think it looks like I already had a program running there so uh let me see if I can start let me see if I can start this thing it bugs me when it starts out on but I must have already had something running in there and that's why it is doing what it is let's see okay so I'm going to ignore that the LED is on what we're going to do over here is let's come over to our code View and what we are going to do is we are that is not a good one let's come to this one okay so you can see the circuit and you can see the code View and so what I need you to do is open up your IDE and then after opening up your IDE what I need you to do is open up bare minimum so you'll come and you will do an open or you will go to examples you will go to basic and then you will go to bare minimum you'll open that then we're going to make sure that we have the right Port so you can see that I I am on Comm 5 because that's where it says the Arduino Uno R4 is you might not be Comm 5 but you select whatever Comm your Arduino shows up on and then I'm going to make sure my board is under the uh board it is an Arduino Uno R4 Wi-Fi so I've got the right board connected I've got the right Port connected I've got my circuit connected I'm going to go ahead and just run bare minimum because that should shut the LED off because it shouldn't be uh there okay that's good it just bug me when you have some random program running from earlier and then it's making your LED do something and so that is a little bit uh that is a little bit awkward okay so now we are going to come in and we are going to start coding well let's start with the LED we're going to turn the LED on and off so I need to give it a pen I'll say red pin like that and that was check to make sure 10 yeah we were on red pin was on uh GPI or input output pin 10 so that looks good but what do we need to do we need to tell it it is what kind of number it is an INT okay now we also are going to have an INT and now we have the potentiometer and we have that pen that we're reading the potentiometer from so I'm going to call that the pot pen and that pot pen is A2 and A2 is an integer in heximal just so you know let's not forget are semicolons now what am I going to do I'm going to be reading something from the potentiometer so I'm going to need a variable for that to go into so I'm going to call that int I'm going to call that pot Val I'm not going to assign a number to pot Val I'm just declaring the variable so that Arduino will recognize it when I use it and know that it's an integer so integer pot Val and now I'm going to take that pot Val and I'm going to turn it into a right Val that I will then then apply to the LED so I'm going to need a variable what WR Val now you can use whatever variable names that you want but do you see what I'm doing I'm making them very distri descriptive and I'm trying to give them consistent naming conventions so red pin uppercase P pot pen uppercase pin uppercase P that is called bumpy fonts where the first letter of the subsequent words or uppercase and it makes the variable name more readable what else am I doing I am trying to make it intuitive like if I look at pot Val that sounds like the value you're reading from the potentiometer right Val seems like the the value that you would be writing to a different pen so try to be consistent and try to be descriptive in your use of variables okay I hope you are so now we got all those set up now we're going to want to put a delay so this thing doesn't run too quickly so I'm going to make a variable called weight t for weight time and let's make that let's say 100 millisecond so it'll be making readings and writings 10 times a second okay we are moving right along here now also I want to kind of see where I am and so I'm going to want to be printing out those pot vs and write vs and things like that and so I'm going to turn on the serial Monitor and I will do that by uh doing serial. begin serial. begin and I want to put in 9600 no no I don't use numbers I use what variable so I'll call that BR for B rate if I'm going to use it here I better come back up here and I better declare it so I'm going to say int BR is equal to 9600 okay like to set all of our variables up up top and then when we need to change something we just change the values associated with them we don't have to go through searching through our program everywhere that we've used that so now we've got the serial monitor fired up and now what we're going to want to do is we are going to want to do our pin modes well pin mode which pin let's do the pot pin okay and the pot pin is what I'm reading from it so it is going to be an input like that okay now I'm also going to have a pin mode on the what red pen what am I doing I am writing to I am writing to the red pen so it becomes a what it becomes an output like that don't forget the semicolon okay so I've got the serial monitor turn on I've got my pin modes done that has gone pretty quickly okay now what I want to do is I want to read the value from the potentiometer so I am going to come down here and then what am I going to do I'm going to say that my pot Val the value I'm reading from the potentiometer is equal to analog analog read of where where am I reading pot pin like that now I want to take that and as that number changes I want to apply a right Val to I want to app apply a right Val to the LED what is my problem the pot Val is an analog read and an analog read is a 10bit number it's 10 switches it's 10 zeros and ones which means the pot Val goes from Z to 1023 but what's the problem the right Val that I want needs to go from zero to 255 so I can't just write potv because pot Val goes to 255 and then it keeps going to 1023 so what do I have to do I have to do some math I have to take the numbers that I want and change them in I have to take the numbers that I have and I've got to change them into the numbers that I want take what I have and change it into what I want so let's come over here to the sketch pad and let's do a little map what do I have pot valves pot valves go from 0 to 1023 what do I want I want right valves and I want the right valves to go from 0 to 255 so we've got to create two points and then we can create a line so let's think what is our first point our first point is if I have a pot Val of zero that's what I have what do I want I want a right Val of zero also why because if the pot Val is zero I want the right Val to be zero because I want the thing to be off so 0 0 that makes sense but now the second point is what if I have a pot Val of full scale which is 1023 I want that to become what full scale for the right values which would be what 250 5 5 now I have two points what can I create a line and that will allow me to calculate all the in between right vals for all the potential pot vals hopefully that makes sense so what do we do we first calculate the slope m is equal to Y 2us y1 over X2 - X1 okay okay what is Y2 that's 255 minus what is y1 0 divided by X2 which is 1023 minus X1 which is zero so the slope m is equal to Simply 255 / 10 23 now I need the equation of the line how do I do that y - y1 is equal to M on to x - X1 okay and now what is y1 it's Zer so y - 0 is equal to M which is 255 ided by 1023 * x - X1 which is zero so we end up with simply Y is equal to 255 ided by 1023 * X okay now we've got Y and X equation of a line but now we need to put it in terms of our variables what is our independent variable what is our X our X is what we're reading our X is pot Val okay our X is pot Val all right and then what is our y our Y is what we want which is right Val and so what the equation becomes is Right Val is equal to 255 / 10 23 times our pot Val okay and this is the equation that we are going to want to program so we take our po Val we generate our our right Val and then we're going to write the right valve to the LED okay so let's come over here and let me switch over to this View and let me have a little sip of coffee okay now that's good so now what do we have here I have pot Val but what do I want I want right Val and that is equal to according to our formula open parenthesis it is 255 divided by 1023 okay and then close the parentheses and then multiply that by what pot Val like that okay so now let's go ahead and let's apply let's apply that right Val to the LED and I do that with an analog write where am I going to write it I am going to write it to pen 10 no no to what red pen like that okay don't forget your semicolon and so now that should take the right Val and it should control the brightness of the LED so let's go ahead and run this thing ah what was that we got a me an error message analog right red pin did I why did that not like that analog right red pin that sure looks right and I did write val ah ah ah I got to give it a value right so I want to analog write to red pin and what do I analog write the value write Val I got confused because it's only when you do a read it's only one value where you're reading from but if you're going to write you got to say where you're going to write and then you got to say what you're going to write okay so now let's try this again momentary glitch in The Matrix there done compiling no errors okay that is looking good and now that is good now we're going to come over here and we're going to start turning on our LED we're going to start twisting twisting twisting Houston we have a problem we have a problem the LED is not coming on okay now what are the possibilities the possibilities are the Arduino board is not working the possibilities are we burned out the led the possibilities are that we didn't hook it up right there's all types of possibilities other possibility is maybe we didn't program it right so I've got to break in now the first thing I would really do is I would check my circuit but as I check my circuit it looks pretty good so what you want to see is when you're troubleshooting who is your friend Mr serial print Ln is your friend and so first of all am I really reading pot vals maybe I'm not reading pot vs if I am am I really getting good right vals so I'm going to do a what serial.println what am I going to yeah I'm going to print first of all I am going to print pot Val like that and I won't go to the next line just to print and then I will do a serial. print Ln and here what I'm going to print is WR Val so if I look at those two things if they're if they're wrong then I've got some coding problem if they're right uh if yeah if they're if they're wrong I've got a coding problem if they're right they're generating the right numbers then I probably have a circuit problem okay so let's look at pot Val and WR Val what do we expect pot Val should go from 0 to 1023 and right Val should go from 0 to 255 so let's run this and then let's see what's happening as those things are Printing and maybe figure out what's going on okay so now let's come up here and see if we can get our serial monitor going uh 1023 and Zer ah you know what I'm going to have to do that is too hard to read and so what I am going to have to do is between those two between those two prints I've got to put a space so I'm going to add a serial. print like that serial. print what am I going to print I'm going to print a string with like four spaces okay do that and now I will run it again now I can see the two numbers better all right so let's see put an e there okay now let's try it it's uploading we kill that and now we're going to come over to our serial monitor okay look now look at that it says that I am reading 1023 on the pot Val it says I'm reading 1023 on the pot Val okay and then I'm changing pot V and right V isn't changing right Val is staying at zero so what do we ask ourselves am I successfully reading from the potentiometer yes I am what do I not like I don't like that when I'm going to go ahead and fix this when I'm all the way to the left pot Val is a large number and I want when I'm to the left I want it to be a small number so I turn the number up as I go from left to right I turn the number up like a volume control you turn it up well how do I fix that I'm just going to trade the red and the black wire here so now the black is going to be the top one the black is going to go to the top leg okay and the red is going to go to the bottom leg and I've always told you with this thing it's either upside down or right side up and if it doesn't go the way that you want it just change the red and the black wire now when I'm all the way to the left pot Val is zero I'm all the way to the right and pot Val is 1023 I am getting good wholesome pot values okay so where is the problem with the right val did I calculate right Val yes I did but by definition I must have an error and the error must be here okay was my math right yes 2 55 / 1023 * pot Val this looks exactly right but it is wrong now what do you guys do you say something's wrong with my Arduino or I did blah blah blah this not working not right no what is it doing it's doing exactly what we told it to okay exactly what we told it to pot Val is a what pot Val is an integer around number 0 1 2 3 4 okay 255 is a what it's an integer because there's no decimal point 1023 is a what it is an integer because there's no decimal point so if I have an integer divided by an integer multiplied by an integer what am I going to get I am going to get an integer now the number 255 integer ided 1023 is some very small number less than one but it would be like let's let's just do that on the calculator so I can be precise if I take 255 divided by 1023 I get. [Music] 249 249 is a floating Point number but because I divided an integer by an integer it's going to give me an integer and that 249 is going to go down to the next next lower round number which is zero so no matter what my pot Bal is I am always multiplying by zero and therefore down here I'm always getting zero so how do we fix that well the 255 we make it a floating number by putting a point the 1023 we make it a a floating number by putting a point now now I have a floating number divided by a floating number that will be a floating number and then when I take that and multiply it by an integer it's okay that I get an integer because that that 10 255 by 1023 is no longer going to be forced to zero why do I keep making this mistake because you guys keep making this mistake and also this is a very subtle mistake and you could have different you could have different numbers in here like if this was the control system of an airplane as long as you had something like 1,50 divided by 500 and you get two and if if if if it was supposed to be 2.3 and it rounded to two everything works the airplane flies but then one day you get to that value where you have 255 divided by 1023 and you get a zero and then the nose of the airplane goes down and crashes so you have to keep track of your variable types or you can get unexpected and sometimes catastrophic results okay enough about talking about that now let's take this and see if it works okay so we're going to leave that print statement in there okay and then we're printing zeros I'm all the way to the left so that's expected but let's come to the right a little bit boom do you see that my right Val went to five and I've got the LED coming on and as I continue to move and as I continue to move and as I continue to move the LED is getting brighter and brighter and brighter until I bring the pot Val all the way to 1023 and then the right Val is 255 boom exactly what we said the math worked I turned 2 1023 into 255 and I got a full bright LED and as I turn it down I smooth smoothly turn it down all the way to the LED being off now what is the one thing that we do not like about this so if you guys did what I just did you get an A on your homework okay but we've really got to take it a little bit further than that because if you did it this way I want you to look at the LED and as I move it here like a quarter of a turn over here on full on I don't perceive any change of brightness it's like I can go almost halfway down and I'm not seeing any perceptible change in brightness but when I come over here to zero if I move from zero just a little bit I can see it and I can see big changes down here at the low end what does that mean our eyes do not perceive brightness linearly that if I make a small change in a dim brightness I see that but if I make a small Small Change in a large brightness I don't see that so I can see very clearly going from a right value of one to two but I can't see a brightness value going from 254 to 255 so what does that mean that means that it would be better if we did not use a linear scale it would be better if we came up with a different scale so let's come back over here and let's think about this a little bit so you see with a linear scale we go something like 1 2 3 4 5 now that's fine but when we get out here and do 253 254 255 we can't see any of those changes so rather than increasing linearly what I would really like to do is I would like for each increment kind of like for each increment of the pot Val I would like to sort of go in more of a doubling I would like to go into more of like a doubling type of scheme so instead of going 1 2 3 4 I would like to go something like start at one and then double it two 4 8 16 32 64 128 and 256 okay now how many times did I double it 1 2 3 4 5 6 7 8 so I start with one and I double it eight times so I want to divide that scale up into an exponential relationship an exponential relationship well what do I mean by that well one is this one's a little confusing but 1 is 2 raised to the 0o why in any number raised to the 0o is a one don't believe me put it in your calculator okay now two raised to the one that means I just have one two it's two now what is 2 raised to the two that would be 2 * 2 is four and then 2 raised to the 3 you see how these things are mapping like this okay 2 raed to 3 is 8 2 ra to 4 is 16 2 raed to 5 is 32 how do we get that well well let's go 1 2 3 4 5 2 raised the 5 is 52 so 2 * 2 is 4 2 * 2 is 4 * 2 that's 16 * 2 is 32 you see you can work it out and it works so then 2 ra 6 is 64 2 ra to 7th is 12 28 and 2 raised to the 8 is 256 so what I want is I want to take two and I want to at a maximum raise it to an eight so when the way we would look at it is when pot Val is 10 when pot Val no this is right Val we're over here we've already uh let's see which direction do I go with that no it is pot Val okay because we're calculating right Val okay so so when we're going to calculate right Val a new way and how do we do it when pot Val is 10 23 I want right Val to be 2 to the eth okay so what do I have to have what I have to kind of think about is I've got to think about it like this okay I have to think about it like this I want right vow my right vow to be equal to 2 raised to the pot Val divided something okay because if I just took two and raised it to the two raised it to the 1023 that would be way way more than 255 okay so what do I want I want pot Val over X and what am I trying to do I'm trying to figure out what I should divide pot Val by so pot Val divided by x pot Val divided by X should be equal to 8 when pot Val is 1023 so when pot Val is 1023 and I divide that by something I should get eight well what would that something be well and you couldn't see that could you so I want 1023 divided by something to be 8 well what I need to do is solve for x so I will multiply both sides I will multiply both sides by X so 1023 is equal to 8 x so now I'll divide both sides by 8 so X is equal to 10 23 / 8 which is equal to that is equal to7 87 let me do that just to make sure here okay so I've got 1023 / 8 is equal to 12787 5 so what does that mean this number is the number that needs to go in here so now let me write that again let me write it up here and that's going to be my right Val is equal to 2 raised to the pot Val divided by 127 875 now let's talk about this because if you're not familiar if you're not familiar with exponentials this might have just confused you a little little bit but let's check it now if pot Val is 1023 if you take 1023 and you divide it by 127.85 you get 8 and 2 raised to the8 is 256 so now when I've got full pot Val I now get full right Val but now let's say that if pot Val is one if pot Val is one then the two the 1 divided 127 is going to be almost zero and then that's going to go to zero so I've created an exponential scale and I checked the two end points and it works now what is the one problem here can we write 256 no we can't we can only go to 255 so this one is one too big and then if I put in 2 to the 0o and I get a one would the LED be off no it would be barely on so what that's one too big so what I really want is I want to subtract one from all the numbers and then the first number works the last number works and everything in between works so the 256 becomes 255 and the one becomes a zero and so then what we would have is we're going to get a right Val is equal to 2 raised to the Pott Val over 127.85 and then minus1 and the minus one outside the minus one is outside of all that okay now that's going to be pretty easy to do in code that's going to be pretty easy to do in code so we need to take two and raise it to a power okay we need to take two and we need to raise it to a power so here when we're doing right Val this time we're going to do a different right Val and this one what we're going to do is on that right Val we're just going to comment it out okay so what we're going to do here is we're going to create a new right Val and what did we want we want to take two to a power how do we take a number to a power in Arduino we use the P the power function what number do we want to take to a power we want to take two to a power and then what power do we want to take it to what did we say over there it was pot Val pot Val divided by 127.85 now even though pot Val is an integer because this is a float it's going to return a floating Point number so we're not going to get that crazy turning things off business so now that is that power that'll go from 1 to 256 but we want to go from 0 to 255 so we're going to take that whole thing we're going to subtract one could this any way this could possibly work let's run it and see what happens okay looks like it's running okay it is downloading boom there it is now let's see what happens let's see what happens okay that right Val is printing at zero which I'm not exactly sure why but let's take the pot Val on on down to zero and then we're going to have to come up and come up and there it is so you see it it lingers at zero a little bit because we're in that region when we're going from zero to one on right Val and right Val won't turn the LED on until it actually gets to one and so we're coming here okay and then there it is at one and you can see that it came on and then two and then the brightness is coming up the brightness is coming up the brightness is coming up so you see how this is very smoothly now I'm about halfway now I'm more than halfway okay and now I'm all the way to 1023 and I'm at full brightness and 255 okay now if I turn it down just a little you can perceive that small change so I can perceive small changes in the knob at the high end as I'm going from Full brightness to almost full brightness because watch this I'm at 255 full brightness on right Val now I'm going to turn it down just a little bit and when I turned it down just the smallest amount I could I went from 255 down to 96 so I made a big drop with a small change and then that was something that I could perceive with my eye so that right Val now is increasing how it is increasing exponentially so I go one 48 you see how it's almost doubling every time I just move it a little bit boom you guys it's probably harder for you to see here but if you do this on your own system then you'll be able to see it and you'll be able to see try it the two different ways and see how much better this way is okay so I do need to give you a homework assignment for next week and that is if you did it linearly I want you to go back and do it the way I showed and then I want you to observe I want you to observe if you can see what I'm saying saying that small changes are seen over the entire range make sure that you post your homework to YouTube and then leave a link back to this video down in the comments put a link over to your homework assignment I'll take a look at it but what I just want to show you again man it's just this is so important I might should actually teach a lesson on exponentials right I didn't want the right Val to go linearly 1 2 3 4 5 6 7 8 9 10 up to 2 55 what I wanted is I wanted to go in factors of two from 1 2 4 8 16 32 64 128 256 and so that 128 to 256 was something that is very observable here and then of course I don't want to go to 256 and I don't want to start at one so I subtract one from both of those okay guys man I hope you are having as much fun taking these lessons as I am making them as always I want to give a big thank you to you guys who are helping me out over at patreon you guys helping me and standing with me on patreon you're the ones that keep this great content coming you can also help me by giving me a thumbs up or leaving a comment down below if you haven't already subscribe to the channel when you do ring that Bell so you'll get notifications When Future lessons drop and most importantly share this video with other people because the world needs more people thinking like an engineer and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy do.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 3,998
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: PXf51k0alGU
Channel Id: undefined
Length: 44min 40sec (2680 seconds)
Published: Thu Apr 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.