Measuring DC Amps using ATMega16

Discussion in 'Embedded Systems and Microcontrollers' started by aurangzeb, Aug 1, 2010.

  1. aurangzeb

    Thread Starter New Member

    Aug 1, 2010
    9
    0
    Hi,

    I am a new comer to AVRs and am trying to design an Amp-Hour meter. My main constraint is that I need to measure on the High side. The second constraint is that the high side current measurement ICs are not available locally in Karachi, Pakistan.

    The current measurement is for a UPS battery (12VDC car battery). Current sensing is to be done with a shunt resistor giving 60mv/50Amps; being attached to the (+) line. Current flow is bi-directional i.e. load current and battery charging current.

    The ATMega16 was purchased in DIP package and as such the gain stages are not available. Therefore I have to resort to using off the shelf op-amps. I have researched the Net and have come up with the initial design using work by Bonnie Baker of Microchip. The attached ZIPPED Excel sheet has been developed to model the IA based on LM324. (pls disregard the Microchip IC number shown in the Excel sheet figure)

    I have the following questions about the IA configuration figuring in the Excel sheet.

    1. Will it be possible to power this IA from 12 Volt line in which the current is to be measured.

    2. Can I connect the output of the IA directly to the ADC input of the ATmega without latchup issues and so forth. The ATmega would be running from a 7805, which (the 7805) would also derive its input voltage from the main 12 volt line.

    3. How do I generate the 2.0 Volts required for VRef.

    Help in the matter is greatly appreciated.

    Aurangzeb
     
  2. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    Hello,

    I have uploaded the schematic separately for others to be able to contribute more.


    1. I don't think you can power this from the 12V supply that you are measuring, because whichever side of the sense resistor you use for the supply, the other side will be higher voltage at some points in time, because the currents are moving in both directions. Besides, op amps do not perform their best near the rails, even if they claim to be rail-to-rail. I think that it would be good to use a voltage doubler to provide yourself a 24V rail for the positive supplies. This can be a charge pump. Check the current draw of the op amps and feedback network, and then you can use a voltage doubler charge pump that can handle 24V output.

    I can think of solution without a charge pump. If you divide the voltage on both sides of the sense resistor, then you can bring them to the 6V range. Then you can use GND to 12V as your power rails for the op amps. You would need to include a trim pot to adjust one of the dividers to make them equal, otherwise you will see an error due to unequal resistors. It is very expensive to get 0.05% resistors or better. It is cheaper and more accurate to use a trimmer, though you must adjust it after building every circuit.

    2. I would use a 1K Ohm resistor to limit current, so that the microcontroller's internal clamping diode can handle it, if the voltage goes out of range of the microcontroller's supply. 1K Ohm will not affect the internal ADC ability to read the voltage. You could even use a 10K Ohm and the a 1 uF cap to the ground, which will give you a greater protection with no voltage sag on the sampling, and also a low-pass filter. That is often what I do.

    3. Some people say a Zener diode, but I prefer the voltage reference chips, such as ISL60002:
    http://www.intersil.com/data/fn/fn8082.pdf
    You can find it in 2.048V. It has 1 mV and 5 mV versions, at difefrent costs. You may find other voltage reference chips in Karachi, I don't know what you have. Otherwise, you can use a Zener to make a reference voltage.
     
    • IA.jpg
      IA.jpg
      File size:
      43.6 KB
      Views:
      72
  3. aurangzeb

    Thread Starter New Member

    Aug 1, 2010
    9
    0
    Thanks.

    The solution sans the charge pump seems more doable. I had considered it but left it because I thought it will commensurately reduce the differential voltage across the sense resistor.

    Regards
     
  4. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    Yes, the drawbacks of using a voltage divider as inputs to a diff amp are:
    1. You lose a fraction of the delta voltage. For example, with a 1:1 divider, you lose half of the delta.
    2. Hard to match resistors perfectly, so you need to use a trim pot. You need to adjust that by hand, and it may drift over time possibly.

    There are also dedicated chips for the exact purpose you want. For example, the MAX4069, MAX4070, MAX4071, MAX4072 chips:
    http://www.maxim-ic.com/datasheet/index.mvp/id/3387

    They already contain internal references of 1.5V or 2.5V. If you can get them, they will suit your needs perfectly.
     
  5. aurangzeb

    Thread Starter New Member

    Aug 1, 2010
    9
    0
    Infact I paid a visit to the local electronic components market yesterday, and these are exactly the part numbers that I looked for. No luck. The AN649 was also on my list as a backup. Alas no luck on that one too.

    It is exactly for this reason that I have resort/fall back to this 'ancient technique' using three op-amps.

    Aurangzeb
     
  6. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    Aurangzeb,

    I am going to give this problem more thought, because I will be facing a similar problem soon, and it would be nice to have a good solution.

    I think that there must be some arrangement of feedback loops that would give a good result, without an additional power rail, or using the voltage dividers.

    Here are two conditions that may help:

    1. The voltage delta over the sense resistor may be very small, in fact the smaller it is, the less power is lost to the sensing, and the less heat generated by the sense resistor.

    2. The sense points are extremely low impedance. They do not need to be buffered to drive a feedback network in the range of 10K Ohm.

    I'll give this some more thought.

    Sage
     
  7. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    Okay. Here is a circuit. I did some thinking "outside the box".

    Please, tell me what the application is for. I am interested in whether it's a product and for what application / market. Anyway, this is a design that I just created, and I am happy with it. I publish it here for the general public, and make it open source. Therefore, not able to be patented by anyone. I reserve the rights to use this circuit, and for anyone who wishes to use it.

    Here is how it works:

    In the normal operation, when "Control" is low, the capacitors both get charged to an equal voltage. This is from the average across the current sense resistor, to the voltage reference. This uses a 1V reference, although this could be anything within normal range of the ADC. This could be a Zener or other reference.

    When "Control" is switched high, then the circuit rearranges, so that the capacitors get disconnected from each other, and from the voltage reference. Therefore, their bottom side moves into a differential relative to the voltage across the current sense resistor. This will be around the voltage of the reference, in this case 1V. But they are in the correct range to read with the ADC of a microcontroller, and the important thing is the *difference* between the voltages.

    You could read the outputs directly to the microcontroller's ADC, or you could amplify it further using a differential amplifier, if you need better resolution than direct ADC will give you.

    You must program the microcontroller to set "Control" high, then wait for a few milliseconds, then make the readings, and then set it low again.
     
    Last edited: Aug 4, 2010
  8. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    By the way, it is not important what MOSFETs you use.

    M1, M5, and M6 are N-channel.
    M2, M3, and M4 are P-channel.

    The only important thing is that M1 must be logic-level, in other words it must switch on at 3.3V.

    It is also not important to use precision resistors. All resistances are fine at 5% tolerance.

    This design does *not* use an op amp. It is a level-shifter for the actual difference across the sense resistor. But now, you can easily use a diff amp to amplify the difference, if you need to.

    However, if you use a 10-bit ADC as on the AVR chip, then you may be able to get good enough resolution.

    If you do use a diff amp to amplify the difference, then may I recommend the simpler version in Figure 10 on this link:
    http://www.eng.yale.edu/ee-labs/morse/compo/sloa058.pdf
    Same thing as here, except it is buffered inputs:
    http://en.wikipedia.org/wiki/Operational_amplifier_applications#Differential_amplifier

    You may also get away with not buffering the inputs, but you would have to reduce the resistors in my circuit to 1K or so, and use resistors on the order of 100K to 500K for the diff amp.... then when you switch "Control" high you would have to read the result quickly, before it decays. But this would save you 2 op amps in your circuit. You could also use larger capacitors than 10uF for that purpose, to slow the decay.
     
  9. aurangzeb

    Thread Starter New Member

    Aug 1, 2010
    9
    0
    Sage,

    My sincerest thanks for your help and input.

    The circuit is for Battery management for UPS batteries. Pakistan is in the middle of a serious power crises. We face electricity outages of upto 4 hours per day in Karachi. In other areas it is as bad as 10 Hrs. Those who can afford them use gas driven generators. For most people it is UPSs. Since deep dischaarge batteries are not available locally most people use car batteries of 200AH range. However it is normal to for most people who do not understand the principles involved, to drive the battery into deep discharge. Thus the normal life of the car battery in this service , is about a year or so, which adds tremendously to the cost of living.

    My reasons for pursuing this design are thus two fold. One- the short term interest is to mitigate the problem indicated above. Two- my long term plans are to move to an area where we live mostly off grid. This therefore becomes an essential item to have. One cannot use imported off the shelf stuff because of the associated maintainence issues. Once we move to the off grid area I am considering developing a full fledged UPS having this feature built into it, for the local market. Having a μC would help to dovetail wind and solar charging once (and if) these become cost effective and trouble free for normal domestic use.

    To come back to the technicalities, I have a question and a comment.

    Would it be OK if I use 4066 (CMOS Quad Analogue Switches) in place of the mosfets.

    As I understand the circuit, won't the voltage across the two capacitors be 12 volts (+ and - the IR drop). As I see it this 12 volt common mode voltage, would latch up the ADC input- would it not?

    I recall a similar scheme of charging a capacitor being used in CA3162, another A/D converter of yesteryear. I had thought of using it, but my interest is to acquire the ability to measure milli-voltages and have a μC read these. This would then add to my repotoir of useful circuits.

    Thanks again for your help and input. Please let me ponder upon the circuit for a day or two and maybe I can put in my two pence worth.

    Aurangzeb
     
  10. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    Aurangzeb,

    I am pleased to hear the application that you have in mind. We may have room for some partnership in a real sense. The reason that I took on this task is that I am designing a low-cost input/charge/load controller for use with solar and batteries. I plan to make this open source, based on a microcontroller, and to have several useful accessories that would be useful to people in many situations. For example, a diversion load that could heat water, or could operate a cooler. Also, an input for thermostat to control a cooler for keeping medicines or food cool. Also, an output for cell phone charging, and charging of 1.5V cells, as is used in radios and such things. Also and output for LED lighting at night.

    The core of this will be a buck-converter MPPT charge controller that will interface solar and/or wind energy inputs to a battery & load.

    I sympathize with the situation in Karachi, as I spent some time in Kathmandu, where the load shedding is also troublesome and very often. Some people are doing business in UPS and also solar due to this.

    My vision for the project is to make it open source -- hardware and firmware -- so that people may learn from it, and modify or improve it as they need. Also, it would be able to become a cottage industry in various places where there is demand and people to do it.

    Now you have given me another input -- the battery charging must be able to work for car batteries as well as deep cycle batteries. In my house I use deep cycle batteries, but now I see that many people use regular car batteries due to what is available.

    Now back to the technical. I suppose you could use a 4066 for the two back-to-back MOSFETs M3 and M4, although I don't think it is worth it. You may use inexpensive MOSFETs for all of the above -- all signal level, and Rds(on) is not important. Therefore, you can find many in the SOT23 package for very low cost. You may also find dual P-channel in a SOT23-6 package, which would be good for M3 and M4. Or dual N-channel for M5 and M6.

    As to how it works, when "Control" is low, then M3, M4, M5, and M6 are conducting. This will charge up the capacitors to (Vbatt - 1V) because it will be "stretched" between the average across the sense resistor, down to the 1V reference. But the input to the ADCs are on the low side of this, so they will both be at 1V at that point. (You see that R4 and R5 will place the tops of the capacitors at the average of the voltage across the sense resistor. But it is not critical for it to be exactly average. What is critical is only that the capacitors are charged to exactly the *same* voltage so that later, their difference will be exactly equal to the difference across the sense resistor.)

    When "Control" is put high, then M3, M4, M5, M6 will all turn off, and then the capacitors will simply be "hanging" there from each side of the sense resistor. Because their charge is exactly the same voltage, then their bottom side will have the same differential as their top side. They make sort of a "ladder" that will project the voltage delta down to the range where the microcontroller can read them.

    You may also do a differential amplifier, but it will be much easier because the voltage to be amplified is very much closer to the ADC input range. You may even use good op amps that use ground to +5V input supplies, because their inputs will be within range.

    I would implement a differential amplifer simply with two op amps, not three. I would do it as two separate non-inverting amplifiers with reference to the 1V reference. Like this circuit, except using the 1V instead of the Ground for the reference:
    http://en.wikipedia.org/wiki/Operational_amplifier_applications#Non-inverting_amplifier

    Your absolute accuracy will depend only on the offset error of the op amps. If you get op amps with 25 microVolt offset or so, you will have really good accuracy. The resistor matching will only introduce gain error, not absolute error. In other words, zero will be zero if the op amps have low offset error.

    I hope this helps you with the circuit, and I hope we might be able to continue to work together.

    Sage
     
  11. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    Aurangzeb,

    If you're still listening, I think all that complicated circuitry was not necessary.

    I think that a simple diff amp as in this link will work for you.

    Make all the resistors the same value, around 75K or so, and instead of ground, replace that with a 1V reference. That should give you a signal centered on 1V. However, this will depend on the resistors being equal. You may have to order precise resistors, or else hand-pick each pair to be precisely equal.
     
  12. aurangzeb

    Thread Starter New Member

    Aug 1, 2010
    9
    0
    Sage,

    Thanks a lot for your input.

    As a purist I would like to first attempt an 'analogue' rather than a 'mixed type' response.

    Your use of the phrase 'level shifter' got me thinking and I have come up with the idea of using a differential long tailed pair to connect to the (+) line. I plan to use CA3086 type matched pair for transistors along with precision resistors. Since gain is not an issue and basically it is the 12 VDC CMRR that has to be ironed out. I plan to use the +5VDC as the VCC for the + line for this long tailed pair, and take the diff o/p from the collectors, which should have the voltage within the safe range of the downstream op amps.

    Comments appreciated.

    Aurangzeb
     
  13. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    Aurangzeb,

    I still cannot picture the circuit that you have in mind. Can you provide a schematic? I thought that the input to a long tailed pair based on NPN transistors is into the bases of the transistors. How can that be coming from around +12V?

    Sage
     
  14. aurangzeb

    Thread Starter New Member

    Aug 1, 2010
    9
    0

    I meant the common two transistor based LTP, pictured in the link below

    "http://en.wikipedia.org/wiki/File:Long-tailed-pair.gif"

    Input to the bases of LTP transistors would be from the the two terminals of the shunt resistor. HOWEVER The POWER RAIL for the LTP would be the 5 VDC from a 7805 also supplying power to the subsequent op-amps. I dont plan to use the current mirrors in the collector circuits, shown in the above picture. Simple resistors should suffice

    The current source for the LTP can be a simple resistor, with one end tied to the conjoined emitters, and the other end going to GND.

    In order to have high CMRR I plan to use precision resistors (if I can source them here), both in the base ckt as well as collectors. The alpha/beta match should come from the CA3086 transistors.

    Is the above sufficiently illustrative. If not, give me a day to draw something up. I am afraid I am not that handy with computer tools.

    Aurangzeb
     
    Last edited: Aug 13, 2010
  15. aurangzeb

    Thread Starter New Member

    Aug 1, 2010
    9
    0

    Exactly. Please see my recent response on using the LTPs.

    The suggestion here is fine, but does not solve the problem that I face viz. having a Common Mode Voltage (12 VDC) higher than the supply voltage (5 VDC) of the op-amp.


    Aurangzeb
     
  16. aurangzeb

    Thread Starter New Member

    Aug 1, 2010
    9
    0
  17. sage.radachowsky

    Member

    May 11, 2010
    241
    38
    Yes, I see the long tail pair that you are talking about, but my question is that you plan to have the inputs from the shunt resistor to the bases of the transistors, but your supply voltage is lower than those. How does that work?

    My suggestion on a diff amp with a single op amp, I think, would work. You could power the op amp from either the 12V supply, or the 5V supply. If you do the 5V supply then your resistive feedback network would require divider that bring the inputs to the op amp below the 5V, of course. The diff amp solution does introduce the problem of resistor matching, and error would create offset as well as gain error. But theoretically it would work well.

    The circuit I designed with the capacitors has the benefit of zero offset error. Yes, it is a little complicated because you need to trigger it with a uC, but you do have a uC already in your circuit.

    Sage
     
Loading...