Arduino Lesson 7 - Analog Write

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so here we have my basic LED circuit which is just a 220 ohm resistor and a red LED hooked up to my Arduino we've got one wire going to ground and the other wire going to one of the digital pins now some of the digital pins in the Arduino can do analog writes which is what we've got going on here so you can see that the LED is not just on or off its being faded down rather crudely at the moment but it's being faded down so what analog right allows us to do is write voltages that aren't just 0 or 5 volts so we can write voltages between 0 and 5 volts now in this case that's allowing us to fade this LED so looking at the Arduino we see here it says digital and then over here says PWM which stands for pulse width modulation and then it has this little squeal now pulse width modulation is the technology used to create analog voltages from these digital pins because these digital pins as we've said before in my digital write video can either be on or off they can't be anywhere in between so you can't just write from a digital pin 2 point 5 volts if it wasn't for this piece of technology called PWM so PWM record requires some specific setups on the pen so only some pins can do it so the ones are the squiggles next to them so 3 5 6 9 10 and 11 are the ones that can do pulse width modulation or in other words can do analog writes so we've got the other end of my LED hooked up to pin 6 because that is how we're writing the voltages that are between 0 & 5 volts in this instance I've used it to control the brightness of an LED but in reality we can use it to control a lot of things from motor speeds to the volume of a speaker so to help explain pulse width modulation I've drawn these graphs so we've got time on the bottom axis and voltage above on the y-axis so down here we've got 0 volts up here we've got 5 volts and each of these sections here count for about 20 milliseconds and the reason I've divided them up is because they are sections of a wave so that's one wave cycle that's the second wave cycle that's the third wave cycle and that continues throughout the pulse width modulation I'll explain that in a minute so with a digital write with a digital pen we know we can either turn the pen off which is zero volts or we can turn the pin on which is 5 volts we can't half turn the pen on so we can't for example write two-and-a-half folds we can either write five volts or zero volts so how do we create analog voltages ie voltages between 0 and 5 volts and we do that using a technique called pulse width modulation which is what this is about to explain so let's say we want to write two and a half volts I'm going to use blue to write the the wave cycle so what we do is for the for one wave cycle if we wanted to write two and a half volts which is half the max voltage what we do is we turn the digital pin on for half the time and then we turn it off and then we leave it off for half the time and what it does is because this is such a small period of time because this is 20 milliseconds it's so quick what happens is the average voltage the volt the voltage out is the average of the voltage over this time so we get out two and a half volts and what happens it is that wave just continues like this and we get an average voltage which is two and a half volts out so the red line here is what our component would see if it was connected to this voltage but what the digital pins actually doing is turning on and then turning off and the voltage we get out is dependent on the amount of time that the wave is on for so this is the amount of wet time that the wave is on for and this is known as the duty cycle and it's as a percentage so at the moment the wave in Indians in one wave cycle the wave is on for about 50% of the time so the duty cycle is 50% so if on the bottom graph we're going to draw another one and we're going to have a duty cycle of 25% which means the wave is only on for about 25% of the time like that and as a result the voltage level is only going to be 25% of the voltage max so we're going to get a voltage out there's 25% of five volts which is 1.25 volts and that is how analog right works so if we do an animal of right in our code we write it like this analog right open up brackets we say a pin number and then we give it a number here and this number is between 0 and 255 now the reason is 255 is because of binary if you take an 8-bit binary number and I've done a separate video on binary but I'll explain it quickly here again so we take an 8-bit binary number if they're all ones we know that the first column is a 1 seconds or two next ones are four next ones an 8 16 32 64 128 and if we add 128 64 32 16 8 4 2 & 1 then it's equal to 255 so that is why we use the value 255 in here so all between 0 and 255 so how do we know based on the value we put here what voltage we're going to get out so this is where the math comes in what we do is voltage out is equal to whatever value we write here so say we write 100 here this will be written 100 here this is how we'd work out the voltage that was being output so we'd say the value written here divided by the max value which is 255 and then we times that by the max voltage that we can output which is 5 volts for the Arduino and 100 divided by 250 5 times by 5 is 1.96 and that would be in volts so we can test this pwm theory and practice as you can see got an analogue right of 100 on pin six down here we've got our Arduino connected to a voltmeter via pin six and ground as you can see we're expecting 1.96 we've got 1.9 5 volts so we're point zero 1 of a volt out but that is pretty much there if you wanted to work out the duty cycle it's a similar calculation so for the duty cycle its equal to and again if we had 100 as the analog right value we do 100 divided by the max value which is 255 and I'll give you a decimal number so to turn that into a a percentage we times it by one hundred thirty-nine percent so the duty cycle of an analog right of 100 would be 39 percent so setting up analog rights is super easy it's mostly the same as digital rights there's only a few small differences so first let's set up our LED at the top to in LED equals six because we've got it wired to pin six remember the pin you use up here must be one of the pwm pins which are marked on the Arduino board with a squiggle those pins are 3 5 6 9 10 and 11 to that this pin here has to be one of those now we're going to set it up in the pin mode and mode LED and it's a we're writing to it so it's gonna be an output and now in the void loop so if we're going to digital right we do it like this the only thing we change with analog rights as we write analog instead of digital so we open our brackets and then we tell it which pin we're writing to same as we would with a digital right we comma and then we say with a digital right we'd say either high or low like that with an analog right we give it a value between 0 and 255 so we're gonna say it 100 and then so that I mean that's it really and if we just wanted to write one voltage level by one point nine six volts in this instance but if we wanted to fade the LED like I had at the start of the tutorial you'd put a value close to 255 to turn it all the way on so let's start with 255 then let's have a delay of 1/2 half a second and then you would analog right again so another analog right to the led okay let's take away 50 from the from the right value so now let's do 200 what will its takeaway 55 actually and then another delay and then 500 and then analog right LED take away the nother 106 to go another 50 sorry delay 8,500 and look right at D 100 and so you get the idea and what this is going to do is it's going to start full brightness there's going to be a half a second delay then it's going to fade down to four-fifths of its brightness approximately then there's going to be a half second delay it's going to fade down to 3/5 of its brightness half second delay two fifths of its brightness and if we continue going and so this analog right right at the end would be exactly the same as saying digital right LED low in the same way this analog right at the top would be exactly the same as saying digital right LED high all the analog works in between it just writing voltages between zero and five volts
Info
Channel: Benduino
Views: 34,536
Rating: 4.9536424 out of 5
Keywords: learn arduino, arduino tutorial, arduino for beginners, analog write, analog write on arduino, analog write for arduino, analogwrite, analogWrite, learn to code, analog signals, arduino lesson, beginners arduino, how to code arduino, an arduino, arduino 101
Id: JwpBn1mPGMc
Channel Id: undefined
Length: 11min 8sec (668 seconds)
Published: Fri Dec 09 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.