cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 5, 3 & 1 MCU

user_3716231
New Contributor II

i want to measure frequency of AC signal.

the problem is showed below.the blue waveform is from comparator output.the yellow one is cennected to output of sensor.

how to eliminate the bounces?

Vfreq.jpg

the blue one of upper picture is  'comV' which drive mode is set to Resistive Pull Up & Down.

the underneath one is set to Strong Drive.why are they different?

strongdrive.PNG

1 Solution
EvPa_264126
Valued Contributor II

Additionally, I was forced to add:
cmp.JPG

View solution in original post

0 Likes
32 Replies
odissey1
Honored Contributor II

user_37..,

The signal appears to be low frequency, 1.5V + noise. Prior to sending it to comparator, I recommend to amplify it using Opamp. Use some high gain to saturate sine signal, making it practically a square wave and apply to a digital pin in HiZ mode. The Opamp_1 here serves as a comparator with low bandwidth, removing noise and digital ringing. Pin_4 serves as a comparator, converting analog square wave into digital signal.

/odissey1

Sine-to-Digital_01a_A.png

0 Likes
EvPa_264126
Valued Contributor II

Additionally, I was forced to add:
cmp.JPG

View solution in original post

0 Likes
user_3716231
New Contributor II

/odissey1,

sorry for the late reply.

i don't understand why this circuit can remove noise and digital ringing.is it high pass filter of RC configuration?

i applied opa method but the waveform seem to be abnormal.

(sine wave-point A , square wave - 'out1')

thanks your help.

sean

opa_method.PNGACsignal-out1.PNG

0 Likes
odissey1
Honored Contributor II

Sean,

I tested some zero-cross schemes and simple Comparator seems to work the best. Project is attached. Several important settings to avoid glitches: Comparator speed (slow), hysteresis (enable); input clock (enable, 1 MHz); VRef (1.024 V). With these settings, AC signal is captured correctly down to ~10mV p-p (1-bit @ 1.02V scale), even with unstable power to PSoC.

/odissey1

Figure 1. Project schematic.

ZeroCross_01a_basic_A.png

Figure 2. Comparator settings.

ZeroCross_01a_basic_B.png.png

Figure 3. KIT-059 pins assignment. Note the onboard C12 used to bypass VRef.

ZeroCross_01a_basic_C.png

Figure 4. Scope traces: Yellow - 60Hz AC input 2.04V p-p, Cyan - AC reference, Fuchsia - Comparator output.

ZeroCross_01a_basic_D1.png

Figure 5. Scope traces: Yellow - 60Hz AC input ~10 mV p-p, Cyan - AC reference, Fuchsia - Comparator output.

ZeroCross_01a_basic_D2.png

0 Likes
user_3716231
New Contributor II

/odissey1,

1.how to determine the value of R and C ?what is the function of each passive component?i applied different capacitor and resistor then RMS is different between oscilloscope measurement and terminal output.

RMS_RC.PNGRMS_RC0.PNGRMS_RC1.PNG

2.when the amplitude is set to greater than 1,RMS is different between them as well even though i applied your project directly.

RMS__amp.PNGRMS_amp2.PNG

3.i have no idea about comparator output.

zerocross.PNG

0 Likes
odissey1
Honored Contributor II

The input filter cut-off frequency

Fc = 1/(2π R1 C1)

must be much lower than the input signal frequency (50-60Hz). You need to increase the C1 to 1-10uF. The C12 is for buffering of Vref, 0.1  - 1.0uF will work,  I used KIT-059 onboard capacitor on pin_02 (1uF). Larger value will take longer time to be charged by the unbuffered Vref.

In the demo the DelSig-ADC range selected is Vref +/- 1.024V. The peak amplitude of the input signal should be kept below 1V to avoid clipping, alternatively, the ADC input range should be increased (e.g. +/- 2.048 V).

(3). I suspect some ground loop. I used internal VDAC to generate Sine wave. I'll try external gen to see the difference.

/odissey1

0 Likes
Len_CONSULTRON
Honored Contributor II

sean,

I assume when you are trying to measure an "AC" signal, you're trying to measure power line AC.  (This is based on your scope plots above).

