1 2 3 Previous Next 32 Replies Latest reply on Jan 16, 2020 2:27 AM by EvPa_264126

    bounces happened at comparator output


      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?




      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?


        • 1. Re: bounces happened at comparator output


          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.



          • 2. Re: bounces happened at comparator output

            Additionally, I was forced to add:

            • 3. Re: bounces happened at comparator output



              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.



              • 4. Re: bounces happened at comparator output


                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.



                • 5. Re: bounces happened at comparator output


                  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.





                  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.


                  • 6. Re: bounces happened at comparator output


                    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[i] x A[i] x G[i]) / SUM(G[i])


                    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[i] * ST[i];
                         MS += GS * GS;

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

                    RMS = sqrt(MS/WG);

                    • 7. Re: bounces happened at comparator output



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

                      • 8. Re: bounces happened at comparator output


                        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) )


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


                        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[i] = Y;                // output data array for visualisation


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

                                  SumG +=Yg;         // total Gaussian weight



                             RMS = sqrt(SumC / SumG)

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


                        • 9. Re: bounces happened at comparator output

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

                          • 10. Re: bounces happened at comparator output


                            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




                            Figure 1. Project schematic.


                            Figure 2. Project timing diagram


                            Figure 3. Optional arbitrary signal generator.


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


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


                            Figure 6. UART terminal output.


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

                            1 of 1 people found this helpful
                            • 11. Re: bounces happened at comparator output



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


                              I appreciated your time and effort.

                              • 12. Re: bounces happened at comparator output


                                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.



                                • 13. Re: bounces happened at comparator output


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


                                  • 14. Re: bounces happened at comparator output


                                    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.



                                    Figure 1. Project schematic.


                                    Figure 2. Comparator settings.


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


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


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


                                    1 2 3 Previous Next