LESSON 11: Using Arduino to Create Dimmable LED

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number eleven on the Arduino and so if you haven't done the earlier lessons you probably want to go back and do those because each lesson will build on the earlier ones and in the last few lessons one of the things that we learned very recently how to read voltages from a potentiometer how we can come up and create a voltage divider on a potentiometer and then read the value from the potentiometer on the Arduino and then in the lesson before that we saw how we could work with an LED how we could control the LED from the Arduino and basically what we're going to be doing today is we're going to be combining those things into one project where basically what we want to do is create a dimmable LED where the brightness of this LED is set by the position of the potentiometer so if you have the potentiometer all the way to the left you want the LED to be off and if you have the potentiometer all the way to the right you want the PL IDI to be at full brightness and then you want it to scale all the values in between very smoothly this circuit should not be a surprise to you this is just basically combining two of the two of the circuits that we did and earlier in earlier projects and so let's look at this and see if we can I you know just get a quick recap you built these before but just remember when you're using an LED that the long leg of the LED always needs to be pointing towards the voltage supply remember we never use an LED without using a current limiting resistor we're using the 330 ohm resistor and then here we are using the potentiometer okay just a reminder that I'm using the components from the Sparkfun inventor kit all of these lessons are going to be using components from the spark fund inventor kit and if you look at my website under this lesson I can give you a link of where you cannot where you can pick that up or you can just go in and you can basically do this with with some of your own components but I think the spark fund inventor kit is an excellent value if you don't have one I recommend that you run out and get one okay well let's uh let's look a little bit at this circuit and see some of the stuff that we're going to have to - it's pretty easy programming there's a little bit of math we have to do because we got to remember when we do an analogue read remember we're reading voltages between zero and five volts but we don't read the number five when we do an analogue read the values that we read scale between what two values okay do you member they scale between zero and ten 23 so if we have a real-world voltage here that we are reading of five volts the number that the Arduino will read is the number 10 23 so 10 23 corresponds to 5 volts reading a value of zero corresponds to zero and they scale in between their 0 to 1023 reads means 0 to 5 volts and you can scale things with a linear equation similarly when we're doing ant writes the numbers scale between 0 and 255 so if you want to write zero you write the value 0 if you want to write 5 volts you write the number 255 if you wanted to write two and a half volts you would write 255 divided by 2 so the numbers between 0 and 5 scale to the numbers that you would write between 0 and 255 and so basically what you're going to have to do is you're going to have to take numbers that are between 0 and 10 23 that you're reading for the real world of 0 and 5 volts you're going to have to scale those two numbers between 0 and 255 so that you'll be writing numbers between 0 and 5 volts and so you got to take the numbers that you're reading 0 to 1023 and you got to map those on to the numbers 0 to 255 and we do that hopefully you would think right off the bat that that that we've done this in an earlier lesson just with some different examples but what we're going to want to do is we're going to want to use a linear equation and anytime we're going to do this just do this every time and just set up your graph because it's good practice about what you've learned in math class but basically to calculate these write values that we want to put to this led we're going to calculate the right values we've got to think about down here on the horizontal axis what numbers are we reading what we're reading numbers between zero and ten 23 those are our analog read values those are the numbers that we're reading off of the potentiometer but what we want to do is we want to write a voltage to the LED between zero and five volts which would mean we would need to analog write values between 0 and 255 and we've got to map all the numbers in between well what we need to do is to get the equation of that line we need two points well what's the first point we know will we know that if we want to write zero volts we would write 0 so 0 corresponds to 0 the point 0 0 is one point the other point is is that if we read a value at the complete other end of the scale if we read a value of 10 23 we're going to want to write a value of 255 so the second point is 10 23 255 so now I have two points and if I have two points I can write the equation of a line the first thing we need to do is we need to find the slope will the slope which we usually say is if in math class is y2 minus y1 over x2 minus x1 so I like to make things before I get positive numbers you can pick either point is x2 or you know is the second point of the first point but I'm going to say that y2 is 255 minus y1 is 0 over x2 which is 10 20 3 over X 1 which is 0 end up with y2 minus y1 over x2 minus x1 is going to be 255 minus 0 over 10 23 minus 0 and then that comes out to be 255 over 10 23 so the slope is 255 over 10 23 well the point-slope form of a line is y minus y1 is equal to M onto X minus x1 you can pick either one of these points it'll be easier if we pick this one you get the same results but it's easier to pick the 1 0 0 so I have Y minus 0 is equal to 255 over 10 23 the slope onto X zero so I just saying y minus y1 is M on to X minus x1 we calculated in we put it here okay we use the point zero zero so we end up with y minus 0 is equal to 255 over 10 23 onto X minus 0 and then that just comes out to Y is equal to 255 over 10 23 times X that's the equation of this line well now that we've done that let's go back and put what we're actually doing why was anta log write values and X was analog read values so we get that the right value that we want to write to the LED is equal to 255 divided by 10 23 times the read value so we take the read value off of the potentiometer we take the read value that we get off of the potentiometry potentiometer we multiply by 255 divided by 10 23 and that gives you the right value the value that you want to put out to pin 9 to turn the LED on and if we use that equation then we're going to smoothly transition from 0 to 2 5 volts okay hopefully you understand that if you don't go back and review some of the earlier lessons but guys on this don't just try to wing it just do the math every time because every time it's going to be a little different if you just think it's a line get your two points map it out it's always going to work for you it's a really good example of why you need to understand how to use a use a line very well you do the equation of a line very well okay so let's get back now we've sort of gone through the math and again I've got the schematic and I've got the summary of the math on our website top tech boy and we are on lesson number 11 already okay so you've got the schematic and then you come down here and you can get a little graphic of the math if you need to go back and look at it a little bit a little bit more closely but we got the math we got the circuit built I kind of went over the key points of the circuit led long leg of the LED always needs to be pointing towards the voltage supply and we're using a 330 ohm current limiting resistor okay so we should be ready now to come in and start writing some code for this thing so let's sort of kind of think about what we're going to need to do on this saw on this code well the first thing is we're going to need to use some variables for our pins and how many pins are we using we're using two pins we're using a pin that we're reading the potentiometer from and we're using a pen that we're writing to the LED so so let's let's go ahead and make variables for those pins and pins are always round numbers and so let's say that we're going to do our LED pen and you can name them whatever you want but make sure that it's something that makes sense and then also make sure that it's something that ah you know that basically is is descriptive of what you're doing so LED pin and I'm going to go ahead and set that to nine because in this circuit and in the schematic I have that LED hooked up to pen to pen nine remember you want to use one of the pins in there that have the squiggly line by it because we're an analog right you need to do one with a squiggly line by okay so we set that up and basically we're saying declare pot or declare LED pin to the arduino ten nine okay let's be good and put our comments in then we also are writing or we're also reading the potentiometer so we need to set up something I'll call it pot pin for the potentiometer pin and in the schematic and in here I have that connected to pin a 0 that is a 0 that's not an O if you put in a oh it will not work so it's integer pot pen a zero and what we're doing is making a comment ok eclair I declare the pot pin to be analog and a zero okay now what else are we going to have to do we're going to have to read a value from the potentiometer so I need a variable I need a bucket to put that number in so I need a variable or a bucket for the number that I'm going to read from the potentiometer so I'm just going to call that the read value that's what I'm going to read from the from the potentiometer I'm just declaring it I don't have to set a value because I'll go get it I'll go get that with a read later on so I just declare it I don't have to set it to a value use this variable to read podium and turn okay now I also need a variable for the value that I'm going to write to the LED so let me set that up I'm going to have a right right value and this is going to be the variable for writing to LED all right I think that we can do that because we need a we need our variables for our pins we've done that we need a variable to read into and then a variable that we're going to use for the value that we're going to write so I think that's him pretty I think that's pretty good shape there so let's go to our void setup what do we need to do here well if you look here we're not really reading anything from the serial monitor we're getting our number from the potentiometer this time and we really wouldn't have to write anything so really you wouldn't have to set up the serial monitor but setting up the serial monitor is always useful because it helps you debug things and I want to look and see what value I'm actually writing to the led and that way if there's any trouble I can I can go in and I can debug it pretty easily so I usually go ahead and turn the serial or on whether I think I'm going to need it or not so I'll turn that on at 9600 okay and then we're gonna on serial port is our comet and then what we do in the setup we need to do our pin modes okay our pin modes we're going to have two pins so we need to pin modes our pot pin is going to be a lot well we're reading from the pot pin and so the pot pin is going to be an input remember input all caps okay set pot pen to be and input okay pin mode and in our led and we are rotting to the LED pin so what should it be it should be an output remember that okay set LED and to EB and output okay now we pretty much got our I think we've got our setup Lloyd set up pretty good remember the stuff you put in void setup is the stuff you're going to do one time then we come down here and we're going to do our loop our void loop well what is it we're going to be looping through what we want to read a number from the potentiometer based on that we want to calculate a number to write to the LED based on the equation that we used and so then we want to do that and then we want to write that to the LED so there's basically basically going to be sort of three steps that we're going to want to do we're going to want to read a value from the potentiometer calculate a write value and then write that right value to the LED and so those are kind of the three things that we need to do in our void loop so we start by reading we want to read that value from the potentiometer so our variable for that we call read value and we're going to where do we get read value when we do an analog read okay remember the command analog read and if we're going to do an analog read we have to tell it where we want to read what we want to read Potkin okay because that's where the potentiometer is so we read voltage from potent Chi on the potentiometer okay now once we've read that value we now want to calculate the number that we want to write to the LED and that was all the math we did there the right value is going to be equal to 255 divided by 10 23 okay times the what 3 value okay and go back and look in the earlier video or on the website if you need to review how we got that now this is something very very important and most of the time when I come around and look at students work and it's not working it's because they did exactly what I did here 255 if you don't put a decimal point after 255 you are telling Arduino 255 the integer well 255 the integer divided by 10 23 the integer is the integer value 0 and so it's not intuitive but you have to come in and you have to tell the Arduino that these are floating-point numbers so that it will give you the end between numbers so you have to say it's 255 point as a floating point number 10 23 point that's a floating point number and that way you'll get 255 divided by 10 23 is somewhere around 1/4 as opposed to 255 the integer divided by 10 23 the integer would come out to be 0 and so if you're getting strange behavior make sure that you're going in and telling it that 255 is a float and 10 23 is a float so here we are calculating calculate right now you okay so now that's the value that we want to put out onto the LED so now what should we do we should actually write it and how do we write it you write it with an analog right and where do we write 2 we write 2 LED and that's where we're going to write and now we got to tell it what do we write what number do we want right we write the I value that we just calculated okay so what's this doing this is right to the led so we're writing our value to the LED now this if we one should work okay but just to be careful I always like to print out just so I sort of see what's going on and see see what I'm printing make sure I didn't do anything that's going to surprise me so I'm going to do a serial dot dot serial dot print and what am I going to print I'm going to print you are writing right team the value of in a space okay I did a serial print because I don't want to go to the next line because here I want to say serial dot print and this time I'll go to the next line of print line and I will say right value okay so this is all going to be on one line it's going to say you are writing the value of it's going to print that string and then on the same line because I did a print on that same line it's going to print the right value the number that I am writing to that pin I do that with a print line so it will go on to the next go on to the next value I'm I'm thinking this is going to work so let's go ahead and download this okay look at that we got a lot of green everybody is happy the program is down in there let's look at the serial monitor and if I look at the serial monitor you see it's showing me that I'm that I'm printing a value of a value of 69 and right that would be a fairly low voltage okay and so now if I turn this to the left what should happen it's the LED should get dimmer and this number here should go down and so as I turn this to the left you can see the number going down and you can see the LED getting dimmer and if I go all the way to the left you can see that it's off so when I'm all the way to the left I'm reading writing a value of 0 and that value of zero corresponds to zero being written here and the LED is off if I begin to turn the potentiometer to the right I begin to turn the potentiometer to the right I should start getting the LED coming on okay you can see there it's just barely barely on I'm just barely writing a value to it and then as I begin to turn it up it gets brighter and brighter and then when I get all the way to the right I'm writing a value of 255 which responds to five volts which puts the LED at full brightness so I can go from full brightness down to a dim value down to dim dim dim dim barely on dimming all the way off okay so we've done what we what we set out to do here and what you can see now is this circuit that's without the serial monitor it behaves without the user inputting anything it's like a little dimmer switch that as I begin to turn it up it gets brighter and as I turn it down it gets dimmer and I'm controlling the brightness of the LED from the potentiometer and to do that we've had to combine a lot of the different stuff that we've learned in all the earlier lessons and now we've got something that does something pretty cool I think okay well I've stepped you through this kind of look in little bitty baby steps but using these same concepts what's your assignment for tomorrow is is that go ahead and send in right you know right the led to full brightness so just sit there and in your code make the LED full brightness the goal is not to set the brightness now what I want you to do is to set the blink rate based on the position of the potentiometer if the potentiometer is all the way to the left I want it to blink very slowly I want it to blink on for a second off for a second on for a second off to us for a second and you have it turned all the way to the right I want it to blink really really fast so all the way to the right I want it to be on for a hundredth of a second and off for a hundredth of a second so what you have to do is you have to scale smoothly between those two values between those two endpoints that it the slowest it's on for a second off for a second that's when the LED is all the way to the left and then all the way to the right I want it on for a hundredth of a second off for a hundredth of a second so that's point oh one seconds on point oh one seconds off so what are you going to have to do you're going to have to get your chart out and you're going to have to make a chart okay and you're going to have to calculate that on time and off time by having a horizontal axis well on your horizontal axis you're still going to be going from 0 to 1023 but now on your vertical axis over here you're going to be going from point O you're going to be going from one second to 0.01 now the interesting thing is on this case your y-intercept is not going to be zero so you've got to get to be a little bit more careful in your math and in your y-intercept is not going to be zero so also you've got to make sure to be very very careful that you're doing floating point math that you're telling it that those numbers are floating points so I'm going to leave that to you for your assignment and I'll come around and look at it tomorrow if you're taking this online it's just up to you to do it on your own if you have any questions posted on my website thanks guys this has been exciting we're going to be starting to really wrap up here and do some more interesting things look forward to seeing you again on lesson number 12 very shortly take it easy to guys
Info
Channel: Paul McWhorter
Views: 142,563
Rating: 4.9116235 out of 5
Keywords: Light-emitting Diode (Invention), Led, Arduino, STEM, HIgh School Science, Engineering, Tutorial, Programming
Id: afurKLOqqSg
Channel Id: undefined
Length: 23min 9sec (1389 seconds)
Published: Wed Jul 09 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.