Measuring Current using adc on atmega16

Go To Last Post
27 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I want to measure dc current by measuring the small difference in voltage across a shunt resistor. The dc has ripple (output of a full wave rectifier). Before proceeding with construction I would like to be discuss an issue that comes to my mind, that is as follows.

The sample and hold circuit of the adc would measure the amplitude of the voltage at various points in time. Since the waveform is full wave rectified dc, the adc would "catch" the waveform at different points thus giving different values for each reading.

How can I mitigate this problem.

Thanks

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Capture the largest (peak) value. Then process in whatever way you wish.

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Depends of what you really want to measure.
Normally you take N samples over exact a AC periode (or M number of periodes), And from that have all the information you need.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome on AVR Forum..

Quote:
I want to measure dc current.

Which parameter you are interested in? Min, max, average, rms, period, di/dt, fft, filter on specified length or infinite time... There are thousands of measures of a time function.

Differential input with gain.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brutte wrote:
Welcome on AVR Forum..

Quote:
I want to measure dc current.

Which parameter you are interested in? Min, max, average, rms, period, di/dt, fft, filter on specified length or infinite time... There are thousands of measures of a time function.

Differential input with gain.

The shunt resistor I will use is 60mv/50A. My measuring range is 5 to 50 Amps. So I would probably be using 46db gain channel as I plan to use 2.56 VDC internal reference.

I would like to measure average value of the dc current. The purpose is to sense the amount of Ampere-Hours discharged from a UPS battery during operation of the UPS's inverter. Since I am using an automotive battery, I need to ensure that the battery does not go into deep discharge.

Thanks

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ross & Sparrow.

Your solutions would require me to add some kind of a zero crossing hardware to detect start and stop points accurately. I had thought about this but somehow I feel I should be able to do the measurement without resorting to zero crossing detection.

Thanks
Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No if you look at exact 1 periode you don't care where in it you are, everything is just repeats!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please verify your shunt resistor. I am familiar with 50A/50mV shunts, which are common. I've not used a 50A/60mV. Anyway, if you are measuring a battery, you will have no ripple. But if you are measuring a rectifier output, you will have some ripple. I would think that the ripple would be small enough to ignore. If you read the shunt current once a second you will probably get an average over time anyway. But, if I were doing this, I would read a short burst of samples over 1/120 of a second - this would be the period of a full wave rectifier on a 60 Hz system. I'd probably read 5 samples in that time, and then average them. The average is to be used for calculation of amp-hours.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

agentile wrote:
Please verify your shunt resistor. I am familiar with 50A/50mV shunts, which are common. I've not used a 50A/60mV

60mv/50A is what it says on the packing. Thank you for pointing it out. I'll keep this point in mind and reverify at finalization.

Actually the ripple comes from the battery charger in the UPS which is a basic full wave rectifier with a center tapped transformer and two diodes.

In order to avoid going into deep discharge I would need to keep an account of charge extracted as well as the charge added. As is obvious, the difference of the two values is the 'state-of-charge' of the battery. The term 'charge-added' creates the need to factor in the ripple.

I am going to use it at 50 Hz. Therefore the time available for half a cycle (of the full wave o/p) is 1 msec. Would it be possible to take (say) 3 or 4 successive readings and then use a kind of Newton-Raphson integration. Would such an approach yeild sufficiently accurate results.

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I would like to measure average value of the dc current.

So you want to make a coulomb-meter.

Quote:
I am going to use it at 50 Hz. Therefore the time available for half a cycle (of the full wave o/p) is 1 msec

Are you absolutely sure about this 1ms?

Anyway, if you know the shape of your waveform, then you do not need to sample it many times per period, but simply measure one parameter, for example its max value, and then multiply it by a const, because average is proportional to amplitude..

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You won't need anything as sophisticated as Newton-Rhapson. At 50 Hz the rectified period would be 1/100 second, or 10 ms. If you set a timed interrupt to read the analog input pin at once every 2ms, then after 5 samples you will have a period of the waveform. With 5 samples, it may not be perfect, but it will be representative of the waveform. You could always have the analog read at 1 x per ms. If you performed this once per minute you would have a pretty accurate means of monitoring the battery discharge rate.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aurangzeb,

This may be obvious to you, but I'll mention it anyway.

Where in the circuit is your sense resistor? High Side?

Is your AVR tied to ground?

Be aware that although the voltage across the sense resistor is very small, you still can't connect the resistor directly to the ADC inputs, if the voltage with respect to ground is greater than 5V, (or whatever you are powering the uC at).

