arduino voltage divider


Those are the pins 1 through 13. These are pins that we write voltages to. We remember that the the pins that don’t have a squiggly line. We can do digital rights to that. Would be a digital right to that pin high or to that pin low, but if we use the pins that have the squiggly line by them, we can actually write an analog voltage and we write an analog voltage by writing a value between 0 and 255. If we write the value 0 to one of those squiggly line pins, we write a 0 volts. If we write a 255, we write 5 volts, so we can write voltages between 0 to 255 on pins, 3, 5, 6, 9, 10 and 11. Along that, along that bottom row, the other pins were able to just write only able to write digital values to higher low, ok, but so far all we’ve been doing with our circuit. Well, we’ve been talking to the user over the serial port printing messages to him over the serial port and then getting input from him over the serial port. So we can interact with the user and then we can make things happen to the circuit. We can apply voltages to the circuit, we can apply digital voltages or we can apply analog voltages. So those are the things that we’ve learned so far. Now, what we’re going to do today is we’re not just going to be talking to the circuit we’re, going to be listening back from the circuit we’re going to be going out in making reads, we’re going to be reading voltages off the circuit and to see how To do this, we want to start with, like the simplest simplest example possible.

So basically, what we have here is a potentiometer if you’re not familiar with how a potentiometer works, go back and work, video go back and watch video number 9 and video number 9. We go through some of the circuit diagrams for how to how to work with a with a potentiometer and how to make a simple voltage divider being. This is just a quick schematic of the voltage divider that we’re trying to achieve the voltage in it’s. Basically, just a fixed voltage, it’s five volts the way in the circuit that we’re getting the the voltage in is we’re coming off and we’re getting 5 volts off of the Arduino from the 5 volt pen coming down and just establishing a 5 volt rail on our On our breadboard and then we bring that 5 volts and we go to the top leg of the potentiometer. So the 5 volts comes then goes to the pop top leg of the potentiometer, where we’re going to read, is and we’re going to read the voltage off. The center leg of the potentiometer, so you can see this – is the center pin on the potentiometer we’re, going to bring that up to the analog pin 0, which is right here so by coming from the center pin of the potentiometer up to a 0 it’s. Going to be that a 0 pin that we use to read a voltage off of the potentiometer and then down on the lower leg of the potentiometer, we bring that over to our ground, rail and that ground rail is our top row.

And so we bring that. Third leg up to the top row and then that top row we connect to ground on the Arduino, and so basically, what we create is a very simple voltage, divider that’s shown here and what the resistance this RP is. The resistance of the potentiometer let’s say for this it’s, a thousand ohms. Well, basically, that thousand ohms is split between r1 and r2, depending on where you have the knob set. If you have the knob set all the way to the left, all tidge appears as as r1. There is no r2 and therefore the voltage out is going to be zero it’s going to be like you’re looking at the ground. If you turn it all the way to the right for this particular particular potentiometer, it puts all the resistances R. There is no r1 V out seized the end, so it would see 5 volts. So, basically, with this circuit, our fee out is the center leg of the potentiometer. Just like you see here the center leg of the potentiometer and by hooking this circuit up like this, what you should see happen is, if you read a voltage on, pin a zero. You should be able to vary that voltage between zero all the way up to five volts by turning this little knob. Okay, if I turn it to the left, I should be getting zero volts. You know if I turn it all the way to the right.

I should be getting five volts. Okay, now, one of the things to understand when we do an analogue read it’s kind of like remember when we did the analog right, that things were scaled and so, if we wanted to write zero volts, we would write zero. We would write a value of zero if we wanted to write five volts. We would write a number of 255 to that pen, so a right value of 255 corresponding to a voltage of 5 volts, and so we had to scale things well. When we do analog readings, we have to scale things as well. Although the scale factor is a little bit different, the analog numbers, the numbers that we will read off of that analog pin, will be between 0 and 10 23. Okay, if we connect and try to read 5 volts, the read value that we will get will be 10 23. If we read zero volts, the revalue that we will get will Z will be 0. So, basically for voltages between 0 amp 5, we will get read values between 0 and 10, 23 and so you’ve got a scale that number that you read that’s between 0 to 1023 you’ve got a map it on the number 0 to 5 and we’ll go through The math on that in a minute, but for right now, let’s just see if we can make of a measurement on this voltage divider on this potentiometer and let’s, see if we can get values that are what we’re expecting let’s see.

