Arduino Tutorial – Lesson 3.2 – Analog Values

hi and welcome back to our Arduino tutorial series for the Far Horizons program this is lesson 3.2 in less than 3.1 we looked at using digital signals remember digital is just means that it’s on or off and we read a digital signal from a push-button and we use that digital signal in order to control an LED well this is a this is useful for four simple switches when we launch our Far Horizons a balloon we are likely going to be collecting data that’s going to be require a lot more sensitivity than just on or off so we’re going to require a to read an analog signal an analog signal measures more than just on or off it measures all the values in between and so we can get some very precise data things like temperature or pressure or or light values we’ll need to will need to use analog values for that so today we’re going to instead of just using a push-button we’re going to use a potentiometer which controls a range of resistances so it’s kind of like a dimmer switch and we’re going to use that to control the output of an LED so let’s get on to the materials that you’ll need for this lesson as always we need an Arduino Uno and a USB cable for the computer we’re gonna need an LED we’re gonna be controlling that LED with our potentiometer we’ll need along with that a 220 ohm resistor as always in order to control our LED we’re going to need a potentiometer the potentiometer sometimes called a pot is just a variable resistor you can we’ve been using fixed resistors up to now but this one we have a dial that we can change and it comes in a lot of shapes or sizes we also need a breadboard and jumper wires to complete circuits and as we said these potentiometers comes and come in all shapes and sizes the ones that are showing here these are all dial type or rotary type potentiometers and they have either either wheels on them or they can you can insert knobs into them to to turn these potentiometers to change the the resistance now the way that these these work you can see that each of these ones are depicted here have three leads on them two of these leads and it’s generally the outside ones are going to be the positive and negative voltages in our case that’s going to be zero volts and 5 volts and the third one in between this one is called the wiper so we’ve seen here’s positive negative and wiper on this one positive negative and wiper on this one so what that looks like on the inside of the potentiometer is potentiometer is a variable resistor so here’s the resistive material that’s the black ring going around and to one side we head will have 5 volts hooked up to the other side will have 0 volts and when I turn this wiper in the middle what I’m effectively doing the wiper is the third wire and it’s touching the resistive material so when I turn this wiper what I’m doing is I’m effectively changing the length of this resistive material if we look at the distance between a and the wiper as I turn it to the right that would be lengthening my resistor and so it would be also changing the voltage that would be read off of the wiper as if we had 5 volts on a and 0 volts on B The Closer this was to a the closer I would be to 5 volts the closer to be the closer to 0 volts and that’s something that I can read from off of the wiper now in order to read that we know that we’ve been using digital pins which only reads 0 volts and 5 volts well we’re not going to use those instead we’re going to use an analog pin and so the analog pins are down here they’re marked a 0 through a 5 so we have 6 pins and I’ve pulled a wire out of pin 0 and we’re going to connect that that wire to the potentiometer and we’re gonna put it on the wiper the wiper for my potentiometer is right here so my potentiometer has 3 leads the wiper and then my my 2 leads which will go to my positive and negative voltages so I’ve already connected I’ve already connected my 5 volts and ground to the to the rails and so I’m gonna go ahead and connect those to the other two the two sides of my potentiometer so let’s let’s go ahead I’m gonna put five volts on the left-hand side and I’m gonna make that red because we’re talking about the positive voltage and on the other side I’m going to use black to connect to the

