Several comments:
- Add component designators to your schematic. It is difficult to talk about the circuit otherwise.
- You appear to be a little confused about resistor dividers. You have two resistor on the input, but only one is doing anything. The resistor to ground is just a load on the input but otherwise has no bearing on your circuit. It sortof looks like the two input resistors (component designators would definitely help) are meant to be a divider that didn't get hooked up right.
- Yes, you can use opamps for gains below 1, but you might as well use resistor dividers instead. Also keep in mind that at best opamps are specified as stable for unity gain magnitude, but not below. You would have to add your own compensation for that.
- Three resistors, one from the input voltage, one to ground, and one to the supply can be adjusted such that the input voltage range maps reasonably well to the 0-5V A/D range without any active parts. You can then use the opamp as a voltage buffer because the output of the resistor divider may not have low enough impedance.
- Instead of changing gains, I would just have a few different gain setups driving different A/D pins. As long as the voltages are properly clipped, they won't hurt being out of range. The full 220V signal may be out of range for the high sensitivity A/D input, so you ignore that and only use the low sensitivity reading. When the signal level is low, the high sensitivity input won't be clipped, so you can use that. Nothing needs to change except in the firmware, which decides which input to use for the particular magnitude of input signal.
- 500 kHz is a lot for the high value resistors you show. Those should work at 50 Hz, but at 1000 times higher frequency the parasitic capacitance will be significant compared to those resistances.
Added:
If whatever software you are using doesn't allow for component designators, use something that does. At least use that when drawing the schematic for other people to see. Simulators are overrated anyway. They have their uses, but all too often they seem to make the user forget he has a brain of his own. For a trivial circuit like yours, it would take longer to enter it into a simulator than to simply think it out.
You can do a lot with a three-resistor divider as I mentioned above:
This can't always exactly fill the 0-Vdd output range with the input signal. But even when it can't, there is usually a good enough solution. Generally the more you need to attenuate, the easier it is to get the output into the desired range.
To analyze this circuit, note that by themselves R2 and R3 form a voltage divider of Vdd. This can be thought of as a voltage between Vdd and ground with a specific impedance (see Thevanin):
Where R4 = R2 // R3. How you can see we have a simple two-resistor voltage divider. The divider gain is R4/(R1+R4) and the output impedance is R1//R4. From 7th grade math we know that whatever this circuit does to the input voltage can be described by:
Vout = Vin(M) + B
You can find M and B easily enough from the above equation from any two different points. In your case, Vdd = 5V, so you want the output to be symmetric around half that, or 2.5V. So at Vin=0 you want Vout=2.5. Two other obvious known points are the peaks of the input waveform. Let's pick the negative one, so at Vin=-250 Vout=0. Now M and B can be easily solved.
If you want to find a exact solution, you can write the equations for M and B in terms of R1, R4, and V1. As long as V1 is more than 0 and less than Vdd, a exact solution is possible. From the simplified second schematic, it should be obvious that:
M = R4/(R1 + R4)
B = V1 * R1 / (R1 + R4)
Note that this system is underconstrained as there are 3 unknowns and only 2 equations. The extra degree of freedom can be expressed as the final output impedance of Vout, which is R1//R4.
You have enough here to write all the equations and solve them. That's no longer electronics but grade school arithmetic, so that's your job. Instead I'll take a less exact but more intuitive hack at it here.
Let's say you want the output impedance to not exceed 10 kΩ. We know the attenuation will be high, so R1 will be significantly larger than R4. For simplicity, let's simply make R4 = 10 kΩ. That will make the output impedance a little less than 10 kΩ. You have a 500V input range and want a 5V output range, so the divider gain should be 1/100. Again to make things simple, we'll just make R1 = 100*R4 = 1MΩ. That actually results in a gain of 1/101, but a little margin is a good idea and you'd have to get 1% resistors as it is to guarantee the gain isn't more than 1/100. So far we have:
R1 = 1 MΩ
R4 = 10 kΩ
At this high attenuation ratio, B pretty much equals V1, so let's just make V1 = 2.5V. Now we still need to get R2 and R3 from R4. From the values above, each should be 20 kΩ. However, we're making some approximations and it's good to allow for a little slop anyway, so I'd start with the next lower common value of 18 kΩ.
Now you need to plug all that in and compute the output voltage at the peaks of the input voltage, taking into account inaccuracies in the resistors. I'll leave that as a exercise to you, but the values above are either good enough or pretty close for a starting point.
Restating the underlying problem statement:
To read the current flowing through a 0.25 milliohm current shunt resistor on the high side of a battery powered circuit, with an accuracy of 0.1 Amperes.
Notes:
- Battery voltage can vary from 10.5 to 15 Volts
- Assumption: Unidirectional current measurement is required, not bidirectional
- OP would like to measure to a sub-millivolt accuracy using a 3-resistor passive configuration
For a minimum measurement unit of 0.1 Amperes, voltage across shunt = 25 microvolts. Even ignoring the common mode voltage of 10-15 Volts, a 16-bit ADC with a 5 Volt reference gives a resolution of 76.2 microvolts per count. Thus the desired resolution can not be achieved with a 16-bit ADC using passive components alone. In short, the specifications in the question are untenable unless a 24 bit ADC is predicated.
Practical solutions:
Hall effect, isolated current path, linear current sensors:
For measuring current of up to 20 or 30 Amperes full-scale, Hall Effect linear current sensors such as the Allegro ACS713 family provide unidirectional isolated high-side current measurement, with an internal shunt resistance of 1.2 milliohms along the current path. For bidirectional current sensing, the ACS712 provides the same functionality, and also has a 5 Ampere full-scale option.
For a full-scale range of 50 or 100 Amperes, the ACS756 series, similar to the ACS713, offer a 130 µΩ internal shunt resistance along the current path.
Both these current sensors provide a ratiometric output, so output voltage ranges linearly up to the IC's supply voltage (3 to 5 Volts), in the ratio of sensed current to maximum rated current (20, 30, 50 or 100 A depending on specific variant used).
Since the current sense path is electrically isolated from the rest of the IC (shown in diagram above), the sensed current is independent of the battery voltage, at least up to several kilovolts. As an added advantage, these sensors can be used on the high or low side without any changes: The output is ground-referenced with excursion up to Vcc of 3 to 5 Volts as applicable.
High-side current sense amplifier:
These are essentially specialized differential voltage op-amps designed to tolerate high common mode voltage on the sense pins, well beyond their supply rails.
For instance, the Texas Instruments LMP8480 (unidirectional sensing) and LMP8481 (bidirectional) current sense amplifiers are designed for high-side sensing with a Common Mode Voltage Range of 4.0V to 76V. They can thus be used for the stated battery with no special considerations, and without even providing a separate 5 Volt supply: They can run right from the battery supply, as shown below.
At the maximum supported fixed gain of 100 V/V, these amplifiers will provide 2.5 millivolts output for every 0.1 Ampere across the specified 0.25 milliohm shunt.
Other such high-side current sense amplifiers are available from many analog IC manufacturers. A web search will provide several options.
This then leaves the matter of digitizing the output voltage of these current sensors or amplifiers.
For the Hall Effect sensors mentioned, to get 0.1 Ampere resolution at 5 Ampere full-scale, an 8-bit (256 values) ADC will suffice: 5 / 0.1 = 50
discrete values. For 0.1 Ampere resolution at 100 Ampere full-scale, a 10 bit (1024 values) or preferably 12 bit ADC is needed: 100 / 0.1 = 1000
discrete values.
For the differential amplifiers mentioned, the ADC bit depth would be determined by the desired full-scale current value, but as a rough estimate any 12 bit ADC would suffice.
Best Answer
You are doing two things wrong as far as I can see.
Firstly, a 230VAC mains supply is a sinewave at 50 or 60Hz and has a positive peak amplitude of \$\sqrt2\$ x 230V = 325.3 volts and a negative peak amplitude of the same. This means the full peak to peak input voltage into your device is 6.5 volts peak to peak (assuming an attenuation of 100 which I think you are aiming for). I think this goes well beyond the ADC input range and may well damage your device.
The second problem is that your attenuator will have a common mode voltage of half AC voltage i.e. 115V AC. Usually, because the neutral wire is grounded, this means the AC voltage will try and lift your circuit to this voltage and (hopefully) if you have your design grounded, it will fail but in doing so you will have shorted out half the attenuation resistors meaning your attenuation is now only 50:1.
These are the main issues as I see them. Because you may not have been aware of these problems it is STRONGLY advised that you choose a safer method of extracting the AC voltage i.e. use a step down ISOLATING transformer.
AC MAINS IS QUITE OFTEN LETHAL.