Tutorial 10: Fade an LED: Arduino Course for Absolute Beginners (ReM)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome this tutorial I'm glad you're joining me in this tutorial we're gonna learn how to fade a light-emitting diode so instead of just blinking it on and off it's gonna fade in and fade out fade on and fade off over and over and over it looks really cool now okay I know that sounds pretty simple but let me tell you what you're gonna learn a lot of stuff in this tutorial first thing you're gonna learn analog right that's going to show you how to manipulate your digital pin so that you can get output that you might typically think of as analog and also we're gonna learn about the if statement which allows us to control when things happen it's extremely powerful and it's really bread-and-butter of all programming languages so lots of stuff to cover let's go ahead and dive in all you need for this tutorial is in our dueño board I use an Arduino Uno but you can use any Arduino clone or a derivative that you like however I do recommend actually sticking with the Arduino brand nose for these brand for these tutorials that we know we're on the same page you're also going to need a 220 ohm resistor that's gonna be red red brown on the markings you'll need a light emitting diode doesn't matter what color and then to attach the two together you're going to need either a saddle this breadboard or you're going to need a little alligator clip now to set up the circuit all you have to do is take either end of your resistor and put it into pin 9 on your Arduino board and then take the short end of your LED the short leg there that's the cathode and put it into ground and then connect the remaining two legs with that alligator clip or conversely you could put them into a saddle this breadboard and then you can go ahead and plug your Arduino in so go ahead and open up your Arduino IDE go to file examples basics fade alright let's start taking a look at this sketch alright so there's a couple big blocks of code the first big block of code that I want to look at are the comments now it you know it names what this function does it talks about how to sit up how to set up the circuit and then it lets us know that the code is in the public domain so we can use it how we want so there's the comments again highly recommend reading the comments every time you're jumping into a new sketch so the next block of code we come down to is where we declare and initialize variables we've got three variables here all right so we're moving up to more complicated programs usually there's just one or two now we've got three now they are all integers which you know those integers are pretty straightforward so that keeps it nice and they've got names that are very descriptive which is an awesome thing it makes me love them so the first one is led it's equal to nine so well what do we think that is well guess what it's the LED that we just hooked into pin 9 so we're referencing the pin number that we have our LED in the next one is brightness we've got set that it set equal to zero and notice how the comments on the side say this is how bright the LED is so brightness is going to track for us the value the relative value of the brightness is a brightness of the LED and then the last variable is fade amount that is set equal to five and again if we read the comments there it says how many points to fade the led by so fade amount is basically going to adjust the rate at which the LED fades on and off so there is that block of code so let's move on to the next block of code that's void setup so setup again it only runs once it's used to setup the sketch and if we look inside here there's only one statement and it's our old friend pin mode all right so if you recall pin mode takes two arguments it wants to know a pin number and then it wants to know what the mode of the pin is going to be either input or output this should be familiar to you we haven't used it in a couple tutorials recently but so just again quick it takes two variables there I'm sorry it takes two arguments the first argument is what pin number here we've got LED so that's pin nine where our LED is attached to Arduino board and then the second argument is the mode do you want it to be an input or an output well we are illuminating LED we want to apply voltage to this pin so we want that pin to be an output all right and that's all that goes on and setup so Before we jump into the loop function let's go ahead and talk about the analog write function all right so analog right is something you'll be using a lot so it's important to get to understand so first off analog right has nothing to do with the analog pins on the Arduino that was a little confusing for me at first I thought you know that's how you would write to pin your analog pins but analog right really what it does is it can it's used to invoke pulse width modulation okay so pulse width modulation which we'll talk about momentarily basically allows you to just adjust the power output of one of your digital pins so you can use analog right on pins three five six ten and eleven on most Arduino boards and again the board is marked on the digital pin that it can be used at it takes two arguments first it takes the pin number so that's the first argument you pass so any number any one of those numbers that we just talked about and then the next thing it takes is the value so what what is this value well those values either 0 or it's 255 or it's somewhere there in between and it is a representative of the amount of voltage that's going to be output at that pin so if you write 0 then ground voltage is going to be at that pin and if you write 255 5 volts it's gonna be written at that pin and if you write anywhere in between there then the amount of power output is going to be adjusted now I use these words loosely because actually what we're doing is we're changing the duty cycle at that pin so let's go ahead I've got a schematic here we'll pull this up and talk about duty cycle and pulse width modulation so what is pulse width modulation well all we do is we adjust the amount of time that five volts is being applied at the pin so whether you're going to have five volts or zero volts but we're going to adjust the frequency at which that voltage is applied so let's look at this first graph up at the very top you see we got three different graphs here so the red line represents the timing okay and for now just consider this arbitrary timing okay and the black line represents the voltage so it's a voltage step that's being applied at the pin so it's either five volts or zero volts there's no you know 3.3 volts being applied again it's at either zero or it's five so it's going to step from zero up to five and then five down to zero now if you notice the first one in a given time section there's only five volts being applied 25% of the time so that would be a twenty five percent duty cycle and an analog right so remember we said the range was zero to 255 if you divide to zero to 255 by four 25% of that would be 64 okay so if we do an analogue rate of 64 we're gonna get it roughly a twenty five percent duty cycle and you can see that 25 percent of this arbitrary time that pin is is high at five volts now what if we did an analog right at one twenty seven so now we look down at that next graph there so we can see 50% of the time five volts is going to be applied at the pin the other 50% of the time it's going to be zero so this would be a half duty cycle 50% duty cycle and then what about a seventy-five percent duty cycle well that'd be analog right 191 and then we've got 5 volts being applied 75% of the time and zero volts being applied the other 25% of the time and that in that given time period alright so if you have a zero duty cycle well then basically you're writing the pin low so you're not going to have any you're not gonna have five volts being applied at any point in time and then if you write an analog right with 255 now you're basically you're setting the pin high and it's going to be at 5 volts the entire time so by adjusting the duty cycle the on and off time of the voltage that's being applied we can adjust the power that is being seen at that pin okay so that's how it works it's pretty cool there's a lot more to it than my cheesy explanation and there will be more about to read in the further reading section but for now I think that's a good enough for us to move on and do some cool stuff so let's go ahead and jump into the loop function just as a reminder loop runs over and over again and it's really the meat and the potatoes of most of our sketches so the first statement that we come to is our new friend the analog right function and here we pass it two arguments we pass it led which is pin number nine and we also pass it the brightness variable which we set as zero so what is this telling pin nine this is saying hey we want you to have a zero duty cycle applied at pin nine which basically means there's gonna be no voltage being applied at all so the LED is going to be dark so the next statement we come to is we adjust the brightness okay so what do we have we have brightness equals brightness plus fade amount now is that a weird statement or what you know brightness equals brightness plus fade amount so what's happening here well we are taking our variable brightness and we're adjusting it basically we're saying it is equal to its current value which is zero and then we add fade amount well what was fate amount fade amount was the number five so the first time we run through the loop this is going to be zero plus five so what is the new value of brightness well it's going to be five now what about the second time we come through the loop well the second time we come through the loop brightness is set to five so then it's going to be five plus five which is fade amount and that's gonna be ten so what will it be the third time we come through the loop well brightness is now equal to ten so it's going to be 10 plus 5 which is 15 and then the next time through the loop 15 plus 5 20 20 plus 5 you kind of get the gist here all right so that is how we are going to increase the brightness in a slow fashion with our LED at pin 9 we're gonna slowly add to that fade amount okay so we're case basically gonna fade in from 0 all the way up to 255 which is the 100 percent duty cycle at our analog right what happens when we get to 255 because if we keep writing past 255 like we talked about before analog right the argument you can pass is from 0 to 255 so what happens to 255 well the short answer is we don't want to know because we want to send analog right the correct numbers we don't want to go outside of the range because you know we could get an error or it could just do something funky and we don't we don't have any idea what it would do so what are we gonna do to stop this so we need to set some type of condition in our program it basically says hey if if brightness gets to 255 let's go ahead well let's go ahead and reverse the sign on fade amount so we're going to start so once it gets super bright well let's start making it less bright let's instead of adding 5 to the brightness variable let's subtract 5 so basically all we have to do is change the sign of fade amount and now well what happens when fade gets down to 0 we don't want to pass negative numbers to analog right because again the range for analog right is from 0 to 255 so we don't want to send it like negative 5 so we'd have to set another condition that says when brightness is equal to 0 go ahead and start adding 5 again so what an easy way to do that is just to switch the sign on the fade amount variable when we get to either 255 or 0 so if we get to 255 make it a negative 5 fade amount that is and if it gets to 0 make it a positive 5 4 fade amount so how would we set a condition like that well one way to do it is to use an if statement so let's go ahead and take a look at an if statement an if statement has 3 major portions so the name of the function if IIF that's easy enough and and open and closing parentheses and this is where you put the condition and so if the condition of your if statement is met that is if it's true it's going to execute the code inside the curly brackets and the curly brackets are the last major portion of your F statement so you've got the name of the function the condition inside the parentheses and then the code that gets executed inside the curly brackets so if your condition is met it's going to execute that code if the condition is not met if it's not a true statement inside of that parentheses then nothing's going to happen the program is going to skip right over the if statement like it wasn't even there so what do we have here what is the condition that we set here well actually we have two conditions but let's look at the first one the first one is brightness equals equals zero so that equal equal sign is called a comparison operator and what it does is it asks are these two values equal so why don't we just put an equal sign well we'd be in trouble if we did that because then the program would think that we're trying to assign our variable brightness to the value zero but we're not trying to change the value of our variable brightness we're just trying to compare it to another value and that's why we have to use a comparison operator now what's our other condition well you might guess it's hey is brightness equal to 255 again we use that equal equal sign for our comparison now there's those two parallel lines in in between those two conditions and that's called a boolean operator okay and basically what it means is or so if brightness is equal to zero or if brightness is equal to 255 then do the following stuff so that's our condition if brightness is equal to zero or if brightness is equal to 255 do the following stuff okay it's not there's also other boolean operators and you can also check about the check out those in the further reading so again if these conditions are met that is if they're true statement so if brightness in fact does equal zero or if brightness does equal to 55 then the code is going to execute it well what's the code this is a clever little piece of code and what I need you to do is on the right side of that equal sign where it says fade amount instead of just seeing a negative there I want you to visualize a negative one multiplied times fade amount because what this does is it takes fade amount which in which in our case is a positive five right now and it multiplies fête amount times a negative one so what's five times negative one well it's negative five so what happens now is fade amount goes from being positive to being negative and so now if you remember that first statement on in our loop brightness is getting combined with fate amount so fate amounts negative five it's going to start subtracting from brightness so it's going to start bringing 255 down in five step increments all the way until brightness is equal to zero and then our if statement is going to get executed again because brightness is equal to zero and it's a true statement and what's gonna happen well fade amount which is now negative five it's gonna get multiplied by a negative one so what's negative one times negative five well two negatives make a positive so now feet amount is going to become positive five and now when we go through our loop that first statement is gonna start adding five to brightness so now zero you know we're brightness was at is it going to be five and then ten fifteen and it's going to keep stepping up now notice that the only time this if statement gets executed is when brightness is equal to 0 and when brightness is equal to 255 any time brightness is in between so the LED is like not often not fully on this if statement will never get run so if brightness is ten if brightness is a hundred if brights brightnesses to forty five it's never gonna get executed it only happens at those extremes at the condition that we set so that's an if statement all right so we're gonna we're going to run into lots more in if statements in this course so this is just your primer so if you don't totally get it yet don't worry we're gonna we're gonna like I said work with it a whole lot more all right so we've worked through the if statement so now what comes after that if statement well what do we do we delay for 30 milliseconds now why why such a big delay there 30 milliseconds well I guess 30 minutes Lincoln's isn't that much but usually we're used to seeing those one millisecond delays so the deal is the microcontroller is going to work really fast and if we don't delay in between each of these steps and we're not even gonna notice that it's dimming you know it's not gonna it's not going to be as apparent unless we delay a good a bit amount of time so that's why we've got the 30 millisecond delay so once that's finished we go ahead and jump right back into the loop so let's go ahead and just do a quick recap of the loop first thing we do analog right we're gonna write LED which is pin 9 we're gonna write the value the current value of brightness then we're going to go through we're gonna adjust brightness we're either going to be adding 5 or subtracting 5 based on the sign of the fate amount variable and then we're gonna check the conditions of the if statement so if brightness has gotten all the way to 255 we're going to change that fate amount sign if brightness has gotten all the way back down to 0 we're gonna change the sign of that fade amount variable if it's not if if that's not the case we completely skip it we delay our 30 milliseconds and then we go back and write the next brightness level okay so you can see it dims up it dims down it dims up it's and dims down over and over and over again so let's go ahead and verify the code and let's upload the sketch and now let's go ahead and take a look at the Arduino we should see our light dimming on and dimming off well there you have it a fading LED so we had to learn a lot to do this we had to learn about pulse width modulation using analog right and we had to learn about an if statement which is you know it's kind of a complicated matter so I'm glad you went through this tutorial thanks so much for listening and make sure you do the challenge that's where that's really gonna help sync this stuff in so until next time we're done with the basic section moving on to bigger and better things see there you you [Music]
Info
Channel: Programming Electronics Academy
Views: 181,016
Rating: 4.9361458 out of 5
Keywords: Arduino (Computing Platform), Light-emitting Diode (Invention), How-to (Media Genre), Learn, switchcase statement, Introduction, electronics, microcontrollers, arduino course for absolute beginners, learning arduino, open source hardware group, leds, pinMode, outputs, void loop, void setup, tutorial electronics, arduino tutorial, variables, programming, analogWrite, Arduino (Brand), Tutorial (Media Genre), Led, c++, C (Programming Language), computer programming, programming for beginners
Id: wpTqiEcHwwQ
Channel Id: undefined
Length: 20min 30sec (1230 seconds)
Published: Wed Oct 02 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.