If, for example, this was a 12 V battery, the voltage at the sense resistor would be about 12V, +/- a little bit on each side of the resistor. But you can not feed the 12V signal into the ADC as it is > 5V.

Your design may have already accounted for this, but I just thought I'd mention it to be sure.

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

aurangzebhaque wrote:
Ross & Sparrow.

Your solutions would require me to add some kind of a zero crossing hardware to detect start and stop points accurately.

I don't wish to be argumentative, but I never said or suggested that at all. I simply said capture the largest value. You could do this without knowing anything about the period. Take enough samples, sort them by size and use the largest value.

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

More accurate to fit a rectified sinusoid signal to the points and integrate it (ie: A*sin[Bx] --> -A/B*cos[Bx]) than resort to N.-R. However, you can just average a window of your ADC readings and be done with it, so long as they not sampled at subharmonics of the mains frequency (ie: not rational fraction of 50Hz or 60Hz). This ensures you're not reading the same points on the curve in consecutive periods or within the same averaging 'window', which would result in an offset error in the calculated average. Try 599 Hz (~1669 µs period) sampling frequency - a prime number... or just kill it in the 100-250 kHz range and don't worry about this numerical drift BS ever again. :D

The chosen 'window' period must be an integer multiple of the mains period.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:
Aurangzeb,

If, for example, this was a 12 V battery, the voltage at the sense resistor would be about 12V, +/- a little bit on each side of the resistor. But you can not feed the 12V signal into the ADC as it is > 5V.

Thanks for this very pertinent point.

The shunt (sense)resistor is located on the 12 VDC + rail. Therefore YES it cannot be connected to the ATmega analogue ports directly without some kind of a voltage divider. However the down side of having a voltage diffence to be measured, is also reduced by the same quantum. Thanks to ATMEL who had forseen my problem in advance and put in a 46db gain stage WHEWW :D . So I am mercifully saved from adding another 741. Thank you too, ATMEL for the op amp on the atmega16 chip.

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brutte wrote:
Quote:

Are you absolutely sure about this 1ms?
/quote]

Oops 10 msec :oops:

Yes it is essentially a coulomb meter that I want to make.

The problem of measuring charge delivered to a battery is slightly more complex. Please visualize the following in a graphical format.

1. The pulsating dc o/p of a full wave rectifier
2. The constant 12 VDC (battery voltage) represented by a flat line parralel to the x axis.

3. The intersection of 1 and 2 above.

Please note that charge will only be delivered to the battery once the voltage is above the battery voltage plus 0.7 volts for the voltage drop in the rectifying diode. This means that the charger can only deliver a charge to the battery once the above condition is satisfied. In graphical form this is represented by the area between the sinosoid and the flat line. As the battery charges the flat line keeps going up (battery voltage rising), thus reducing the area between the two curves. We know that this happens when we charge a battery using a constant voltage source, in the shape of falling charging current.

Therefore in my opinion it is not possible to measure this by generating an inturrupt every 2 msec as there would be no current flow during most part of this period. The current only flows during a part of the cycle around the peak of the sinosoid.

Am I wrong in my design . If so how do I go about doing it.

I may please add here that I have already excluded the option of using hall effect sensors and battery charging atmegas. These are not available locally and experimenting with imported stuff is dicey. If something goes wrong you are stuck until the new (and very expensive) component arrives.

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For accuracy with an arbitrary waveform use an integrating amplifier (as opposed to a peak detector). Sample and reset as necessary. You might get by with AC coupling, else drift will need correction somewhere.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
use an integrating amplifier

That is another option. You could use OCR channel to trigger ADC and to reset coulomb-meter at precise times. And you could kill integrating and gain with one op-amp.

Another option is to use a capacitor to smooth the ripple. Current flow will be DC and ~const.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Therefore in my opinion it is not possible to measure this by generating an inturrupt every 2 msec as there would be no current flow during most part of this period.

???

I'm not an artist, but it sounds like this is the general concept, illustrated below.
Sin voltage Vin.
Full Wave Rectified, as shown.
Va is voltage on battery + 0.7 diode drop, and this increases as the battery charge increases.
Current through Rsense shown above.
(Ignore any negative current from a load between the charging pulses for now).
At 50 Hz, period is 20 mSec.
AVR can sample at 15 KSamples/Sec.
Who said you have to limit your samples to 1 every 2 mSec???
Sample faster, much faster, for the overall resolution you want on your Coulomb meter.
Sample every 0.5 mSec for example.