If we can make this thing work, so in order to do that, what we need to do is we need to call up a nice new, Arduino and integrated development window, and we will start writing some code here. Well, if we’re going to talk to, pin Z a zero, if we’re going to work with, pin a zero, we probably should assign a variable to it so that we can go through and write our program based on using that variable. And so what we can do is we can set it up as an int and then what am I going to call it well it’s hooked to my potentiometer so I’m going to call it my pot pen, all right and that’s, going to be equal to a Zero, you try to display a zero all right now, the thing to understand is normally anytime. You do an integer declaration. You would just put a number in here but it’s unique, that it will recognize as integers those pin names and we are connected to an a zero this. We are ok and then what we are doing is we’re assigning a SS assigning a pot pin to a zero. We try to be good and put our. We try to be good and put our our comments in here now we’re going to we’re going to be reading from that 10, so we’re going to need a variable to put that number into. So we need to declare a variable it’s going to be an integer and we are going to declare it we’re going to call that our read value, so the value that we read from 10 8 0 we’re going to call that read value we’re, declaring the variable Here but we’re not assigning a value to it, because we’ll get the value from the pin.

We’Ll go out and we’ll read it from the pin. So we’re not assigning it here, but we are declaring our read value. Variable. Ok, now, let’s do our pin modes. Let’S come in our void setup, which pen are we going to be doing? We are going to be working with our pot. Pin, okay, that’s pin a 0, but what is it going to be always up until now, our pins have been output because we’ve been outputting to the pins. This pin we’re going to read from so this is going to be an input, so we’re going to say 10 mode, and then it is the pin pot pot pen and it is going to be an input. I think this is the first time we played an input variable also. We are going to be working over the serial port, interacting with the user, so we need to turn the serial port on so we’ll. Do our serial dot, B again and I’m, going to go to a 9600 baud this time right just make sure that whatever baud you use, it matches the bod. That said in the serial in the serial port and the serial monitor, okay, look I’ve declared my variables I’ve done my pin mode and I’ve started my serial port. So what can we do down here? Well, let’s see if we can make a reading off of that. Pin so the way to make an analog reading is you do analog, read notice the bumpy font and the R is uppercase, so analog read with our uppercase, you see it turned orange.

It says I recognize that command it’s happy and then what pin are we going to read we’re going to read pot n okay? Now that does the read that the value that we get we’ve got to put somewhere? Well, where are we going to put it we’re going to put it in our variable? Read value, so read value is going to be equal to analog, read of pot pin so read values our variable that’s, our bucket. Where do we get our number to put in that bucket? Well, we get it from over here at this analog pin. So we do an analog, read a pot pin, so I go out to a zero pot. Pin I read a value and then I stick it in my bucket in the bucket is labeled read value, so the Arduino has the value at that point, but let’s print it out, so the user can see it will do a serial dot, print line, println and We’Re going to print the value, read value that we just read in boom. Now this can go so fast. We want to slow it down, so we can see the numbers coming up so we’re going to put a delay just so that we can sort of see the numbers going by 250. Milliseconds is a quarter second and to me that’s that’s, a good value to use. Okay, so we declare our pin, we create a variable called read value. We do our pin modes, we turn our serial port on and look at.

