Arduino - Use Potentiometer To Control LED Brightness

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone my name is Jason I'll be showing you how to map the values taken from a potentiometer and output them to an LED like so all right so let me go ahead and tear this down and I'll show you how to build the circuit already and for this build you're going to need an LED a potentiometer I've got a 560 ohm resistor and some jumper cables and I'd also like to make a note that whenever you're messing with the Arduino and building circuits are adding any other pins make sure that you unplug the USB or any external power sources such as this little 9-volt battery it just keeps the board from getting fried alrighty to start this project I'm going to go ahead and connect the potentiometer it's got three pins that outer - they are either 5 volt or ground it doesn't matter which and the middle pin is your your data pin so that's what you're going to be reading from and connecting to your arduino analog input pin so let's go ahead and just push it into here alrighty now I've got these two power lines already so we've got your positive and your negative and like I said it doesn't matter which way I'm going to go ahead and connect this pen to the negative and this outer pen to the 5 volt positive so there we go and I've got this yellow cable and I'm going to go ahead and connect that to my analog in I'm just going to use the first one which is zero so right over here let's put that in there and then alright ok so right now we've got the potentiometer set up and let's go ahead and get the LED in there ok so we've got the LED right here and a resistor and the reason we're using the resistor is because if you were to supply it with five volts from the board it would just blow out the LED and we don't want to do that we want to keep the LED and we want to see it light up so I'd like to keep my things across the board so if I have something over here I'd like to shove it all the way over here and it's good to keep note that with all LEDs they've got a longer pen that is your positive side so just keep note of that and don't reverse it otherwise you're going to pop it all right so I'm gonna come on there we go all right that's in now I'm going to go ahead and connect this resistor in just so I don't forget and we're going to connect that to the positive side of the LED sorry if you can't see that okay and we can just shove it somewhere over here all right and then let's put our ground in just so we don't forget okay and this cable right here is going to connect to this side of the resistor and that's going to come from a pulse width modulation pen that's the little pins with the squiggles next to it now I'm going to use number three since our that's our closest one let's go ahead and connect that to our resistor and right now our circuit is ready to go so let's go ahead and hop over to the code and push it to the board and see what happens so now that we're at the computer we're going to have to write this code to drive the circuit over here now I'm going to define the pins for both the potentiometer and the LED so let's go ahead and do that LED is pin number three because remember we needed the pulse which pulse width modulation pin and that was number three and we've also got the knob oh sorry I don't like calling it potentiometer because it's just way too long for code purposes and it looks like a knob and it is a knob so we're just going to call it a knob all right so now that's done we need to run over to the set up and set the pen mode of the LED as an output and the reason being is because the it defaults as an input every pen defaults as an input by the Arduino language and you have to tell it if it's an output if you're going to actually be sending out data which is high or low or a value from zero to 255 for an LED and that's exactly what we're going to be doing okay so over at the loop we're going to create a new variable called Val and that's going to equal what the is reading so we're going to do analog read remember our knob okay and that's just taking the value that the knob is putting out and that's one to two hundred and no sorry one to 1024 and senses one to 1,024 and we're trying to send that to the LED but remember I said the LED takes 1 to 255 well we're going to go ahead and map that out and that's using a nifty little function called map and we're going to go ahead and use the value that's the data we're taking in and that defaults from 1 to 1024 remember and we need to map that out to 1 to 255 so let's go 1 to 255 so every time the potentiometer reads 1 it's going to output 2 LED 1 but if it reads out to a thousand 24 it's going to read 255 and so on so on and so forth and it meets down on the middle so 512 will map out to 122 and a half maybe I did my math right anywho let's go ahead and just do analog right and let's do the LED and use the Val we just used so what this is doing is it's outputting to the LED whatever value is being mapped out from the knob that may be a little bit confusing but just go ahead and click upload okay it's done uploading and let's go check out the Arduino and see what happens all righty and since the code uploaded it's already lit up and when I go to use the potentiometer wheel it wrong way it's seen no no it was the right way it seems to go down awesome and like I said with the potentiometer is pretty cool because you can reverse it I'm going to go ahead and disconnect it just for safety reasons we can go ahead and we can actually flip this around and it'll be reverse remember how it was bright and we turn it the other way it's going to brighten it instead of dim it alright so thank you for watching go ahead and check out my channel for any other videos if I may upload them as great doing this tutorial and I'll see you guys another time thanks for watching
Info
Channel: Jayson Herlth
Views: 91,637
Rating: 4.8382354 out of 5
Keywords: arduino, led, potentiometer, map, arduino uno, uno, mega, digitalpin, analogpin, analog, digital, leds, turn on, electronics, Light-emitting Diode (Invention), Brightness, source, code
Id: RBYVUTIU4FE
Channel Id: undefined
Length: 6min 53sec (413 seconds)
Published: Sat Aug 15 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.