code for AC current measurement

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

I am working on project where I need to measure AC current value. I am using a CT for this purpose. I would like to calculate the RMS value of the current.

I think this situation has arose in many of your lives. I would like to ask for the code or pseudo code for finding the RMS value of current.

I am using the internal ADC of the ATmega32 which has a resolution of 10 bits. Even if you can't give me the code, a general pseudo code or algorithmic explanation would be of great help.

Note: The frequency of ac is 50Hz.

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

What waveform/value do you get out of your CT circuit?

With the signal conditioning on my board, I get a DC level proportional to the AC current, and use it as input to the AVR's ADC channel.

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

I haven't yet got my CT connected and working. I presume the waveform to be sinusoidal, as it is a simple transformer with no signal conditioning of any kind. Please note that I don't have a current sensor with in built circuit, rather a transformer.

Do you have the signal conditioning circuit to convert the raw output of the CT into corresponding DC voltage. In that case I might be use simple ADC conversion.

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

You can calc the rms in hw or sw, depending on what you prefer. I think the output of the CT goes to a resistor to convert the current to volts. As long as the pk to pk volts fits in 5V, you can read it with the a/d. Take a bunch of readings, square them, sum them, divide by the number of readings, than take the square root. Done! RMS! QED! I bet you are in the same class with Mr excuseme that just asked how to read ac current with a ct. Those Indian Institute of Technology professors are really smart... they give an assignment to the students that requires use of an avr microcontroller, and then they go out and relax while the avrfreaks teach the students how to do their assignment.

Imagecraft compiler user

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

As Bob points out, you can determine RMS in hardware or software.

If software, you will need to measure a number of points in one cycle, and then compute using the real meaning of RMS, which is "root mean square" which translates into the square root of the mean of the signal-squared.

But, now you have a challenge with AC, which has one half of the cycle that is negative. It will be necessary to offset the whole thing by (at least) (peak-peak)/2 and use that as the ADC input. THEN, you need to do a little algebra to figure out how to get the proper squared signal value from an offset AC input. Not hard, but that is left as an exercise for the "student". If you don't know what mean of a collection of values refers to, you had better look that up, also.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Application note AVR465... explains it all.

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

I have been reading and re-reading AVR465 for a couple of days of now. While it explains how to do the measurement and calculation of the values obtained from the analog front end from both voltage and current measurement, I do not remember seeing anything that talks about offset of the measurement.

How is the negative cycle be measured? How can this offset of AC to the DC level can be achieved?
I understand that the offset can't be created with the software, but can only be reverse computed to find out the original value. What hardware is needed to create the offset? Something like a clamper? Keeping in mind that the voltages can be very low sometimes.

If some kind DC conversion is possible, kindly throw some light on it too.

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

Take a closer look at 465. You will see that a simple digital filter routine is used to track and remove the DC offset. If I recall correctly, it is in the adc interrupt service.

Tom Pappano
Tulsa, Oklahoma

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

tpappano wrote:
Take a closer look at 465. You will see that a simple digital filter routine is used to track and remove the DC offset. If I recall correctly, it is in the adc interrupt service.

I have seen it. Kindly do through my previous post once again. I have said that removing of the offset can be done in software.
My question now how to create that offset in the first place. To be more clear:

1. Can the ADC of AVR measure negative voltages, which are the second half in an AC cycle?
2. If it can't be done, how to shift the entire signal to the DC level?

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

You use two equal resistors and a capacitor. Make a voltage divider between Vref and Gnd, connecting the Vref/2 node to the adc input. Connect the secondary of your CT across its load resistor with one side of that combination to ground and the other side through a capacitor to the adc input. Combine this hardware arrangement with the "good parts" from the 465 appnote and you are good to go

Tom Pappano
Tulsa, Oklahoma

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

The secondary of the current transformer has 2 wires. The load resistor goes across them. Ground one end. The other end has +-ac on it. Hopefully less than 5V pk to pk. You can use a big cap to couple this to a 2.5V voltage divider from 5V to the a/d input. Then you read the a/d, multiply by 5/1023 to get volts, subtract 2.5V bias, then you can invert anything less than 0V. Now you have a full wave rectified signal... you can square and summ all the samples, then take the avg, then the sqrt.

Imagecraft compiler user

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

Thank you all for the help. I am posting the connection as suggested. Kindly correct me if there is a mistake.
.

Quote:
then you can invert anything less than 0V

What do you mean by invert here? Sorry for asking this, but I need to be clear, Does the ADC of AVR measure negative voltage? and Does the inversion you mentioned here is about bit wise inversion?

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

tecoholic wrote:
Does the ADC of AVR measure negative voltage?

No, you have to shift negative values to positive (and zero to middle of adc_ref and gnd).

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

The trick in the Atmel note is that they use an inverting opamp with the + input set at 0.55V. As an opamp always tries to keep the difference between + and - input zero, the input (R31 and R41 in the app note schematic) is also biased at this voltage, and the voltage/current transformer is AC-coupled and biased at this voltage; e.g. at zero current/voltage the opamp out is 0.55V out, at maximumum positive current/voltage zero, and maximum negative current/voltage 1.2V (assuming no amplication here).

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

You don't really need to offset the signal. There may be issues with feeding negative values straight into the ADC but if you're going through an opamp first then it will clip it for you. You can just presume the bottom half is the same as the top, unless you're after super awesome accuracy. I'd just get it working with a half wave first. Depending on what your requirements are it could be totally fine to do it this way.

You can also get CTs that output an RMS value, so that might be something to consider also. I know LEM have some split cores that do RMS but I haven't looked into the details.

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

Quote:
You don't really need to offset the signal. There may be issues with feeding negative values straight into the ADC but if you're going through an opamp first then it will clip it for you. You can just presume the bottom half is the same as the top, unless you're after super awesome accuracy. I'd just get it working with a half wave first. Depending on what your requirements are it could be totally fine to do it this way.

But, when the circuitry to do it correctly is so trivially simple, and a software example is free to use, why not just do it correctly?

Tom Pappano
Tulsa, Oklahoma

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

Invert means multiply by -1. In c that's x = -x;

Imagecraft compiler user