In this section, we'll learn how to use the analog pins to read inputs and control outputs.

Transcript

While using digital signals is all well and good, many things in the world around us use analog information and potentiometers, 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 rounds 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 give 1024 possible values. A lower resolution ADC gives a rougher approximation of the actual signal while a higher resolution ADC 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. So let's say on a graph of voltage versus time we have 5 volts and 0 volts. Now let's take an analog signal; it could start here, meander up 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 10 bits, that's 1023 so 1024 possible values ranging between 0 0 index and 1023. It breaks it up into all of these increments; there was way more than I can draw on this graph. Do you get the idea? Now it would break this up and find what this signal is equal to at a set rate which is the frequency that it's sampling at. So let's say here it samples it and it says yeah it's at that particular value it might assign that to let's say maybe 400. So 512 is going to be halfway then we're going to get 256 there get 768 like so. Then when it gets to here let's say it's sampling this here that's a lot slower than it would actually sample but you get the idea. Now when it gets to this sample point yep still singing at about say 3 400 this sample point yeah it's in between there a bit but it's closer it's closer to 256. Then this sample point though is equal to two notches above so on and so forth and the more steeply it rises the faster you change over time. And then it breaks it up into these incremented values which a digital system can read it can break those up into a 10-bit value which is very cool.

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 read and analog write. Analog write is actually somewhat misleading in the sense that you would think that analog write outputs a varied voltage but it doesn't it doesn't do that at all. Instead what it does is it generates a PWM signal pulse width modulation which turns the pin on and off hundreds or 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 there that is a digital to analog converter which converts that digital signal into a true analog voltage but most don't it's 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 write to connect to the PWM hardware capable pins and that means there's hardware on the chip on the ATmega328 which takes that function that command and turns it into the PWM signal for us which saves a lot of overhead resources. You could of course use a software PWM function and write it in yourself with turn on, delay, turn off, delay 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 whiteboard we go. So this PWM business what's it all about? Well as we said it's a really rapidly changing signal switching between our binary values of zero or one, five volts, zero volts. So it looks a little like this. It's a square wave because the wave is rectangular but it's called square wave. So 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 Colour-based to make it a little bit easier to understand what we're talking about. All right 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 it on and off very rapidly again but let's take this in halves and we'll make this whole section a duty cycle. Make this whole section a duty cycle. Now what this turns into is the placement's a little off but you can get the idea. It's on for half of the duty cycle and off for half the duty cycle. Pretend that's shifted there then it's on for half the duty cycle and off for half the duty cycle and so on and so forth. And what that means is because it's switching so rapidly it's the percentage of the on versus the off time. Let's move down here. So if it was on for 75% of the time and off for only 25% of the time before turning back on then we would perceive the LED brightness to be approximately 75%. If however it was then on in the next duty cycle for 25% of the time and off for 75% of the time we would perceive it to be at around 25% which is very cool. Very very cool and that's how we can use PWM to emulate an analog voltage. It doesn't quite always work that way but things like LEDs which your eye can average out the brightness of, motors which have a natural, because of their inductance they take a while to get going, they respond naturally to that PWM signal which you can use to control speed and brightness and things like that. LEDs don't have a quite linear response to this PWM voltage so you'll see that it's not 100% linear with that change but it works pretty well.

Now take your breadboard and we're 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. Voltage divider you ask? We mentioned this before. What is it? Well let's get back to the whiteboard and take a look. So voltage dividers, what is it? Well a potentiometer, the way it works is it's two, what can be thought about, there's two resistors connected like that and a potentiometer has three legs. There's one in the Spark Fundamentals kit actually it's a smaller one known more properly as a trim pot because it's small designed to be on a board and used for controlling or adjusting different values either for a digital system or an analog system. You can get larger ones which you can use for real-time control on panels and things like that but they work exactly the same way. Here's one, very little, three legs. So how do they work? Well we've got one leg here, call that leg one, and we've got the bottom leg here which we'll call three and they're the two outer legs and we have a middle leg here which is two which is called the wiper. The wiper, now how this works is 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 pin one to five volts, pin three to zero volts, and pin two as our output signal. Now you could switch those around and connect pin three to five volts and pin one to zero volts it'll work exactly the same way they just change in the direction of rotation. So we could connect this to our analog pin on our Arduino, say A0. So let's use A0 as an example pin. A0 is a pin on our Arduino which is an analog capable pin capable of inputting analog signals or receiving those and then converting them. It's connected up to the internal ADC, 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. 10k. Now that means that when it's turned all the way in one direction, so when it's 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 alternate. It's a ratio and that is then zero ohms and that is 10 kilo ohms. Likewise for a linear potentiometer you can get different responses. For a linear potentiometer if it's turned halfway it's in the middle then that would be five kilo ohms and that would be five kilo ohms and they respond as such and then the output voltage when it's configured like this as a voltage divider will output according to the ratio. So when it's halfway in the middle it will give you 2.5 volts. So 2.5 volts. When it's all the way in one direction it will give you say five volts depending on which direction and all the way in the other direction it will give you zero volts and we can measure that voltage as we're looking at our graph before to import to our Arduino.

