Editor’s note: I am pleased to have Aubrey Kagan back as a Planet Analog blogger after a brief sabbatical. Aubrey is one of the best tech bloggers I know with a wealth of knowledge on so many interesting Analog topics
Hands up all who remember I did a blog on measuring the RMS (root mean square) value of a signal. Hmmm. Nobody! Well for those of you who don’t believe that I wrote one you can find it here: RMS Measurement. In that blog I described different methods that could be used, but I omitted the possible approach where the RMS value is generated using hardware for the mathematics. You could use a Digital Signal Processor (DSP) or an FPGA. I an unfamiliar with both, but as I have become more familiar with the PSoC family the thought niggled at me that perhaps I could create an RMS measurement completely within a PSoC5 device.
For those of you unfamiliar with the PSoC concept from Cypress, the device provides a microcontroller plus hardware blocks to create your own peripheral set. These peripherals are known as “components” and each component in fact comes with its own data sheet. The concept extends across digital and analog functionality. It may not have the granularity of an FPGA, but the approach can generate some very versatile designs. Full disclosure: I am not, nor have I ever been employed by Cypress or its representatives and distributors, but my employer is a part of the Cypress Design Partner program.
Let’s start out with a little background of how I am going to adapt the mathematics to get to a result. The RMS value of a signal is given by the equation:
If we work with discrete measurements and for simplicity let’s work with t0=0 we can approximate and sum the measurements as follows:
Where Δt is the sample time and N is the number of samples. Conveniently the Δt cancels out and we are left with
In other words we take N samples, judiciously timed to include a full cycle exactly (technically you can of course go beyond that), sum the squares of the readings and divide by the number of readings- and then take the square root. That’s pretty much the way it is done in software as well.
This series of blogs follows the signal path, which means I have the advantage of hindsight as to what problems there were and the actions I took to solve them. I certainly didn’t develop the concept linearly. I really needed to figure out how to realize the more complicated parts before I knew if the idea was viable. I was fortunate to have a CY8CKIT-050 PSoC 5LP development kit (using a CY8C5868AXI-LP035 device) along with a MiniProg3 debugger. I am indebted to the folks at Cypress Tech Support, especially Sujay Yadawadkar and Erik Schembor for their excellent support as I built this concept.
You can find my PSoC Creator project here. Please be aware that because of a minor bug (detail to come in my second blog) in PSoC Creator version 3.3 it is better to work with Creator 3.2 or earlier. I developed this with PSoC Creator 3.1SP3. If you are going to open my project you will need to download the Square Root component before you will be able to generate and compile the project without an error. I will describe how to do that that in my third blog on the square root generation.
The first thing to note is that in generating the RMS value, the input signal is squared, so there are no negative numbers and for a single supply device this translates to the possibility of using a full wave rectifier. Of course you might just go for a bridge rectifier, but there are the voltage drops due to the diodes and the zero is offset from ground.
I did find a Cypress app-note loose on the web on making a precision rectifier using the mixer component, but as I tried to implement it I found that there is no AN59649 on the Cypress web site and that description does not match the mixer component, so obviously the promising concept did not really bear fruit. That means I had to resort to a simple precision rectifier using an op-amp, of which there are many examples on the web. I appropriated a circuit from Linear Technologies (also in Microchip’s AN1353) which allows for rectifying a bipolar signal with a single supply op-amp. This is the circuit that appears in Figure 1-1.
A single supply op-amp (a component on the PSoC5) configured as a precision rectifier. Note how PSoC Creator allows you to add external components (in the dark blue) to help with documentation. The physical input and output pins on the device are indicated by the squares on the schematic.
Implementing this on a PSoC is straightforward using its op-amp component. I did find a 95mV differential in the peak value between input and output for a sine wave, and the difference for a triangular wav was imperceptible. The output signal started clipping at 5.85Vp-p on a device powered at 3.3VDC.
The rectified signal is connected to an analog to digital converter (ADC) as shown in Figure 1-2.The ADC is configured to initiate a conversion according to an external signal that is derived from a clock that is 16 times the frequency of the incoming analog signal. This is one of those cases where my choice of 16 samples is described later.
Here the rectifier drives the ADC analog input and the timing circuitry initiates a conversion at 16x the analog signal frequency of 50 or 60Hz. Note that there are 3 I/O pins for the op-amp and a separate input pin for the ADC analog input. They all must be externally wired.
The ADC can be configured as 8, 10 or 12 bit converter, but that is just for the software API (Application Programming Interface) provided. The conversion is always 12 bits and the DMA access is always 16 bits. Transfer to the next stage of our RMS measurement is via DMA, but you may do better to insert a hold in the logical sequence. The next blog will deal with the squaring of the data, but it is easier to work with preprogrammed constant data rather than with the vagaries of a continuously changing ADC output. The approach in this blog has been rather conventional. Hold onto your hats for the Digital Frequency Block.