reading analog input at higher frequency?

Hey, i am measuring sine wave, basically AC reduced by transformer and with some offset. when i try to measure the voltages with simple analogRead() function, it's too slow ofc and only some of the values are gathered. So i wanted to ask if there is some simple way to measure this wave in a way that plot of the gathered samples looks like a sine wave and it's slow enough for me to read?

analogRead() takes about 100us to obtain a value, so you can sample at approximately 10kHz. Is that not fast enough for you? AC line frequency is much slower than that.

The theoretical sampling rate is 2 times (the Nyquist Criterion) of the band-width of the input signal which is here 50 Hz line frequency. Prcatically, the sampling rate is set at about 10 times of the band-width. So, if you sample the input signal at 500 Hz (10x50 = 500) then the DAC should produce a signal that resembles the input signal. Too much samplings insert unwanted noises that are hard to filter out.

yeah i was probably a bit mistaken. Though to be absolutely exact, i think it'S better i'll just show you what i am seeing.
This is the "plot" i am seeing, this is obviously wrong. I think it's because i am doing something wrong with reading the values. It just seems too simple to work like i expect it to.

(This is my code btw.)

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(analogRead(A0));
}

I ran a simple 'for' loop and got an 8.9 kHz sample rate (112.19 microseconds per sample). About 300 samples will get you two full cycles of 60 Hz. Use Tools->Serial Plotter to view the graph.

const int SampleCount = 300;  // At 8900 samples per second, about 2 cycles of 60 Hz.
int Samples[SampleCount];


void setup()
{
  Serial.begin(115200);


  unsigned long startTime = micros();
  for (int i = 0; i < SampleCount; i++)
  {
    Samples[i] = analogRead(A0);
  }
  unsigned long stopTime = micros();


  for (int i = 0; i < SampleCount; i++)
  {
    Serial.println(Samples[i]);
  }
  // Serial.println(stopTime - startTime);
  // Serial.println((stopTime - startTime) / (float)SampleCount);
}


void loop() {}

Serial.begin(9600);Oops

Your version works much better at 115200 baud. 9600 baud is way too slow to sample 60 Hz signals.

Yeah, this was exactly what i was looking for. Problem solved, thanks alot :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.