to the ground rail and the wiper of course goes back to my analog zero pin okay now for the for the other the other wiring I need to do I’m I’m going to use this potentiometer to control something I’m going to control an LED and so we’re gonna hook up the LED just like we have in the past we just stick it into the board we have the positive and negative sides the positive side I’m going to I’m going to connect to my pin and it doesn’t matter which pin you use but it’s got to be one of these one of the ones with these little Tilda’s next to them little wavy cent symbols and that would be for this for this Arduino would be three five six looks like 10 and 11 what these are these are ones that use pulse width modulation which is what we’re going to use to control the brightness of our LED and so it’s important that you hook up your LED to one of those one of those pins this will not work on the others as as they just don’t they won’t use that and there’s actually you can see right here where it says PWM is the little tilde pulse width modulation okay and so now I have it hooked up to I poked mine up to pen 3 and it’s hooked up to my positive side so when this when a voltage comes out of pin 3 it will complete the circuit through the LED through my current limiting resistor back to ground so there’s there’s the complete circuit okay so our wiring is is complete let’s go ahead and start our sketch so we can control this LED ok we’re going to write an analog read program this program is going to read analog values off the potentiometer we’re gonna use those later to control the brightness of an LED but for right now we’re just gonna we’re just going to read the analog values we get a sense of how this works so first thing we’re going to do is we’re going to declare an integer for that potentiometer pin that’s the wiper pin so we’re gonna name an integer I’m gonna call it pop pin and we’re gonna set that equal to believe that was on analog zero was the was the wiper pin and so I put a zero you could also put just zero and it would still work but I like to put a zero to specify that it’s a that it’s the analog value and I need to declare an integer for the LED pin although we’re not gonna use it just yet let’s let’s go ahead and put this in there my LED pin I put that on pin three remember that was a pulse width modulation or LED pin on I’m gonna put that on PWM three so pulse width modulation is required for for this pin later and we need an integer to store the value for the pot pin so we’re gonna we’re gonna use a val again for value and we’ll start with it equal to zero and now we have an integer that we can use to track the value of the potentiometer so now I’m gonna go down to the set up and we’re going to need to establish pin modes for the LED and potentiometer and so the pin mode for my let’s start with the LED pin that’s gonna be we know that’s going to be an output because we’re gonna be writing to the LED pin and we also have a pin mode for the pot pin and that’s a that’s the analog pot pin and this is going to be an input because we’re gonna be reading analog values off it okay I put in here begin serial communication we’re gonna do a little bit of tracking and troubleshooting we want to see what this what value this pop pin is actually outputting so we’re going to do a serial dot begin and we’re gonna run this at 9600 baud as always and then we’re gonna go down into my loop function so for the loop we’re gonna read the analog value off the potentiometer so we said we’re going to be using using our integer valve to track that this one right up here to store the popin value so we’re going to read the analog value of the potentiometer and store it to the the variable Val so Val is going to be equal to in our last lesson we did digital reads well now we need to do an analogue read analog read of the pop pin and so this will read an analog value something between 0 and 5 volts and it’s going to store it as Val then we’re going to serial print that value so we’re going to do a serial dot print let’s do print line so it prints out one per line and

we’re gonna print that Val and so we’ll be able to track it on the serial monitor and so it doesn’t stream the information too fast for us to read let’s put a little delay in between each reading I’m just gonna do 200 milliseconds or 2/10 of a second and so now I’m going to load that up to our Arduino and then I’m going to go to my serial monitor once that’s uploaded and there’s my serial monitor and so what we see here on the serial monitor or the values streaming along here just a bunch of zeros and so what I’m going to do is I’m going to turn turn my knob here the the the potentiometer and as I turn it we hope to see is that the the resistance will change and so will so will the value off of my analog zero and so I’ve started turning it now and we see that value start to go up as I turn it and I’m going to keep turning in it’s gone from zero all the way up to ten twenty three and it seems to stop there and that is the that is the maximum value that you can read off an analog pin so we have we have values between zero and ten twenty three so ten hundred and twenty four possible possible numbers and as I turned my potential potentiometer back down we can see it go from ten twenty three all the way back to zero so I have over a thousand a thousand values in between the zero and five volts and so we can we can have some pretty high sensitivities on the the sensors and and the analog readings that we that we take from either a potentiometer or different types of sensors so those all correspond to different voltages between between zero and five volts but it doesn’t return a voltage instead it just returns a relative value from zero to two ten twenty three okay so now we want to we want to use that use that information okay so we were just able to to read that that analog value from the potentiometer and we’re able to print it out so now we have a number that we can use so now we’re going to use that that value that we stored as valve we’re going to use that to control the brightness of my LED so this would be like a dimmer switch that you might have in your house now we I made sure that we we wanted we wanted to put that on a pulse width modulation pin and we put ours on on pin three and the reason we’re using pulse width modulation is because it allows me to control or effectively control the voltage that will be sent to the to the LED and what we have here this is just a depiction of how pulse width modulation works it works on a duty cycle and so when we when we send values to our LED we’re not we’re not actually sending a different voltage what we’re doing because that’s a digital pin all we can do is turn it on and off and so what we do is we turn it on and off for different periods of time and so what we’re seeing here is is different pulse widths and so up here this is a duty cycle of 10% so this means that the pin would only be on for 10 percent of the time so what the what the Arduino is doing is digitally riding it on and then leaving it off for 90 percent of the time then writing it back on then off then on then off so it’s only on for 10 percent of the time it’s it’s 5 volts for the rest of the 90% of the time it’s at 0 volts so what the LED effectively sees it can’t really differentiate between these pulses so what it effectively sees is well about 10 percent of 5 volts or about 0.5 volts as we increase the duty cycle as we lengthen this the time that the the pin is written on well then that’s effectively raising the voltage at 30% that would be like 1.5 volts at 50% half of 5 volts this would look the same to the LED as two and a half volts and if we went all the way up to 290 this would be like this would be the same as four and a half volts so we can control the brightness of our LED based on how long or what’s a percentage of the time we leave this pin on and this is done