I have not done comments. I better go back and put my comments on and geek and it’s just good practice right. Let’S let’s get in this habit, whether it just seems silly, sometimes we’re, not declare pot, pen and input and then start your serial port. Okay, read 10: read pot, pin and put value in read value: okay, so that’s the bucket we’re going to put it in we’re. Going to come here and print results to serial, monitor and then delay 250 milliseconds, all right. So what they should do is when we download it. It should just go out and start making measurements on this potentiometer and what value it will read should depend on where the little knob is set at so let’s see if this is going to work. Let’S see, if I did my code, could look at that lot of green down here. It looks like it’s, happy, everybody’s, happy so let’s turn our Syria tur on and let’s see what we get it’s. Reading zero and the reason it’s reading zero is I’m turned all the way to the left and when I’m turned all the way to the left. All of this resistance, R, P shows up is r1. This is down here and you’re, just seeing zero volts, what’s reading zero, volts and it’s reporting that as zero. What happens if I start turning this to the right, if I start turning it to the right, you see, I start seeing bigger and bigger and bigger numbers.

As I slowly turn it to the right I’m getting more and more and more resistance coming from up here down to here, so this is like not pointing here it’s pointing further and further and further towards the top 526 I’m about halfway there because remember 0 to 5 volts will read 0 to 1023, and so a value of 526 527 corresponds to about 2 and a half volts so I’m about halfway to turning it all the way to the right and so I’ll continue to turn it here. You can scroll in growing growing and then I turned it all the way to the right, and I get it all the way up to 10 23 when it’s reading 10 23 that corresponds to a voltage of 5 volts. You know everything scales from 0 to 1023 represents numbers from 0 to 5, volts, ok, so now what you can see here is is that I have a little input device and the number that I get on my serial monitor depends on where I set this potentiometer. Like I have a little knob like that, could be a volume control now I could set the brightness of the LED, based on where I put this or I could set other things based on, where I put that so now I don’t have to just get user From the input over the serial monitor, I could set things based on where I put this potentiometer. So now all the sudden it’s like a little product where I could start turning that knob and getting the LED brighter or dimmer.

I could turn the knob and that could affect how fast the LED blinks. I could set the parameters for the operation of the circuit from this from a potentiometer like this. So, even though this is real simple, this is extremely powerful, extremely powerful because it lets a device works, sort of autonomously that on the circuit, you can start getting your input directly from them directly from the circuit. Ok, let’s just take a quick look, though at what you might want to do is you might want to turn this read value like I think, in terms of 0 to 1023, so I can look at and I can just see what the voltage is, but if You were going to give it to a user. You might want to give them a number from 0 to 5 volts and so let’s see how that math works all right. Well again, we go back and we use what we learned with linear equations, let’s. Think about what you read. You read a value from 0 to 1023 and so we’re going to make this axis the horizontal axis, your independent variable, that’s, what you read: it’s your read value and then that corresponds to a real world voltage between 0 and 5 volts. And so we make that the vertical axis voltage between 0 and 5 volts corresponds to read value of 0 to 1023 let’s see if we can find two points on here, point number one: we know that a read value of zero corresponds to a voltage of zero.

So we have one point: the point: zero, zero, alright what’s. The second point we know. The second point we know up here is that a read value of 10 23 corresponds to a world voltage of 5. So we have a second point of 10235. So we have two points for this line. If we have two points, we can calculate a slope. Slope is y2 minus y1 over x2 minus x1 that’s going to be equal to let’s. Have this, as as the second point in this is the first point, makes it a little easier. I like to do my linear equations to try to avoid negative numbers, so I have 5 minus 0 over 10, 23 minus 0, so that’s Y 2, minus y1, y2. Minus y1 over x2 minus x1 and that comes out to be 5 over 10 23. Now we know the slope. The point: slope form of a line is y. Minus y1 is equal to M over m times X. Minus x1. Your point: x1. Y1. You could pick this point where you picked that point and you’ll get the same answer, but it’s a lot easier. If you pick the easier point, let’s pick this one all right now, let’s put in the values Y minus y1 is M onto X, minus X, 1 Y minus 0 right. Ok, that’s 0 is equal to 5 over 10 23, which was the slope times X. Minus x1 all right times X, minus next one, so we have 5.