Power Line AC is notorious for having non-fundamental artifacts.  This is because not only are your powering your devices off of it, other people outside your environment are also.  Devices being turned on and off and motor loading noise and other line-induced noise show up on your AC power.  This is called Conducted Emissions (CE for short).

To measure the AC fundamental, you should use a "notch filter".  However a low-pass filter (LPF) might be sufficient since most of the line-induced noise tends to be > 60Hz.   Others on the forum might help you to create a good pass filter like the one /odissey1 or Evgeniy defined.

I'm curious, is your ultimate goal to create a "zero-crossing" detector?  I'm noticing on your plots your comparator switches near the zero point.

If so, there might be better (simpler) techniques.

In a design of a zero-crossing detector, you need to decide why you need to detect the zero-crossing point.  If it is to control a high current load device and you want to have the least impact to the line for causing load-induced noise, you need a zero-crossing current detector.  This measures when the current is near zero.  The power company tries to have the voltage in-phase with the current but downstream devices (usually industrial devices) sometimes skew the current phase.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
user_3716231
New Contributor II

len,

sorry for the late reply.

i currently detected zero cross by software to calculate RMS but the frequency and power factor must depend on hardware.

comparator and opa are the ways i can come up with.if you could share other ideas ,i will appreciate.

Evgeniy,

sorry for the late reply.

hysteresis was successful,but the bounces still occured.i add RC LPF in the circuit.it still needs a few day to observe.

thanks your help.

sean

0 Likes
odissey1
Honored Contributor II

Sean,

To calculate RMS there is another technique, which doesn't require period detection. It may work if RMS data needed not every period, but, say, once per second. It is very simple:

1. Prepare array of Gaussian distribution G[1000]

2. Accumulate about 1000 ADC reading  at rate 1 kHz A[1000].

3. Calculate RMS:

RMS = SUM(A x A x G) / SUM(G)

Here is pseudo-code (needs testing):

double GW[640];   // Gaussian coefficients = exp(-a*t^2)
double WG;           // Weight of the Gaussian window
double MS = 0.0;   // Mean Square
double RMS;         // Root Mean Square
double ST[640*2]; // Source Signal from ADC
double GS;            // temp var

int i;
for (i = 0; i < 640; i++) {
     GS = GW * ST;
     MS += GS * GS;
}


for (; i < 640*2; i++) {
     GS = GW[640*2-1-i] * ST;
     MS += GS * GS;
}


RMS = sqrt(MS/WG);

0 Likes
user_3716231
New Contributor II

/odissey1,

how to decide  the value of variable 'a' and 't' of gaussian coefficient and 'double  WG'?

0 Likes
odissey1
Honored Contributor II

Sean,

Attached are simulation charts. ADC samples simulated by the Cosine (10 periods), and weighting function by the Gaussian, both calculated over 1001 points (1000 intervals).

Cosine = Ac * cos( 2 * PI * Freq *  X )

Gauss = Ag * exp( -(X * X) / (2 * sigma *  sigma) )

where:

Ac = 1, Freq = 10, Ag = 1, sigma = 125.

All calculations made in double precision. Having 1000 points is overkill (100 samples per period), 200 points should suffice (20 samples/period).

Layout0.png

Calculated RMS was 0.707106, which matches well theoretical value (1/sqrt(2)).

The pseudocode (IGOR) is provide below:

