How to take care of noise for Peak-to-Peak detection of sine wave??

Hello

I have been working on taking sensor output data.

The sensor produce two sinusoidal outputs one is reference and one is active signals, 2.5Hz.

I found max and min value of sine wave to get the peak-to-peak value.

However, there are so much noise on peak-to-peak voltage graph as you can see on the graph I attached.

Here is the code I used.

const int sampleWindow = 4000; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
unsigned int sample2;
unsigned long time; 

void setup() 
{
  Serial.begin(57600);
  //analogReference(INTERNAL1V1);
}


void loop() 
{
  unsigned long startMillis= millis();  // Start of sample window
      /*
      Serial.println();
      Serial.println(startMillis);
  */
  unsigned int peakToPeak_ACT = 0;   // peak-to-peak level
  unsigned int peakToPeak_REF = 0; // peak to peak REF
  
  unsigned int ACT_Max = 0;
  unsigned int ACT_Min = 1024;
  unsigned int REF_Max = 0;
  unsigned int REF_Min = 1024;
  // collect data for 50 mS
  while (millis() - startMillis < sampleWindow)
  {
     sample = analogRead(2);
     sample2 = analogRead(3);

     if (sample < 1024 and sample2 < 1024)  // toss out spurious readings
     {
        if (sample > ACT_Max and sample2 > REF_Max)
        {
           ACT_Max = sample;  // save just the max levels
           REF_Max = sample2;
             /*
               Serial.print("MAX ");
               Serial.print("\t");
               Serial.println(signalMax*0.00488);
           delay(1);
           */
        }
        else if (sample < ACT_Min and sample2 < REF_Max)
        {
           ACT_Min = sample;  // save just the min levels
           REF_Min = sample2;
        }
     }
  }
  
  peakToPeak_ACT = ACT_Max - ACT_Min;  // max - min = peak-peak amplitude
  peakToPeak_REF = REF_Max - REF_Min;
  
  delay(1);

}

Do you guys think this noise comes because of the Arduino code?

or the signal itself does not stable enough? When I see the oscilloscope the two sine wave look stable there is no noise.

Any advice would help. Thank you

MODERATOR ADDED CODE TAGS.

if (sample < 1024 and sample2 < 1024)

What programming language is it?

Magician:
What programming language is it?

Well. I just used it and it works on Arduino..... it gave me no error..

So any comments about the noise??

Do you guys think this noise comes because of the Arduino code?

or the signal itself does not stable enough? When I see the oscilloscope the two sine wave look stable there is no noise.

In my experience when working with analog signals to be read by Arduino, I usually test with external power supply for Arduino not the USB cable. I noticed differences in reading using external supply compared to USB cable attached to your laptop or computer.

dozcoov:
In my experience when working with analog signals to be read by Arduino, I usually test with external power supply for Arduino not the USB cable. I noticed differences in reading using external supply compared to USB cable attached to your laptop or computer.

Do you know what happen inside Arduino when you plug external power supply with USB?

Does Arduino cut the power from USB and use USB only for data transfer and completely depends on external power supply?

Tell us more about the sensor.
What is the t/t output voltage of the sensor.
How much of the A/D range does it use.
Is the output impedance less than 10k.
Leo..

+1 for telling us more about the sensor, and all the wiring. Also, please post code using code tags ("</>" button).

There are many sources of noise, such as whatever is providing the signal, exposed unshielded wires and sensors, breadboards, USB and wall wart power packs, etc.

Batteries are the least noisy source of power and a battery powered Arduino is less susceptible to environmental noise, for example from AC house wiring.

if (sample < 1024 and sample2 < 1024) // toss out spurious readings
{
if (sample > ACT_Max and sample2 > REF_Max)
{

Try using && instead of "and"

The IDE may not give you an error, but that doesn't mean your program is functioning as you intend it to function.

jremington:
+1 for telling us more about the sensor, and all the wiring. Also, please post code using code tags ("</>" button).

There are many sources of noise, such as whatever is providing the signal, exposed unshielded wires and sensors, breadboards, USB and wall wart power packs, etc.

Batteries are the least noisy source of power and a battery powered Arduino is less susceptible to environmental noise, for example from AC house wiring.

It is NDIR CO2 sensor from Alphasense. It produces two AC signal about 2.5Hz.

I attached the schematic of the readout circuit. The two Active and Reference outputs are directly connected to the Analog input of Arduino.

I used bread boards to build those lamp driving and readout circuits.

When I connected external power supply, the signal becomes stable but not much.

For Op-amp I used LM358.

I am trying different method but the results is not getting better yet.

TeslaIaint:
if (sample < 1024 and sample2 < 1024) // toss out spurious readings
{
if (sample > ACT_Max and sample2 > REF_Max)
{

Try using && instead of "and"

The IDE may not give you an error, but that doesn't mean your program is functioning as you intend it to function.

Thanks for the advice.

The schematic diagram from post#0 assumes a positive AND negative supply for the opamps.
If you have used a single 5volt supply, the circuit won't work.

You could try replacing R5, R6 with a (well decoupled) voltage divider, so the +inputs are at 2volt bias.
Leo..