Minus 0 is equal to 5 over 10 23, the slope on 2 X, 0 and that’s, because we chose this point of zero zero to make it easier. The other one would have eventually given us the same answer. So Y is equal to 5 over 10 23 times X. That is the equation of this line. Now the only thing I’m going to do now, I should switch it to our y axis Y was voltage and X was read value, so we change Y for voltage and X. We make read value, so what we see is the real world voltage is equal to 5 over 10 23 times the read value. Okay – and these are the read values that we’re getting here, and so we can use this equation that we got from using that equation of a line that you had to learn place in seventh grade and wondered when you were ever going to use it well, you’re Using it now to allow you to really create an equation that will take this read value. This read number that we’re getting that’s between 0 to 1023 and turn it into a real world voltage a real voltage that’s between 0 and 5. So let’s go ahead and see if we can do that, so we’ve got our revalue. Let’S create another variable called integer and it’s our voltage – and this is our real voltage we’re going to calculate this. So we are not going to assign a value to it.

We’Re just going to declare it ok, declare our volt voltage variable okay, now notice how didn’t it just out a habit, but I want you to think about this. Are we interested in just the voltage? 1. 2. 3. 4. 5. There could we have in between voltages? Well very much we could have in between voltages and we could have a whole lot of in between voltages. We could have 0.1 volts point to point 3. Point 4. We could have very, very, very small, increments in voltage, and so really we want the in between numbers. So we should not declare this an int. We did should declare it a float and again when we declare something as float. That means a floating point number. If we go back to math class, that would be equivalent to a real number it’s all in between numbers, and we call them floats in programming language. So we have a float variable now we’re going to calculate what is our real world voltage? Will the voltage the voltage is going to be equal to remember it’s, going to be 5 over 10 23, but now this is very, very, very important and I mean over and over. I see students forget to do this if we are declaring something to be a float and if we are using floating numbers, we have to write our numbers as floating numbers and sit so instead of saying it’s 5 over 10 23 that’s 5 over 10 23.

When our dueo sees 5, it sees it as an int and when it sees 10 23, it sees it as an int. And so, if you do, the int math integer math of 5, divided by 10 23 it’s, going to come back with a 0 because it’s going to round it to 0 or it’s, going to truncate it to 0. And so you don’t want it. Because you want a real floating point, number well, we’ve said voltages floating points, so we need to make these floating point numbers and we do that by putting a decimal. Five point tells Arduino that you want the floating point: number 5, not the integer 5 and similarly here point so when you are doing floating point math on the Arduino, make sure that you put your decimal in or you’re going to get some surprising results. That’S going to take a long time to figure it out, so what was it? Voltage is equal to 5 over 10 23 times Reed value, and so we have 5 as a float divided by 10 23 as a float and then we’re going to go times. Reed value, okay and then we’re going to do this is what is it doing? It’S calculating the real world voltage? Okay, now let’s download this and see if it’s still happy looks like it’s going to be happy see if we can get our serial monitor back up. Call that thing up all right now.

Look that does not look let’s, see let’s, try downloading it again. Ah I see what I did. I was still printing read Becky. So even though I calculated voltage, I was still uh. I was still printing read value, so that was a little bit of a shocker to me. So what we’ve got to do? Is we don’t want to print out the read value anymore? We want to plot out serial dot, LS serial dot print line, and then we want to print voltage, voltage. Okay, write out the real world voltage. Okay, this should work this time. So this time we should be getting this value soon. Somebody should have told me semicolon at the end of almost all lines: okay, so now we should be getting real world voltage isn’t this out. Okay, that looks happy. Let’S call this up. Okay, so remember how that was a read value of about five hundred and twenty something well, that corresponds to about two and a half volts, so we’re just sort of almost halfway up. So if I turn it all the way, let me get the circuit back here where you can see the circuit. I get this all in here. So if I turn this all the way to the left now you can see that I have zero volts. I can turn it just a little bit and look at that I’m. Getting about me see if I can get it okay, that’s about as close as I can get it but I’m getting about a tenth of a volt there.