So to connect up our potentiometer there's a circuit diagram example in the section resources and you can see here how we've got that wired up so we've kept the LED and the button in the circuit. We're going to take those three pins these need to be isolated from each other so we're going to connect it in this direction to our breadboard. Take some of our trusty jumper wires and we'll connect one of the outer legs doesn't really matter which one to five volts. So first we're going to take the five volt pin on our Arduino and connect that up to the 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 and then we'll connect the middle pin to our analog pin so we'll use a zero which is 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'll use it to control the LED brightness. So we'll 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 pin and in its place put pot pin which is equal to A0 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 pot pin and it's going to act as a regular input we don't need an input pull-up resistor because it's always going to be connected to a value unlike with the button. Now as you can see here in the source code in the section resources we're creating an integer called pot value analog read 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 code's not formatted correctly here on the website we'll fix that up but we'll go back and rewrite in our Arduino IDE. So the first thing we're doing delete that is going to create a new local variable called pot value and it's going to be equal to analog read spelt the American way analog read now analog read much like digital read takes one argument the pin that you're reading it's going to be reading from pot pin. Now what have we got up next we're going to be using digital write for the LED high and then we're going to be using the value of pot value to weight which is really cool. So next we've got digital write now we're going to be writing LED pin going to be writing that high and now we need to wait so we're going to delay we're going to wait for as long as pot value tells us to wait. Now the cool thing about this is remember back to how the ADC works values between zero and 1023 so when it reads this it could get any value between zero and 1023 which if we put it directly in our delay function means it's going to turn on and then wait anywhere between zero milliseconds and just over one second or 1023 milliseconds. Then what we want to do is we want to use pot 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 same value for the delay. Now if we didn't re-read this when we turn the LED off and then wait it'll mean that if we change if we change it really quickly and it's going to do that a little bit anyway but we can get around it a bit by using by re-reading it because if we changed it really quickly it wouldn't be able to read that new value until it's delayed for at maximum value perhaps 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 going to then use digital write LED pin low delay pot value. So we've declared pot value as a variable in 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 value and put a new value in it and then when it gets to the top of the loop it'll go so on so on and so forth. Now we could of course recreate that but that's really bad programming practice you're going to get some errors depending on your compiler may say it's already declared already defined so don't do it when I said you can do it it's plausible perhaps but don't do it and then it waits so let's take a look we'll upload that to our board and then we should be able to control the blink rate of our LED using the potentiometer.

Fantastic so it's done upload so when it's 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 for zero milliseconds so it just looks like it's turning on and off so fast that it's just on as we slow it down you can see that delay happening the blink rate all the way until 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 we change 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've looked at analog read now let's take a look at analog right so everything is going to remain the same and you can see this code is actually a bit simpler again so again we'll use pot value to get rid of all of this going to read the value of the analog pin but what we're going to do here is we're going to use some really simple maths and we're going to divide the value that we're reading by four why are we doing this 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 fantastic that's great values between 0 and 1023 the PWM however the hardware plvm is only eight bits which means it can only output varying varying values between 0 and 255 so 256 different possible values so it'd be all well and good we turn the lr potentiometer up to a quarter and it gets 255 which would change exactly as we expect it to but then the rest of the travel of the potentiometer it wouldn't register it because it's beyond the value of the PWM to write so we divide it by four which gives it scales it now our maximum value of pot value is going to be 255 or thereabouts very good now all we need to do is use analog right and again like digital right they're 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 to LED pin I'm going to use pot 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 that in one direction the LED is off as we turn it on it gets brighter and brighter and brighter until it's at full brightness now as we mentioned before it has a slightly non-linear response that's according to voltage but it still has the same effect for PWM so we can see that between 75 and 100 there isn't any real noticeable difference whereas between 0 and 25 there's a really markable difference which is cool now in the hello world example just in case you're wondering we used pin 13 for our LED now something that is important to note is that the hardware PWM pins are denoted by a little squiggly line on them you can see that pins 3 5 6 10 11 they are hardware PWM pins which means you can use the analog right function to them and they'll work exactly as intended if for example you tried to connect the LED to a non-hardware PWM functional pin and tried to use analog right it would simply turn off when you can turn the potentiometer and then when it gets to about halfway you have to turn on and off because it's only capable of ones or zeros again you can use software PWM but that's a little bit beyond the scope of this section so hopefully you've learned a bit now about using analog functionality analog read and analog right and we're going to wrap up this chapter by inputting 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.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.