arduino analogwrite


We’Re gon na learn how to fade a light emitting diode, so instead of just blinking it on and off it’s gon na 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 gon na learn a lot of stuff in this tutorial. First thing: you’re gon na 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 gon na 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 dueo 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 gon na 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 gon na 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 gon na 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 gon na 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 gon na 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 gon na 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 gon na slowly add to that fade amount. Okay, so we’re case basically gon na 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 gon na 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 fte 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 gon na happen well fade amount, which is now negative. Five it’s gon na 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 gon na 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 gon na 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 gon na 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 gon na we’re gon na, 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 gon na notice that it’s dimming, you know it’s not gon na 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 gon na write LED, which is pin 9 we’re gon na write the value. The current value of brightness, then we’re going to go through we’re gon na 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 gon na 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 gon na 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.


arduino analogwrite Video







arduino analogwrite news








arduino analogwrite Social






👋 Want to bring Tony Stark-like gesture control to your projects? Learn how with the BLE-enabled MKR WiFi 1010 and Nano 33 BLE Sense boards using the ArduinoBLE library.
safe image.php?d=AQDyi1ELcW4VGzhx&w=720&h=720& dChOhNMmeF - arduino analogwrite

Arduin-yo ho ho! A fairground favorite, the pirate ship is a fun way to explore the oscillation of a pendulum. How much fun, you ask? Access our Science Kit Physics Lab preview and see for yourself:

🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today:

70537898 550174072395145 8798850142103928832 n.jpg? nc cat=102& nc oc=AQmCfMPwMf1oPpYqFhzbxfnGdbrmsjZXrEEBy1ox5Z8EJtiYMADoFNZ5mnSNRgCCY74& nc ht=scontent - arduino analogwrite



(Visited 29 times, 1 visits today)

About The Author

You might be interested in

Comment (24)

  1. Very good tutorial. A real teacher at work. Each section divided in small steps making it very easy to digest.
    Little slow for my taste some times but better slow than too fast.

  2. The instructor is probably one of the best teachers. Not only of micro controllers but in the sense of teaching as a gift. He is very intuitive and assumes nothing of the viewer. He does not come off as condescending or pretentious. He uses technical language only in instances that a new learner of micro controllers would need to know to receive a solid foundation in this field of electronics. He does not overwhelm me with too much technical jargon that might prove to confuse me more than help me. He adds DIY challenges that fit very well into his lesson and poses great insight for the viewer. His voice is confident but not alarming and he has a way about explaining arduous concepts in the most relatable fashion. However I find his most seducing characteristic is his dry humor. He adds ridiculous comments in his explanations/instructions with the utmost sincerity that I often wonder if he is being funny on purpose or just weird. Either one or both is right with me. Teacher Dude….I may not know you, or even know your name. But I love you. You make this easy, fun, and bring joy into so many lives. Thanks.

  3. Hi! What if I would use fadeAmount 6 for example? 255/6 is decimal number. Do I need > and < instead of equals? Thanks for your good video and sorry for bad language.

  4. for question 3, you don’t need to write any extra code to fade multiple LED’s at the same time. Just put an extra led in the circuit, and your good. And btw, great series 🙂

  5. Thanks Man! You have a great natural gift of teaching. This is my favorite Arduino tutorial channel!

  6. now we have ( int led = 9 ) > this pointing to the pin 9
    after that we have (int brightness = 0) > how does that pointing to the brightness not to the pin 0 ????

  7. Great videos. i’m loving following along. took CompSciJava in high school 10 yrs ago, so i generally know syntax and such, but this guy is a good educator, my teacher was a GREAT coder, and a “qualified” educator,
    either that or now i’m just soo interested in coding to control hardware, but wasn’t very fun for me to code a black and white 8bit game of pong for a couple weeks, then go home and play XBOX lol


Your email address will not be published. Required fields are marked *