Function MeanSqr(Y2)

     Wave Y2                     // output curve into Y2 wave

     Variable numPoints;   // number of ADC samples

     Variable X, Y,  Yc, Yg, step

     Variable SumC           // integral over Weighed cosine

     Variable SumG           // integral over Gaussian weightin function

     Variable RMS             // Root Mean Square result RMS = sqrt(SumC/SumG)

     Variable Xmin =  0;

     Variable Xmax = 1001 // NumPoints

     Variable Xo = 500;      // center point

     Variable Ag = 1             // Amplitude of the Gaussian weighting function = Ag * exp( -(x*x) / (2 * sigma *  sigma) )

     Variable sigma = 125   // sigma width of the Gaussian function

     Variable Ac = 1;           // Amplitude of the Cosine (ADC samples)  = Ac * cos( 2 * PI * Freq *  X )

     Variable Freq = 10;      // Frequency of Cosine

     numPoints = 1001

     SumC=0; SumG=0;

     step = (Xmax-Xmin) / (numPoints-1);

     Variable i

     for (i = 0; i < numPoints; i += 1)

          X = Xmin + step * i;

          X = X - Xo;

          Yc = Ac * cos( 2 * PI * Freq *  X ) ; // ADC samples

          Yg = Ag * exp( -(X*X) / (2 * sigma *  sigma) ) ; // weighting function

          Y = Yc^2 * Yg;

          Y2 = Y;                // output data array for visualisation

          SumC +=Y;           // Product of Cosine and Gaussian weight

          SumG +=Yg;         // total Gaussian weight

     endfor

     RMS = sqrt(SumC / SumG)

     Print SumC, SumG, RMS      //  output: 156.498  312.996  0.707106 (RMS=0.707106)

End

0 Likes
EvPa_264126
Valued Contributor II

Strange signal behavior - it looks like improper inclusion of comparator feedback ...
POS.png

0 Likes
odissey1
Honored Contributor II

Sean,

Attached is demo project showing RMS calculation using weighting function. The incoming AC signal is sampled by DeltaSigma ADC (16-bit)   and transferred to the RAM Buffer using DMA. Once Buffer is filled ADC is stopped and Buffer data is processed. RMS value calculated using weighting function (Gaussian) as discussed above.

Notice that AC signal and ADC sampling are not synchronized. ADC sampling rate is 1kHz, RAM Buffer is 201 samples. ADC conversion stops upon filling RAM buffer and Buffer data processing begins. ADC conversion restarts after RMS calculation is complete. Resulting RMS results update rate is approx. 5 Hz.

Project includes optional arbitrary signal generator, which can be safely removed if external signal generator is available. It requires following custom components:

*   DDS32_v0_0                  : Afbitrary frequency DDS generator. https://community.cypress.com/message/158566#158566

*   WaveGen8_v0_0 (beta) : Arbitary shape wave generator (included into this project)

*   QuadDec_SW_v0_1      : Quad decoder w/button swich. https://community.cypress.com/thread/30654

Other optional custom component used in the project:

*   PSoC Annotation Library_v1_0: https://community.cypress.com/message/204321

/odissey1

Figure 1. Project schematic.

RMS_dsADC_wFunc_01a_A.png

Figure 2. Project timing diagram

RMS_dsADC_wFunc_01a_D.png

Figure 3. Optional arbitrary signal generator.

RMS_dsADC_wFunc_01a_B1.png

Figure 4. Connection diagram to CY8CKIT-059 using PSoC Annotation Library_v1_0.

RMS_dsADC_wFunc_01a_C.png

Figure 5. Scope screenshot. Yellow trace - AC signal input; Cyan - signal reference; Fuchsia - measured RMS voltage output.

RMS_dsADC_wFunc_01a_E1.png

Figure 6. UART terminal output.

RMS_dsADC_wFunc_01a_F.png

P.S. Attached project is updated (StopWatch component is removed).

user_3716231
New Contributor II

/odissey1,

it seems there are too many things that i have to learn.

I appreciated your time and effort.

0 Likes
odissey1
Honored Contributor II

Sean,

Attached is stripped down version of the project (without signal generator and other external components). This may help you if external signal generator is available.

/odissey1

RMS_dsADC_wFunc_01a_C1.png

0 Likes
odissey1
Honored Contributor II

Sean,

Improved version of the RMS detector project is uploaded to the Community section:

RMS measurement using window function

It uses double-buffering technique to have the ADC sampling continuously (uninterrupted).

/odissey1

0 Likes
user_3716231
New Contributor II

i measure the frequency of sine wave with 60hz,but why will the value sometimes be 61hz?(there are no bounces)

i applied the clock source with 60hz instead of comparator output,and the value is fixed to 60hz.

and i observed the output 'comV' on the oscilloscope and its edge location would shift.

clock.PNG

Tout.png

