The analog in pin measures the voltage. That pin is very high impedance, which means that not much current should flow into or out of that pin. It looks like a very big resistor to ground.
Keep in mind that current is a measure of the amount of charge flowing past a certain point. The voltage is the amount of potential at a certain point.
You also need to know that any time you have 2 equal resistors in series, the voltage at the point in the middle of these two resistors will always be half way between the voltages at the ends. This is a consequence of the series resistance rule that say the total Resistance of resistors in Series is just the sum of the resistors. It also makes intuitive sense.
In your diagram it is not clear where the analog in pin would go. Let's say we put it here...
In this case, if only the bottom switch is closed, we can figure out what the voltage on the input pin would be by using ratios.
We have 4 equal resistors in series. The value of the Resistors doesn't matter for figuring out the voltage since they are equal.
The total voltage across all the resistors is 5 volts. That 5 volts is Diovided into 4 equal parts by the resistors, so the voltage at each place looks like this...
...and your ardunio analog input should read 1.25 volts. Try it! You can also use a multimeter to test.
Now lets see what happens when we close the 2nd switch from the bottom. Now the bottom resistor doesn't really matter because all the current will flow though the newly closed switch. Now the 5 volts are divided by 3 equal resistors. Let's see what that looks like...
Now your ardunio analog input will read about 1.66 volts.
Repeat for the 3rd switch from the bottom and you will get about 2/5 volts at the input because now the current is only flowing though 2 resistors and you are measuring in the middle of them.
Finally, when we close the top switch, now we are effectively connecting the analog in pin directly to the +5 volts at the top of the battery, so you will see about 5 volts.
Make sense?
Now lets look at how you might pick possible values for the resistors. In theory, as long as all the resistors are the same then you will get the same results. In the real world, this is limited by factors including (1) the wire between the resistors has some resistance itself, (2) the analog input actually does let some current flow into it, and (3) there is a limit to how much current that battery can supply while keeping the voltage across it 5 volts.
If you pick too high a value for the resistors, then the little bit of current flowing into the analog in pin will now actually significantly impact the voltage. The input impedance (basically the same as resistance in a case like this where the signal is DC) is for an Ardunio analog input is about 100M ohms. So, if we picked 100M ohms resistors then take the case where the top switch is closed....
Now the reading We get is 2.5 volts because the (circuit that acts like a ) resistor inside the ardunio is equal to the resistor outside.
If we go to the other extreme and pick values that are very low, now the resistance of the wires will become important. Let's say we pick 1 ohm resistors, you should be able to see now that the wires will be in the same ballpark as the intentional resistors and throw things off that way.
Another important factor is that in this circuit, the current flowing is not really doing any work - it is there just so we can detect the resistors in the path by their effect on the voltage. We want to pick resistors large enough so that no too much current will flow and drain our battery and potentially even make a fire (high current running though a low resistor makes heat).
Turns out that 10K ohms as shown is actually a good value to use. IN the worst case when the top switch is closed, we have 5 volts / 10K ohms = 0.5 milliamps which will not be much of a burden on the battery but will still be big enough that the 100m ohms on the input pin will not even be noticeable.
Not wasting power
Take a look at this circuit here...
Again, all the resistors have the same value (10K in this case) to make things easy.
When all switches are open, the Analog In pin is connected to 0 volts through a resistor and there is no connection to 5 volts, so the input will be very close to 0 volts. Also interesting is that no current will be flowing, so when no switches are closed you don't use any power.
simulate this circuit – Schematic created using CircuitLab
Now try closing switch S1. We now have a total of 4 resistors between 5 volts and 0 volts, so they are dividing the 5 volts into 4 parts - each part being 1/4 * 5 volts = 1.25 volts. So, when S1 is closed, the analog in should see 1.25 volts. Note that since the Ardunio gives you a number between 0 and 1023 representing the values between 0 and 5 volts, you should see a value of about 1023 * 1/4 = 255 in your code.
simulate this circuit
Next try closing switch S2. Now we have only 3 resistors in between 0 volts and 5 volts, so we should see about 1/3 * 5 volts ~= 1.6 volts on the analog in pin. Switch S1 and Resistor R2 now don't matter anymore. Make sense?
simulate this circuit
Keep going to switch S4 and you'll see that now the input pin is connected almost directly to the 5 volts. The 10K resistor that is also connecting the pin to 0 volts doesn't really matter anymore since the resistance of the wire going to 5 volts is almost zero. Now the analog in pin will see about 5 volts.
simulate this circuit
At first glance, it might looks like there is a short in the above case where S4 is closed, but let's take a closer look...
simulate this circuit
Remember that the analog input pin is "high impedance", which in cases like this basically means that it has very high resistance to the flow of current, on the order of 100M ohms. If we use V=IR with a V of 5 volts and resistance of 100M ohms, we get a current of 50 nano amps. A "nano" is 1 billionth, so you can see that very, very little current Actually flows though the input pin in this Case.
Yes, you certainly should. The resistor in parallel with the coil increases the loading on the circuit under test unnecessarily. The optimum value for R2, therefore, is infinity!
Assuming that this is a fixed range meter rather than a multimeter the normal solution would be:
- Measure the coil resistance and find its full-scale deflection voltage.
- Calculate the series resistance required for full-scale voltage in your application. Since \$ \frac {V_{TOTAL}}{V_{METER}} = \frac {R_{TOTAL}}{R_{METER}} \$ you can work out the series resistor.
The result will give you the minimum possible loading of the circuit under test by your meter.
simulate this circuit – Schematic created using CircuitLab
Figure 1. Basic DC multi-voltmeter. Usually a rotary selector switch would connect the red lead to the appropriate point in the resistor chain.
If you look at the specifications of any analog multimeters you will generally find that they had a resistance of 20 kΩ/V on the DC voltage range. Changing range just switched in the appropriate series resistance.
Figure 2. Simpson analog meter clearly showing 20 kΩ/V on front panel. Image source: Simpson160.com.
In contrast, most digital multimeters have a constant input impedence of 1 M on all DC voltage ranges.
Best Answer
What is your required precision/accuracy?
I will cover the foundations of a simple method, and update if necessary.
Parts:
You may already have what is necessary ;)
Lets look at your PWM outputs. Depending on the duty cycle, or how long the pulse is "high" compared to "low", an average level can be achieved.
You can keep this chart in mind as you are following along:
If at 50% duty cycle and you can somehow chop a 5V waveform to fill in the hole you will have roughly 2.5V. You can use a simple RC filter for this:
This is just a quick taste. You can learn about RC filtering to your heart's content after reading this (or before continuing.)
http://en.wikipedia.org/wiki/RC_circuit
http://en.wikipedia.org/wiki/Low-pass_filter (what we are doing here!)
Simulation:
An LTspice (free) simulation assuming 100MHz PWM just for this example with 10% duty cycle, 15.8k resistor, and 1uF capacitor:
(cutoff = \$\frac{1}{2\pi RC} =\$ 10Hz)
The graph will show:
In practise you will have defects and more variables to worry about and will be worse than this somewhat, while following the general curve.
Cleaning up the signal:
You can add more filtering stages to decrease noise, sometimes at the expense of a longer time to stabilise as capacitance increases. You should get a steady reading on your multimeter at the very least, you can even parallel with an ADC to calibrate - however note that microcontroller voltage references may not be that accurate to begin with.
An opamp in non-inverting configuration (with or without gain) can follow to do what you wish such as source current if required, for your low power tests on devices.
A DAC may be suitable if you require less noise and faster response time. A good DAC may cost you $2-3 which may be justified if an RC filter is not effective enough. You can build your own, feel free to read up on many methods to get an understanding of how they are implemented.