So you see why we needed floats, because we need the numbers between the integers and so now I can turn it up and I can get 0.5 volts let’s like let’s see I can get about I’m trying to get about 175 it’s kind of hard to get It exactly about 7 5. I can take it up to one volt. Take it on up, keep turning to the right, keep turning turning keep turning, I get five volts. Yes, you see what I’m doing is I’ve got this potentiometer. Okay, I did the math and it was based on this voltage divider that we talked about okay let’s, see if we can get it all, showing here and I’m reading. Now an analog voltage off of that voltage, divider and I’m reading it in real volts from zero to five volts, so I’m reading, an Arduino number that’s between zero and ten twenty three and I’m, converting that to a real world voltage between zero and five volts in Cc it’s like a little volt meter that we would that we built here and the voltage corresponds to whatever voltage that this, that this pin, that this thing analog analog, pin reads: well, let’s just do an experiment! Okay, what? If I just unplug this from the potentiometer? Okay – and I put it on my ground, what should it read if I put on the ground, it should read zero volts. I didn’t put it on the ground.

If I put it here on the ground right there, it should read zero volts and it reads: zero. Volts because ground is zero volts. What if I put it on the next one down at the positive rail and when I put it in there it reads five volts. So you see all the sudden here. With this little pin, I have a little voltmeter that will read anything between zero and five volts. Okay, so I’m going to come back in here and put it on my potentiometer again. But really you would not use this as a you know, to build a voltage supply really really what you’re doing is you’re trying to read basically read and understand where the potentiometer is set to, and what you’re really going to use this, for, usually is for an Input on your circuit so like let’s, say that I could write a little program that if I have it turned to the left, the LED blinks really slow and, as I turn it to the right, the LED starts blinking, faster and faster, and so basically this is A way that you can start controlling things with knobs there’s, all types of different reasons that you would use that you would do these analog reads. Just to recap, remember: when you’re doing an analog read you have to you one of these six pins, a zero to a five. You have to use one of those if you’re going to do an analogue read these pins down here.

We’Ll, do digital writes the ones with the squiggly lines will do analog, writes and the ones that do not have the squiggly lines will just do digital rights and so squiggly line on the on this bottom set of pins will do analog, writes or digital writes the Ones without the squiggly lines will do only digital rights. Okay, so this is a powerful tool, a new tool we have now we can talk to the user over the serial monitor. We can read values from the serial monitor. We can apply voltages. Digital voltage depends. We can apply analog voltages to pins, and now we can read analog voltages, so our toolset is getting pretty pretty sophisticated and we’re going to be able to do some pretty impressive things. With these simple tools fairly quickly. Okay, again, I am Palmer quarter.


arduino voltage divider Video







arduino voltage divider news








arduino voltage divider Social






Researchers have developed a new strategy to enhance interactions between humans and swarms of drones.
safe image.php?d=AQBQrGeW0iG9Gl5e&w=720&h=720&url=fbstaging%3A%2F%2Fgraph.facebook - arduino voltage divider

For the first time ever, we’ll be participating in the
Hacktoberfest monthlong celebration of open source software.

Contribute! is a good place to start joining the fun!

safe image.php?d=AQBK8KX 5 Oc7R Q&w=720&h=720& social 744d976f227e4aff6866443abcede8c651b309ec9c7c9f7410f5944f8e1299b9 - arduino voltage divider
4aLH6 jQ normal - arduino voltage divider
Fri Sep 15 16:30:07 +0000 2006

Hey, it’s a voltage divider circuit with an Arduino!

Vv WIfsT normal - arduino voltage divider
Tue Sep 25 20:50:45 +0000 2018

RT @jsumocom: “XMotion includes one trimpot (connected to A3 pin) and one voltage divider resistor group (connected to A0 pin).” https://t.…

Measuring resistance with Arduino (voltage divider)

A basic voltage divider exercise with #Arduino



Originally posted 2016-05-29 06:09:52.

(Visited 84 times, 1 visits today)

About The Author

You might be interested in

