Arduino Workshop - Chapter Two - Using Analogue Pins

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] while fusing digital signals is all well and good many things in the world around us use analog information and potentiometer it's for example can be connected to act as a voltage divider which will create a voltage varying depending on where it is set it acts as a rotary control which is a really natural intuitive way to input data potentiometers are a simple way of controlling settings and adjusting menus in electronics so it's a good idea to know how to use them now what exactly do we mean when referring to an analog signal well if a digital signal is 1 and a 0 5 volts or 0 volts then an analog signal is anything and everything in between it can be a single stable voltage between your voltage limits or an ever changing analog signal such as a sine wave or an audio waveform as we mentioned previously a digital system can't read an analog signal directly so we first need to convert it into an analog signal to do this the analog capable pin reads the voltage converts it and runs it up or down to the closest increment of resolution a 10 bit analog to digital converter or ADC for short can convert an analog signal into different digital levels between 0 and 1023 10 bits gives a 1024 plus and valley a lower resolution ADC gives a rough approximation of the actual signal while a higher resolution ABC gives a more precise resolution so to understand what's going on here with these analog signals and conversion and all the rest we're going to take a look at how it actually works let's say on a graph of voltage vs. 5 volts and 0 volts now let's take another look people it could start out here and drop that way a little bit down and up and that could be you turning a potentiometer to control the voltage now what happens is that so we said that the 10 bits that's 1023 so on 1,024 possible values render between 0 0 index and 10 23 so it breaks it up into all these increments it was way more than I can draw on this graph via the idea now it would break these up and time what this signal is equal to that it's set rate which is frequency that it's sampling us so let's say here it samples it and it says yeah is that that that's ticular valid it might assign that so it's a maybe 400 so 512 is going to be halfway and we're going to get 256 there like so then when I get to here let's say it sampling [Music] here the hell up slow that was actually painful but you get the idea now when I get to this point yet still singing at about three four hundred this sample point yes in between there bit but it's closer closer to fifty six the net fan point O is equal to too much of the bottom right and so forth and the more speak Leah rises faster your change over time and then it raises up into these incremented values which a digital system can read it can break those up into a 10 bit value which is very good in the same way that we did with digital read and digital write we can use analog capable pins in the same way using analog berries and analog grow analog row is actually somewhat misleading in the sense that you would think that analog right outputs a varied voltage but it doesn't it doesn't do that at all instead what it does is it generates PWM single pulse width modulation which tends to pin on and off hundreds of thousands of times per second which turns the LED on and off faster than the human eye can see it then averages out the on time versus the off time and gives the impression of a changing brightness now not all microcontrollers do this they could have extra Hardware on it that is a digital to analog converter which converts that digital signal into a true analog voltage but most don't as usually an extra feature but most will have PWM capable pins which is technically just a varying digital signal pulse width modulation but we can use analog right to connect to the PWM Hardware capable pins and that means there's Hardware on the chip on the 80 mega three to eight which takes that function that command and turned it into the PWM signal for us which saves a lot of over in resources you could of course use the software PWM function to write in yourself with turn on the way turn off the light and so on and so forth but it's a very inefficient way of doing it and takes up that overhead resources so let's take a look at how PWM works all right again to the white board we go so this PWM business what's it all about well as I said it's a really rapidly in singles switching between our binary values of 0 or 1 5 volts 0 volts so it looks a little like if it's a square wave because the way it is rectangular but called slower this is PWM now PWM has two very important components it has the duty cycle and the frequency now the duty cycle represents one cycle which is a constant length color it makes a little bit easier to understand what we're talking about so in a duty cycle it represents one complete change so we can see here that in this duty cycle it is on for the whole time which means the LED would be on for full brightness now in this duty cycle it goes off now this is not entirely correct because it's only turning on and off very rapidly again but let's take this in a car and we'll make this whole section of duty cycle make this whole section of gtfo now what this turns into placement a little like that you can get the idea it's on behalf of the G so I can offer half the duty cycle kind of shifted there and then on for half the juice cycle and offer half the juice apple and so on and so forth and what that means is because it's switching so rapidly to the percentage of the on versus the off time let's move down here so if it was on the 75% of the time and Oscar the 25% of the time for turning back on then we would see the LED brightness to be approximately 75% if however it was then on in the next year up to 25 Senators on and off the 75% of the time go to save it to be it around 25 cents which is very cool very very cool and that's how we can use PWM I can emulate an analog voltage it doesn't quite always work that way that seems like LEDs which you like and which as the brightness of Moses which have a natural in it because of their inductance they take a while to get going they respond naturally except PWM signal which you can use to control speed brightness and things like that led is don't have a quite linear response to this PWM voltage to your scissors it's not not 100% linear with that change but it works pretty well now take your breadboard I'm going to add a potentiometer to it to the already led and button that's there and it's going to act as a voltage divider mold is dividing like we mentioned this before what is it well let's get back to the white board and take a look so voltage divider what is it well a potentially organized the way it works is it - welcome he thought about as two resistors connected like that and a potential it has three legs there's one in the zeesoc Monument Escape is a smaller one and owns more properly as a trim foot because it's small device feliner board our needs for controlling or adjusting different values in for a digital system or an analog system you can get larger ones which you can use for real-time control and penalties you would like that but they work exactly the same way here's one very little three legs so how they work well we've got one leg here call that leg one and we fold like you which we'll call tree and they're the two applet and we have a middle leg here just to called the wipe up the wiper now how it works if you can use it for a lot of different ways you can use it as a simple variable resistor to change the resistance by only connecting two of the legs up or we can connect pins 1 5 volts 10 3 0 volt and pin 2 as our output signal okay you could switch those around in 10 pin 3 5 volts within 1 0 volt the work exactly the same way then just change in the direction of rotation so we could connect this to our our pin on Green okay a zero so let's use a zero as an example ten a zero is a pin on our Arduino which is an analog capable pin capable are inputting analog signals or receiving note and then converting this connected up seeing tail ABC the analog to digital converter now it also acts as a digital pin as well but it just means that it's analog capable so a potentiometer let's say it's a 10 kilo ohm potentiometer 10 K now that means the let's turn all the way in one directions when it completely say counterclockwise one resistor is going to be 10 kilo ohms and the other one is going to be zero ohms but then when it's turned all the way the other way these resistances alternative ratios and that is been 0 ohms and that is 10 kilo ohms likewise for a linear potential you can get different responses for linear potentiometer if it's turned half way in the middle then that would be 5 kilo ohms and that would be 5 kilo ohms and they respond as such and then the outputs or g20 figures like this as a voltage divider will output according to the ratio I went half way in the middle it will give you 2 point 5 volts 2.5 volts and then all the way in one direction will give you a fireball depending on which direction and all the way on the other direction it will give you 0 volts and we can measure that voltage as all of the anti graph before 2 inputs to our Arduino so to connect up a potentiometer there's a circuit diagram example in the section of resources you can see here how it got that while officers kept the LED and the button in the circuit going to take those three things knees need to be isolated from each other so we're going to access in this direction to our breadboard take one of our trusty jumper wires and we'll connect one of the outer legs really matter which one to 5 volts but first we're going to take the 5 volt in on our Arduino and case out up five volt rail then we're going to connect the outer leg to the five volt rail the other outer leg to ground or to the zero volt rail then we'll connect the middle pin to our analog pin so we'll use a zero just all the way up here now we'll go back to the IDE and we'll first use the potentiometer to control the delay rate of the earlier blink project and then we will use it to control the LED brighter so it will build upon the code that we used earlier again if you go to the section resources you can see the source code for controlling the blink rate so take that and copy it into your Arduino IDE however I'm going to write it out so you can see the process first of all we need to remove button pins and the in its place torch Hopkins which is equal to a zero we have to use a when we're defining these pins so that it knows what we're talking about and instead of button pin we're going to be using pop pins and it's going to act as a regular input we don't need an input a pull-up resistor because it's always going to be connected to a values unlike with button now as you can see here in the source code in the section resources we're creating an integer called pot value analog Riis for the pot pin which is going to get the pin value in the same way we did with the button now bear in mind that the codes not formatted correctly here on the website we'll fix that up but we'll go back and rewrite in our Arduino IDE the first thing we're doing select that is going to create a new local variable called pot value and it's going to be equal to analog red filthy American life and I'll grade now analog reads much like the jewelry takes one argument to pin that you're reading it's going to be ringing from Plotkin now what have we got up next we're going to be using digital write to the LED high and then we're going to be using the value of what value to weight which is really cool so next we've got digital right now we're going to be writing LED pin turn to writing that high and now we need to wait so we're going to delay we're going to wait for as long as plot value tells us wait now the cool thing about that is you remember back to how the ABS they work values between 0 and 1000 23 so when it reads this it could get any value between 0 and 1000 23 which if we put it directly in our delay function means it's going to turn on and then wait anywhere between 0 milliseconds and just over one second or a thousand 23 milliseconds then what we want to do is we want to use plot value again and read our analog signal again now the reason we're doing this is because when you use the delay function in your code it can't do anything else it doesn't know that we want to use that time value for the delay now if we didn't reread this when we turn the LED off and then wait it'll mean that if we change if we change it really quickly that's going to do that a little bit anyway but we can get around it a bit by using by rereading because if we changed it really quickly it would have be able to read that new value until it does later at maximum value has two seconds which is quite a long latency in the response so we'll read it again and hopefully cut that down to a second at the most and then use digital right led can load delay watch value so with the clear hot value as a variable and the first thing we do then we've made it equal to the value of analog read and we've already declared it in void loop so we're simply going to overwrite that bell input a new value in it and I'm going to get to the top of the would your goes on so on and so forth now we could have caused recreate it but that's a really bad programming practice you're going to get some errors depending on your compiler may say already declared already defined but don't do it and I think you can do it Wars will have to don't do it and then it waits so let's take a look upload that to our board and then we should be able to control the blink rate of our LED using the potentiometer antastic so start upload so we went all the way to the right you can see how the LED is just on it looks like it's on normally and that's because it's delaying to zero milliseconds so it just looks like it's turning on on off so fast finish it on as we slow it down you can see that delay happening blink rate all the way and still one second on one second off and so on and so forth and that is the blink rate so you can see when we turn all the way there exchange it rapidly it takes a second or so to adjust and that's because of that delay between when it reads the next value so that's controlling the blink rate now let's take a look at controlling the brightness using analog right we look to a delivery now let's take a look at analog right so everything is going to remain the same as you can see this code is actually a bit simpler again so again please put value get rid of all of it going to raise the value of analog pin but what we're going to do here is we're going to use some really simple math and we're going to divide the value that we're reading by four why are we doing it well it might not be apparent yet but there's a really good reason and that is that the ADC the converter which converts the analog signal into a digital signal is 10 bit tenth update that's great these values between 0 and 1000 23 the PWM however the hardware PLM is only 8 bits which means it can only output varying varying values between 0 and 255 to 256 different possible values so it would be all well and good we turn the LR potentiometer up to a quart up and it gets 255 which it changed exactly as we expected to but then the rest of the travelers attention ometer it wouldn't register it because it's beyond the value of the PWM - right so we divided by 4 which gives it scale that now our maximum value of height value is going to be 255 or thereabouts very good now all we need to do is use analog right and again like you to write this sort of mirror images of each other analog right requires two arguments the pin that we're writing to and the value so we're going to write LED pin we're going to use plot value and that's all there is to it very very easy and it's really cool so let's give that an upload and we'll see how it works all right it's done uploading so we can see it in one direction the LED is off as we turn it on gets brighter and brighter and brighter until it's at full brightness now as we mentioned before has a slightly nonlinear response that's according to voltage but it still has the same effect for PWM so we can see this between 75% and 100% there isn't any real noticeable difference whereas between 0 and 25 cents there's a really markable difference which is cool now in the hello world example just in case you're wondering we use pin 13 for our LED now something that is important to note is that the hardware pwm pin is denoted by little squiggly lines on them you can see that pins 3 5 6 10 11 they are Hardware pwo pins which means you can use the analog write function to them and they'll work exactly as intended if for example you try to connect the led to a non Hardware PWM functional pin and try to use analog right it was simply turned off when you turn the potentiometer and then when it gets to about halfway its turn on and off because it's only capable of 1 or 0 again you can use software PWM but that's a little bit beyond the scope of this big section so hopefully learns a bit now about using analog function on the analog red and analog right we're going to wrap up this chapter by employee all of these values taking our circuit and printing it back to our computer using the serial monitor so we can find out what's going on in the brains of our microcontroller
Info
Channel: Core Electronics
Views: 54,202
Rating: undefined out of 5
Keywords: arduino adc, how to use analog pins on arduino, arduino analogue pins, arduino analog pins
Id: qS1O84HsXNQ
Channel Id: undefined
Length: 20min 19sec (1219 seconds)
Published: Tue Feb 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.