# How Can I Calculate Voltage Sag and THD with an AVR Microcontroller?

• Pagedown
In summary, you will need to sample a lot faster than once a second, and you will need to build an additional ADC circuit to get the sampled data into the microcontroller. You will also need to think about safety in your design. Beyond the safety aspects, it sounds like you need to be sampling many times per 60Hz cycle.
Pagedown

I am doing a project to monitor a single phase(240V) power using a ATMEL AVR microcontroller, and I face some difficulties :-

1. How do I calculate the voltage transients : voltage sag/swell? What is the analysis method?

2. How do I calculate the Total Harmonic Distortion (THD) ?

what are the equations if I have the microcontroller senses the data of voltage and current every second and display useful information?

Pagedown said:

I am doing a project to monitor a single phase(240V) power using a ATMEL AVR microcontroller, and I face some difficulties :-

1. How do I calculate the voltage transients : voltage sag/swell? What is the analysis method?

2. How do I calculate the Total Harmonic Distortion (THD) ?

what are the equations if I have the microcontroller senses the data of voltage and current every second and display useful information?

You're going to need to sample a lot faster than once a second, I would think. What sensors are you using? What are your ADC channels? Can you pull in ADC samples quickly in an ISR and do the calculations as time allows?

I am not really good about microcontrollers.But I will learn through it. A simple voltage divider for voltage sensing and still deciding -current sense resistor/current transformer for the current sensing.

Is it possible I can sample at 60Hz? Will it be tough?

Basic question first:
If I can have samples of data of voltage and current amplitudes, what are the equations that or analysis method to calculate the voltage sag& THD?

Pagedown said:
I am not really good about microcontrollers.But I will learn through it. A simple voltage divider for voltage sensing and still deciding -current sense resistor/current transformer for the current sensing.

Is it possible I can sample at 60Hz? Will it be tough?

Basic question first:
If I can have samples of data of voltage and current amplitudes, what are the equations that or analysis method to calculate the voltage sag& THD?

Since you are sensing the AC Mains, you need to think about safety in your design. You can't just be sampling the AC Mains directly and make connection to your AVR circuit, if the AVR circuit is human-accessible (can be touched). That would be a bad shock hazard.

Instead, you would usually use some sort of isolation technique to get the sampled data (or the analog waveforms, but more usually the digitized data) into the AVR. You can use optocouplers to safely couple the digital control and data signals across a barrier, for example. The sensing and ADC circuitrty would be on the AC Mains side, and the AVR would be on the SELV (Safety Extra Low Voltage) side of the optocoupler boundary.

There are current sense transformers (CTs) that can be used to sense the series current. These are inherently isolated, so you would not need to isolate the sensing and digitizing circuit for the current measurement, if you use a CT.

Beyond the safety aspects, it sounds like you need to be sampling many times per 60Hz cycle. Especially if you want to be able to calculate THD. What kind of specification do you have for your measurements of "voltage transients"?

Yup, I forgot to mention I will be using optocoupler in between the sensing circuitry and the AVR. I'm using the ADC of AVR, and I dun't know whether it is possible.

It doesn't need to be a high-tech measurement, but just a basic approximation of voltage sag and THD. That's why I really don't know where to calculate this and present it in a useful form.Maybe in percentage?

Pagedown said:
Yup, I forgot to mention I will be using optocoupler in between the sensing circuitry and the AVR. I'm using the ADC of AVR, and I dun't know whether it is possible.

It doesn't need to be a high-tech measurement, but just a basic approximation of voltage sag and THD. That's why I really don't know where to calculate this and present it in a useful form.Maybe in percentage?

It's easier to pass digital data across the opto boundary, rather than try to send analog information across the boundary for digitizing after.

For the THD, you will need to do the signal processing of the samples to compare them to a pure sine wave (of the same phase):

http://en.wikipedia.org/wiki/Total_harmonic_distortion

.

So should I built another ADC circuitry for both sensing?