Comment (33)

  1. Hi Paul 😀 great vids- i’m learning more from this than i did at TAFE lol- my formula is a little different i used this code:
    float j=(readValue/204.6);
    readValue=analogRead(POT); //read the POT and set the name to reaValue

  2. hy.any body guide me about tutorial to read analog value of pulse senor thorough arduino and bluetooth hc 05 and send it to laptop

    1. Because reading analog (A0,A1…A5) send info to processing units using 10 bit pipeline in decimals =2^10 =1024

      And writting (pin 1,2,…13) using pipeline 8 bit in decimals = 2^8 = 256

      One is less in both because we also count zero …

  3. hi sir i appreciated your Videos but i think most of them , you are not explain about Arduino itself but about maths, that it is not what we are looking for , what we are looking for is programming , Codes what they do, math is boring

    1. Actually the math is very important. If you do not understand the math, you will never be a successful engineer or scientist. My goal is to train people to become successful, not create ignorant coders.

    2. @Paul McWhorter Sir with all do respect when you go home and push the button to turn the lights ON or OFF in your house, you don’t care what AC volts are 110v or 220v behind the button ,what is really matter if the lights is ON or OFF, when you want to take things 1 meter far way from you i don’t need to know that 1 meter has 100 centimeters , i just want 1 meter , simplify thing it is part of life make life easy! sorry if i bother with my ignorance, just because you knowhow 2 + 2 = 4, it does not make you a genius, have a awesome day sir

    3. I think you just dont get it . . . when you turn the light switch on, you want more to happen than you are saying. When you turn the light switch on, you want the light to turn on, and you want the house to NOT burn down. If you do not do the math, you have not calculated the current carrying capacity of the wire you put in the wall, the correct breaker to use, and how many outlets can be put on the circuit. You do this job without the math, and maybe the light comes on and the house burns down. Luckily, successful, practicing engineers and electricians have better judgment than your comment reflects.

  4. I get the concept of commenting. But with this language you can make commands so obvious that there is little point. Like come on it’s not G-code.

  5. for the math i just did 1023/5= 204.6 n so readValue/204.6 = voltage. i dunno about all this line and graph stuff haha i mean like it seems unnecessary haha

    1. You have a light sensor that reads 129 in the dark and 1264 when the lights are full bright. you want to output a voltage between 2.5 and 5 volts depending on brightness between dark and full bright. You want output voltage to change smoothly between the two values based on brightness.

    2. @Paul McWhorter I know the math is still the same, but, just wondering, wouldn’t the maximum value for the light sensor be 1023? I mean at full brightness

    3. No, the sensor changes resistance based on how bright the light is . . .ends up between two values you have to measure. Might never get to 1023.

    4. @Paul McWhorter Ahhh yes, sorry, you said sensor reading and I was thinking about the maximum value for the potentiometer reading being 1023.. I see. Thank you for responding. Much appreciated

  6. Great Video – this makes me think – how can this circuit be modified to read lets say up to 50 Volts ? need some chip that takes 5 volts and can output max 50v?

    1. You really need to know what you are doing as you use higher voltages. With simple low current 5 volt inputs and outputs things are safe, and nobody gets hurt. As you deal with higher voltages and currents, make sure you know what you are doing.

  7. Sir ur lessons are Awesom…I have aQuestion related to this
    Q1: I have used 3.3V pin instead of 5 volt pin but Serial Monitor shows output voltage upto 3.5V , 3.6 V , 3.7 V , 3.8V why is it so ?

    kindly Answer Sir

  8. i can’t get this to print Strings along the Voltage Float values

    EDIT : My bad, it was the bloody cable not letting me do it.

  9. Great lesson Mr. Mcwhorter. I’ve been down this road before but have since gotten rusty…I took notes and you make it clear and concise. Thank you.

  10. Could someone help me with this problem, thanks!
    I tried out the sketch from this video with the same setup and everything worked except for the serial monitor; it gives me values between 0 and 1023 but it updates very slowly. For example, if I turn the potentiometer from one end to the other, the read values have a delay of 250ms but the values given are approximatly 30sec late than what is physically happening with the potentiometer. So I don’t really know what problem could be


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