0 Likes
odissey1
Honored Contributor II

did edge shifts when input signal is  sine or digital clock?

Please attach project archive.

0 Likes
user_3716231
New Contributor II

the edge shift occured at comparator output no matter the sine wave source is from power line or wave generator and both sine wave are fixed.

0 Likes
odissey1
Honored Contributor II

user_37..,

It looks like the frequency measurement is done per each period, so there could be jumps related to the noise on sine wave. I suggest to measure AC frequency over several periods to reduce this error. As a solution you can use a frequency meter component from this project

Re: HELP, with example of frequency measurement Error

which measures frequency over several full periods within 100ms window (~5 periods).

/odissey1

Frequency+meter_2x_1a.png

0 Likes
Len_CONSULTRON
Honored Contributor II

Sean,

What is the ultimate source of the wave you are measuring?  Is it the incoming line AC?

If so, you cannot trust the frequency to be absolutely accurate.   Most times the frequency is controlled by the power generating plant probably within +/10%.  It's a complex setup where the direct generation of the frequency is usually caused by steam engines coupled onto motors turned into generators.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
user_3716231
New Contributor II

len,

even though the sine wave is from wave generator,the frequency that i measure is still extra 1hz.

but my oscilloscope is able to measure it accurately .AC power line as well.

0 Likes
EvPa_264126
Valued Contributor II

The UDB Counter is implemented as a synchronous counter that uses the clock input only as a synchronization clock.

The pwm signal has a frequency of 1 Hz so the result will be 60 or  61

odyssey 1 already gave a link

I can't find the LCD_I2C v_02 component in my  PSoC Creator 4.2

It seems necessary to get rid of the high-frequency debris in the signal.

Use the RC filter at the input. 

Evgeniy

F.png

0 Likes
user_3716231
New Contributor II

thanks your help, /odissey1 and Evgeniy.

your answers are helpful for me.

but the phase difference of two sine waves which i set 30 degree from wave generator,it is not accurate on terminal output.

when the waveforms are square waves,the error of phase difference is only about 0.02 degree.i think it is because of edge shifted at comparator output.are there any solutions?

and i attach lcd_i2c componet.you could download it if you need it.

0 Likes
odissey1
Honored Contributor II

user_371,

I can't open archives. To send project use build-in function: (1) Select project in the Project Explorer Panel; (2) File->Create Workspace Bundle-> Minimal. (3) In created zip Archive delete Generated_Source folser to save space.

/odissey1

0 Likes
user_3716231
New Contributor II

i updated the file.

0 Likes
EvPa_264126
Valued Contributor II

Sync comparator improves noise immunity, but adds delay.

To measure the phase difference, you have to abandon the synchronization of the comparator, use the identical settings of the comparators.

For example, use a common stable Vref, set the same hysteresis and, possibly, amplify the signal to increase the steepness of the wawe at the input of the comparator.

The picture shows how the change in the resistor R6 from 100K to 50K affectsF2.jpg

0 Likes
user_3716231
New Contributor II

i don't know why the phase difference is greater than 30(both sine waves have the same amplitude 2V).i've already have the same setting of compatrator and hardware hysteresis.

(by the way,i tried to change one of the amplitude of sine wave,the phase difference changed too.)

cmp_set.PNG

擷取.PNG

0 Likes
EvPa_264126
Valued Contributor II

Do we use different projects?

I do not see the parameter f1 and d1 in the last project.

C1 and C2 are very large. They can be very different and introduce a delay.

I created a test project, let's compare the result

0 Likes
user_3716231
New Contributor II

i just add the below code in my last project.

        if(freq < 59.5 || freq > 60.5)

            freq1 = freq;

        if(phase < 29 || phase > 30.5)

            phase1 = phase;

       

        sprintf(ch,"freq:%.2f   f1:%.2f,  deg:%.2f    d1:%.2f \r\n",freq, freq1, phase, phase1);

however,i applied the test project directly but the same thing still happened even though i removed the RC filter.

0 Likes
EvPa_264126
Valued Contributor II

Extremely simplified and added a generator with a fixed phase shift.

Actual phase may vary due to different components starting times.

0 Likes