As stated by several people, you want your sample interval to be 20 mSec, or in this case, 10 mSec, as each half of the cycle is symmetric. Where within the period you start & stop sampling doesn't matter, what matters is that your sample interval is 10 mSec.

If you start "late" into a sample, past the zero crossing point, you will capture the "missed" part as the beginning of the next half period. Since you are adding (or integrating) the data, it doesn't matter.

Low battery state, lots of charging current, lots of + values. High battery state, low charging current, very few samples with a positive value.
Fully charged, the charge pulses are absent, samples are all zero.

You take your samples via an interrupt service routine, triggered by a timer. Sample every 0.5 mSec, or whatever. You have two buffers, one for storing the incoming data for the new samples, one buffer with the last interval's samples which you process. Swap the buffers every sampling interval.

JC

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC

Not only am I obliged, I am simply overwhelmed. My sincerest gratitude for your effort and time to make this diagram and explain this so succintly. May God bless you for this.

I am now confident and will proceed. Will keep everyone posted as it progresses. Thanks to others for their time and input.

Regards & Good wishes all round.

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again everyone.

Having started work, I am now faced with the next issue which is regarding the actual measurement of the voltage across the shunt resistor. I had initially planned to use the differential input on the AVR ADC for measuring voltage. After purchasing the components to start construction, I was dismayed to discover that the PDIP package of the ATMEGAs, does not guarantee the differential function of the on-board ADC. Now I am back to square one with a ATMEGA16 in PDIP package already purchased. I can’t use the TQ and other packages since I am using a veroboard for construction.

Research on the way forward indicates that I will need to use an instrumentation amplifier(IA) to amplify the 5-50 millivolts available at the shunt terminals. I am thinking of using the classical three op-amps based instrumentation amp, using LM324, primarily because it has 4 op-amps per IC plus the fact that it is easily available. The more sophisticated IAs from Analogue Devices are also possible subject to their availability in the local market. The availability aspect however is yet to be determined.

My problem is now to design an IA using a single supply. I am toying with the idea to create a virtual ground using two 5 volt zeners in series and a resistor connected between the 12 volt supply and GND. The virtual ground would be at the junction of the two zeners, and would be used to connect to GND point of the IA.

Will such system work accurately enough and be interfacable with the u-controller without latchup etc. Will it have sufficent gain. Am I even on the right track.

Thanks and regards

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

the PDIP package of the ATMEGAs, does not guarantee the differential function of the on-board ADC. Now I am back to square one with a ATMEGA16 in PDIP package already purchased.

AFAIK the differential ADC restriction is not present in the pin-compatible ATmega164. You might consider using that model, as it has more toyz ihn general and you will probably find it less expensive.

The differential+gain can be really cool in certain app situations, allowing e.g. overcurrent and undercurrent "trips" to be recognized with a very minimum of components.

However, we find that for "measuring the current apps--to, say, 9 or 10 bits, +/- 20mA on a 0-10A signal--we'll have signal conditioning and an op amp in there anyway.

[Most of our apps seem to end up with a CT or Allegro sensor instead of the conventional shunt resistor.]

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
AFAIK the differential ADC restriction is not present in the pin-compatible ATmega164. You might consider using that model, as it has more toyz ihn general and you will probably find it less expensive.

[Most of our apps seem to end up with a CT or Allegro sensor instead of the conventional shunt resistor.]

Most of the time the issue is non availability of parts. Being a price sensitive market, we only get the least costly parts locally off the shelf.

Before embarking on this project I had done a thorough survey in trying to locate a hall effect current sensor, without much success. The very low demand for such components discourages imports/ready availability ex-stock locally.

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Most of the time the issue is non availability of parts. Being a price sensitive market, we only get the least costly parts locally off the shelf.

OK, and which is "least costly" of the two models that I mentioned? that is part of the whole point.

I guess sometimes you just must make do with what is on the shelf at the corner market. We haven't done that for at least 10 years, preferring to go with distribution channels.

So if your corner market handles no RGB LEDs or whatever, then you must create your own LED clusters?

Tough environment.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
So if your corner market handles no RGB LEDs or whatever, then you must create your own LED cluster.

In a way, Yes. In this case we are constrained to use a primitive method (using the shunt).

BTW it is not the corner market but the main shops of the city where I checked. Don't have any place better locally to source these kind of components.

Aurangzeb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You keep mentioning "locally". There is a place in your profile to fill in your city and country. Then we will have an idea what you mean when you say "locally".

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry about that.

Karachi, Pakistan.