# Measuring an RMS value on a PSoC5, Part 1: Signal Acquisition

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

Introduction

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.

Signal Input

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.

Figure 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.

Figure 2

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.

## 13 comments on “Measuring an RMS value on a PSoC5, Part 1: Signal Acquisition”

1. David Ashton
May 11, 2016

Thanks Aubrey, always good to see you on these pages.  Nice understandable explanation.  And reinforces my resolve that I MUST get into the PSOCs (when I have done all the other ten million things hanging over me…. 🙂

2. dmunson917
May 12, 2016

I found this document on the Cypress website:

Precision Rectifier Using Mixer – PSoC
®

3/PSoC 5
Project Name: Precision Rectifier
Programming Language: C
Associated Part Families:CY8C38xx/CY55xx
Software: PSoC
®
Creator™
Related Hardware: CY8CKIT-001
Prerequisites: Example Project: Using System Wide Resources
Author: Praveen Sekar

June 3, 2010 Document No. 001-61919 Rev. **

3. antedeluvian
May 12, 2016

dmunson

I found this document on the Cypress website:

Thanks. I poked around and I see that it looks like the component will in fact work- it's just that its features (and appearance in setup) have expanded. It certainly is a different and interesting approach, but in my case would still require 3 external components (2 resistors and an AC coupling capacitor) in order to bias the input signal to 1/2 rail.

I gave up too easily, but perhaps the simpler approach is better when you see quite how complicated the Digital Filter Block is, in Part 2.

4. antedeluvian
May 12, 2016

David

always good to see you on these pages .

Ditto

Nice understandable explanation.

Thanks. This was the easy bit. I hope you say the same thing after the 2nd part.

And reinforces my resolve that I MUST get into the PSOCs

You know where I am if you need advice.

5. dmunson917
May 12, 2016

You can also create a precision full wave rectifier in a PSoC 1:

The PSoC 1 has several types of configurable continuous time filters (constructed from switched capacitor blocks) available.

6. antedeluvian
May 12, 2016

dmunson

The PSoC 1 has several types of configurable continuous time filters

There are some things I miss from the PSoC1.

I remember working with Ganesh (the author of the blog you referred to) on resolving an issue I had with the CRC generator on the PSoC1. I actually acknowledged his contribution in my article “Create Modbus Slave” in the July 2008 issue of Circuit Clellar.

7. Victor Lorenzo
May 14, 2016

A nice and good one as always.

If I ever had a time I will give a try to PSoCs and this seems like a good starting point.

Thanks!

8. antedeluvian
March 15, 2017

Unfortunately Dropbox has chnaged their implementation of public files. This has affected many, many instances where I have linked to my files. I am trying to update the links as I come across them. Dropbox will ask you to register in order to copy the file or open the file (you can only open the file once you log in)

In this blog this is the revised link to the project RMS1

9. UdyRegan
October 22, 2018

David – I can totally get where you are coming from. It's really hard to start going into something new when there is just so many things lining up for your attention isn't it! Thanks Aubrey for this article though. And for the update about your files. I'll definitely be referring back to these couple of articles for a while to come so thanks for including in the links for us

10. EdwardThirlwall
February 26, 2019

It seems that there are indeed various angles to look at this problem. Sometimes it does take an expert to truly see many more angles that a single problem could be solved from. It won't be long before I bet you would be finding even more possible ways to tackle this single equation.

11. Edward barr
April 7, 2022

Superhero Jackets

12. Harris Fox
April 7, 2022

Very interesting. I wish to see much more like this. Thanks for sharing your information!
Venom Outfits Collection

13. Melvin150
April 7, 2022

This blog is complete all you want to know.
Spiderman

This site uses Akismet to reduce spam. Learn how your comment data is processed.