internally to the to the Arduino all we have to do is use a new function and so let’s go back to our sketch and so I’ve changed one thing on the sketch here we’re not just going to read the values from the potentiometer we’re going to use them to control the brightness of our LED now and the way that we’re going to do that is we’re going to use an a function called analog right we did a digital right before where we could only write higher low because digital just means on or off now we’re going to do an analog right using the value we’re actually I’m sorry there’s two there are two parts to the analog right the first one we got to tell it which which pin it’s writing to and then we tell it how high we want this thing to write so we’re gonna want to write val in there okay and we can we can now I’m going to take this delay out because I don’t really want to delay I want instantaneous response on this and I’m going to load it up to my Arduino and we’ll take a look at how it works okay we can see my potentiometer which is on the left-hand side I’ve got my LED the red one on the right and when I turn my potentiometer I’m going from zero up to ten twenty three we don’t see what we expect we see we see it get bright and dim bright and dim multiple times each time I go from 0 to 1023 okay so we’ve got a problem here so the problem the problem stems from the types of the range of values that we’re using for the analog read and the analog right when I analog read a pin it can give me a value from 0 all the way up to 10 23 and we saw that coming off of a pot pin when I do an analogue right two to one of these digital pins well I can only use values from 0 to 255 it doesn’t quite have the resolution of the analog read so it can only do 1/4 of the of the the range of an analog read so I can’t use those I can’t use those values so what I what I have to do is I have to use a new function and this one’s called a mapping function and this one this is one that can definitely come in handy when you go to do your Far Horizons balloon launch what this uh this mapping function does and this is straight from the Arduino website it remaps a number from one range to another and we see right here we’re using map and it’s got four four inputs it’s got the value that we want to we want to map and then it’s got the the range from low to high to low to high and so what that means is is I’m going to make my value now equal to the map and I’m going to put these arguments in remember the first one is the value so so I’m going to map the value and we’re going from we’re going from 0 to 1023 and we’re going to map it to 0 to 255 because that’s the range of the analog write function this analog write function okay so what this effectively does is it changes my value so it uh it reads the value and then maps it to 0 to 255 depending on where it is from 0 to 1023 and so now when I reload this this up to my Arduino hopefully what we see is one that changes brightness based on the range of my potentiometer okay so a moment ago I had the LED flashing looked like it flashed on and off four times now you’ll see when I rotate my potentiometer from 0 to 1023 on the potentiometer it writes to the LED from 0 to 255 which is the full range and so we go from very dim all the way up to the full brightness of 5 volts and so now I’ve made a great little dimmer switch for my LED using a potentiometer and the analog read and write functions okay so I’m gonna give you a challenge now so this is challenge three challenge three is going to be to to have the same scene led the red one there with the which is controlled by the potentiometer but I have a second LED that I’ve added to it and I’ve attached it to a

different pin and this one I want to come on only when I’m at my maximum value for the for the red LED and so what that’s gonna look like is I’m going to turn my potentiometer I wanted to to go from go from all the way off right here I’ve got all the way off and now I’m gonna turn it up slowly and when I get to my maximum value for the adjustable dimming LED I want to see that second LED come on and so that’s just some extra light when I when I need it at the max value and of course when I go back the other way it would make sense that I would want that LED that blue LED there to turn off as I turn my potentiometer back down and the red one dims down to zero so so just think about what we need to we mean we need to accomplish that of course we’re going to need the analog read and write to control the red LED but the second LED is going to think about using a an if function in order to accomplish that and the the value of our analog read so good luck you