Okay, can the 8bit-microcontroller handle all this calculations?

Pagedown said:
Okay, can the 8bit-microcontroller handle all this calculations?

That depends on the specifications for accuracy, etc. What exactly are the specs for this project? And have you been given some safety guidance by the instructor? I hope s/he didn't just say, "Go measure the AC Mains", and turn you loose!

he just asked me to include THD and voltage sag in my measurements.

It is a power monitor & control using a microcontroller to measure voltage and current, power consumption, power factor etc..

He did not give me exact specs, thus it means I can make my own specs. But, I still can't as I'm struggling to calculate both of this, even by using hand.

Pagedown said:
he just asked me to include THD and voltage sag in my measurements.

It is a power monitor & control using a microcontroller to measure voltage and current, power consumption, power factor etc..

He did not give me exact specs, thus it means I can make my own specs. But, I still can't as I'm struggling to calculate both of this, even by using hand.

Doing it with an 8-bit uC will be challenging. I wrote some DSP code on an 8051 one time a while back, to calculate EEG spectra in real-time. I don't recall the sample rate offhand, but you will be sampling much faster, at least 16x 60Hz, and preferably faster.

You can use some shortcuts to help out, like using table look-ups for some things instead of actually calculating them. Does your AVR at least have a hardware multiply?

You can use other tricks like including zero crossing circuitry on the V(t) and I(t) channels, instead of calculating the zero crossings from the full digitized waveforms. That will give you an approximate power factor, just from the zero crossings.

You can also look at "metrology" chips, which are made for measuring power in these kinds of circuits. They will give you some idea of what-all is involved in these measurements. Also, you can compare the accuracy available from the metrology chips (and interfacing them to the AVR uC with SPI or whatever), to the best that you can do with your uC by itself. The difference will be quite large.

If you could assume that the THD was low, then you could use peak sensed values for V(t) and I(t), along with the zero-crossing data, to estimate all of the numbers that you mentioned. But to measure / calculate THD, I think you will need the sample rate that I mentioned (16x or higher).

Maybe others know of some tricks for calculating THD without full sampling of the waveform?

## 1. What is voltage sag?

Voltage sag, also known as voltage dip, is a temporary decrease in voltage level that occurs in an electrical power system. It can be caused by various factors such as sudden changes in load, switching of heavy equipment, or faults in the power grid.

## 2. How does voltage sag affect electrical equipment?

Voltage sag can cause disruptions and malfunctions in electrical equipment. It can lead to reduced performance, unexpected shutdowns, and even damage to sensitive components. In severe cases, it can result in complete equipment failure.

## 3. What is THD?

THD stands for total harmonic distortion. It is a measure of how much the waveform of an electrical signal deviates from its ideal sinusoidal shape. It is usually expressed as a percentage and is used to evaluate the quality of the power supply.

## 4. How does THD relate to voltage sag?

Voltage sag can cause an increase in THD levels in the power supply. This is because the sudden drop in voltage can cause the equipment to draw more current, resulting in distorted waveforms. High THD levels can further worsen the effects of voltage sag on electrical equipment.

## 5. How can voltage sag and THD be mitigated?

There are various methods for mitigating voltage sag and THD, such as using voltage regulators, UPS systems, and harmonic filters. Proper maintenance and regular inspections of the power system can also help prevent these issues. In some cases, it may be necessary to upgrade the power system infrastructure to handle the load and reduce the risk of voltage sag and THD.

• Electrical Engineering
Replies
38
Views
3K
• Electrical Engineering
Replies
1
Views
2K
• Electrical Engineering
Replies
11
Views
3K
Replies
18
Views
1K
• Electrical Engineering
Replies
3
Views
2K
• Electrical Engineering
Replies
7
Views
3K
• Electrical Engineering
Replies
8
Views
1K
• Electrical Engineering
Replies
10
Views
3K
• Electrical Engineering
Replies
4
Views
3K
• Electrical Engineering
Replies
1
Views
999