Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

dsPIC33F to measure RMS value of current

Status
Not open for further replies.

kvk.1986

New Member
Hi
I am using a dsPIC33F micro to measure energy drawn from a battery. The battery is connected in a solar energy system. The output of a MPPT controller charges the battery during the day and in the night it is fed to a sine wave inverter that drives AC loads. The battery current is measured using a bidirectional current sensor (LEM CAS-6, www.lem.com/docs/products/casr%20series.pdf)., which gives a DC output - 2.5V @ zero current. This voltage is fed to an ADc channel on the dsPIC33 to measure the value of the current.
The output of the current sensor swings from 2.5 to 0V in one direction and 2.5V to 5V in the other direction. When the inverter is turned OFF, the sensor output voltage is steady at a value proportional to the current (100mV/A).
The problem is that when the inverter (whose output is 230V AC, 50Hz) is ON, the current sensor output voltage is getting modulated by a 100 Hz sine wave. This is like an AC signal clamped to a DC offset and not a real AC signal with zero crossing, the voltage does not swing around 2.5V DC. The DC offset and the amplitude of the AC portion vary in proportion to the magnitude of the current.
I am unable to understand how to get real current value from this signal. I am also calculating the energy drawn from the battery by multiplying the battery voltage and the current, so the accuracy of the current measurement is important to get the correct amount.
Any ideas ? Does the RMS value of the signal in this case give the actual current ?
 
Since the voltage is a constant DC value (if I understand correctly) then the current value to use for calculating the power is the average current, not RMS current.
To get the average current, just low-pass filter the voltage from the current sensor, either with an analog RC filter, or a digital filter in the micro software (as long as the A/D converter sampling frequency is over twice the AC frequency or >200Hz).
 
Depending on the number of factors, the invertor-induced amplitude in battery voltage may be quite high (up to 20% of battery voltage with mine). So, the correct way is to measure current and voltage simultaneously and then calculate sum(V*I*dt).

Moreover, because of changes in battery voltage, the output capacitor of the solar charge controller charges and discharges accordingly. So, when production is low (or at night) the current between batteries and solar charger may be reversed during some parts of the 100Hz inverter-induced cycle. When such reversion happens, batteries are charging the output capacitots of the charger. During the night, this may waste some energy, which goes into heating wires between batteries and the solar charger. If you measure the flow of energy from the solar charger, you may need a bi-directional sensor too.
 
Hi ,
Thanks for the inputs.
crutschow: You are right, the voltage is a constant DC value - either 48V or 96V, which of course reduces slowly with discharge through the night. Right now i am taking an average of the current over 32 samples at a frequency much > 200Hz. I am worried that in my serial port log i see the average value of current samples varies greatly. Hence this thread :).

NorthGuy: I find this an interesting observation, about the output capacitance of the solar charger. I had not thought about it. As for the energy calculation, as you said, i am indeed integrating the V*I product over time to get the energy.
Just an observation : There is a problem in using the same sensor for bidirectional current sensing in case of same amounts of current in either direction. The sensor is capable of swinging 2.5V in either direction, resulting in 25A (@100mV/A) sense in each side. However, the dsPIC being a 3.3 ~ 3.6 volt (max) device, the range of measurement is only 0 to 3.6V. So in one direction we can measure 25A but in the other, the ADC saturates at 11A :(.
 
Hi,

It looks like one problem is that you can not take the average over any period you feel like taking it over, you must think about what it is that you are measuring, such as the frequency. It doesnt matter if you take 10 samples or 100 samples that's not the only consideration.
For example, sample a 10vac, 1 Hz signal with 10 samples near the peak and you'll get values near 14 volts, but sample near the zero crossing and you could get zero.

What this means is that the samples have to be spread depending on the lowest frequency. If the lowest is 100Hz then you should be sampling maybe 10 times per cycle over maybe a 1 second period. We could look at some exact numerical examples to see how this works better though. If you take too few samples over some time near one cycle but not exactly one cycle, that probably wont work either because when we take the average we have to know the period and that implies that we must be synchronized to the waveform period. Over many cycles however, the error damps out.

There is a nicer, cleaner way to do it however, and that is to digitally filter the input using methods from sampling theory. The simplest of outcomes from this would be a filter that ends up being a running average filter, which could also be called a digital low pass filter.

The implementation in code is almost TOO easy.
Starting with what we know about averages, if we had 10 samples that averaged 20v, then we know that if we did a true running average we would subtract the first sample taken and then add in the last sample taken, then compute the average again. But since we already have the information about the average (the average itself) we can approximate this process by subtracting instead 1/N times the average, which in this case would be 2v. We then add in the last sample divided by N, and we have the new average ... all without any extra storage. In equation code form this looks like this:
Avg=((N-1)*Avg+NewSample)/N

and that would most likely have to be done in floats.
So for example if we choose N=32 then we would have:
Avg=((31*Avg)+NewSample)/32

however, N should be large enough to get a true average over enough cycles of the lowest frequency. This might mean making N much larger than 32. Of course you then have to watch the precision and make sure there is no overflow, and also make sure the entire progression is fast enough for your needs. If it turns out to be too slow then you may have to go to a more complex calculation.

Since you are doing DC plus a sine wave, if you are sure it is a sine wave then you might be able to search for the peak and valley and simplify the calculation. We could look at this more if you like.

You could also convert the equation above to other forms if you like.
 
Last edited:
Hi ,
Thanks for the inputs.
crutschow: You are right, the voltage is a constant DC value - either 48V or 96V, which of course reduces slowly with discharge through the night. Right now i am taking an average of the current over 32 samples at a frequency much > 200Hz. I am worried that in my serial port log i see the average value of current samples varies greatly. Hence this thread :).
.........................
Then just use a simple RC low-pass filter to smooth the signal.
For example, a one-pole RC filter corner frequency of 1Hz will attenuate the 100Hz ripple by a factor of 20dB /decade or a total of 40dB.
 
Hi,

Actually, digital low pass filters can yield better results than their analog counterparts, plus they dont require any extra parts.
 
Hi,

Actually, digital low pass filters can yield better results than their analog counterparts, plus they dont require any extra parts.

...but the analog versions are sure easier to debug and understand...
 
...but the analog versions are sure easier to debug and understand...

Hi there Mike,

Yeah, i guess it depends what the individual is comfortable with. I've worked with digital filters enough to feel confident about how it is going to work and what benefits i can get from it and what the limitations are. What i like the most probably is i dont have to add an extra resistor and cap, and the cap sometimes comes out being a larger electrolytic. I also like getting higher resolution sometimes too though using the digital method.
 
However, the dsPIC being a 3.3 ~ 3.6 volt (max) device, the range of measurement is only 0 to 3.6V. So in one direction we can measure 25A but in the other, the ADC saturates at 11A :(.

Either get dsPIC33EV which runs at 5V or build a voltage divider (possibly followed by a buffer) to decrease your voltage to the suitable range (e.g. 0 to 3V centered over 1.5V).
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top