This engineering brief introduces the concept Root Mean Square (RMS) calculations. It is applicable to a sampled system such as the one described in this article. This prerequisite article describes how a MIKROE Hall Current 7 Click current sensor may be used to measure a 60 Hz current. This sampled data is placed into an array providing a convenient starting point for our RMS introduction. Representative data are shown in Figure 1 with a 60 Hz waveform sampled at a rate of 2000 samples per second. Figure 2 presents the Arduino and MIKROE equipment featured in the prerequisite article.
Figure 1: The microcontroller stores 1000 samples in an array called samples.
Tech Tip This demonstration assumes that the amplitude of the fundamental 60 Hz signal is significantly higher than any harmonics. For simplicity, we deliberately avoid the conversation about Nyquist sampling rate and filtering.
Note that a representative portion of the periodic signal must be sampled. In theory, measuring a single half cycle of a sinusoid would be sufficient. Anything less would fail to identify the full symmetrical signal resulting in an RMS calculation that varies with every iteration. In practice, additional samples are desirable to mitigate against noise.
Figure 2: Image of the Arduino and MIKROE equipment used to measure current.
Understanding Root Mean Square (RMS)
RMS is best viewed in terms of heat. We begin with two circuits; one is a DC system, and the other is AC. Each system drives an equivalent load resistance. The term RMS equates the voltages and currents in each system. For instance, 10 A DC current is equivalent to a 10 A AC_{RMS} current, as each provides the same amount of heating.
With integral calculus operating on a pure sinusoid, we can show that the RMS values are calculated as:
V_{RMS} = \dfrac{V_{peak}}{\sqrt(2)}
I_{RMS} = \dfrac{I_{peak}}{\sqrt(2)}
Always remember that this calculation holds true for a pure sinusoid. As a counterexample, a square wave symmetrical about the X-axis has an RMS value equal to the peak value.
Tech Tip: While this article is focused on calculating current, we recognize that the same technique is used for RMS voltage calculations.
How is RMS current calculated in a sampled system?
The Root Mean Square math calculations are implicit in the name “RMS”. It’s a left to right operation applied to a sampled system:
-
sumOfSquares: We start by squaring each sample and adding all values to an accumulator.
-
meanSquare: The squares are added together and then divided by the total number of samples.
-
RMS: The final operation is to take the square root of meanSquare.
The process is described in this equation:
I_{\text{RMS}} = \sqrt{\frac{1}{N} \sum_{i=1}^{N} I_i^2}
Tech Tip: The RMS calculation is inherently related to noise. The term “mean” implies that we are calculating the average of many independent samples. This averaging operation operates as a low-pass filter with a tendency to average out the noise. Just like a running average filter, a filter that incorporates 20 samples will appear steady when compared to a filter with 2 samples.
Never forget that a “long” averaging filter lives in the past. A sudden change in current will not be detected until it propagates through the filter chain. In other words, there is a balance between noise and responsiveness.
This code snippet performs the RMS calculation on an array of samples stored in an array called samples. As described in the prerequisite article, the samples are stored in their raw integer values taken directly from the ADC. Use of integers instead of floats saves memory and results in a faster sampling process. The slow RMS calculation
sumOfSquares = 0.0;
for (i = 0; i < NUM_SAMPLES; ++i) {
uint16_t sample = samples[i]; // samples[] is an array of type uint16_t
ADCVoltage = fMap((float)sample, 0.0, 4095.0, 0.0, 5.0); // Convert raw ADC values to a voltage
ADCCurrent = fMap(ADCVoltage, 0.5, 4.5, -100.0, 100.0); //Convert voltage to corresponding current
sumOfSquares += ADCCurrent * ADCCurrent;
}
meanSquare = sumOfSquares / NUM_SAMPLES;
RMSCurrent = sqrt(meanSquare);
The crude but functional demonstration code is located here: RMS.ino (2.3 KB)
Tech Tip: The code efficiency could be improved by implementing a lookup table. In fact, a single table could be used to convert a given raw ADC integer into the square of the current. This would eliminate the two fMAP statements along with the Multiple-Accumulate (MAC) squaring operation. Be sure to evaluate your project to balance the speed vs memory consumption tradeoffs.
Next steps
A batch process is used for the code described in this article where 1000 samples are collected and then processed. As written, there is a 500 ms delay between each calculation. This provides a good result as 1000 samples are used in the calculation making the system relatively immune to noise. This may be ideal for a data collection system but is unlikely to meet the requirements of a control system that acts on the RMS value.
The system’s performance may be improved if several different calculation techniques are included each with a different time frame:
-
Instantaneous: A reflexive system could be constructed that faults on a single datapoint. For example, a short circuit could be detected by a rapid change in current. This could be a simple sample-to-sample jump detector or a complex control algorithm that predicts the next sample current with a windowed trigger to identify when the current sample varies significantly from the prediction.
-
Intermediate: The RMS calculation must include enough samples to truly represent the waveform. For a symmetric sinusoid waveform, at least 50 % of the waveform must be sampled. For a 60 Hz system, this implies that an RMS calculation include multiple samples over an 8.3 ms period. For noise immunity, it is better to include at least a full cycle with a 167 ms period.
-
Long-term: The long-term RMS calculation is featured in this article where each calculation included 500 ms of sampled data. This provides the smoothest yet old data.
Additional steps could be taken to perform a “running average” RMS calculation to provide best available RMS data. The programming technique would use a circular buffer like the classic boxcar running average filter. This subtract-the-old and add-the-new to the sumOfSquares accumulator has a relatively low overhead making it perfect for use in an ISR.
Parting thoughts
The RMS calculations for a sampled system are relatively simple. A few lines of code with a loop to accumulate the sumOfSquares is found at the core of the calculation. At the same time, there is wonderful complexity as we attempt to build a control system on top of the RMS calculations. We must contend with the time it takes to sufficiently sample the signal and mitigate against noise. A variety of trade offs may be required depending on the needs of the project.
You are encouraged to review the prerequisite article. You will find a collection of promising projects including construction of a complex power meter to calculate complex AC power.
Please let us know what information you found helpful. Also, let us know what topics should be refined or expanded.
Best wishes,
APDahlen
Related information
Please follow these links to related and useful information:
About this author
Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET-accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this.