Wrong values from ADC

Hi there!

I’m building a frequency meter using as input a sine wave between 0.05V-0.55V (from a peak detector) and a frequency range between 5Khz-10Khz. I tried to change the sampling rate (I need at least 20Khz) to 76Khz (setting 16 prescaler from ADCSRA register), but the resulting data from ADC it’s a constant (=255). I didn’t find anything which could cause this constant output.

Having a higher sampling rate means that I could find more precisely the max amplitudes and then I could measure the time period between two max amplitudes resulting the frequency.

Any help or tips would be much appreciated! Thanks in advance!

const int MAX_RESULTS = 256;
volatile int results [MAX_RESULTS];
volatile int resultNumber;

void setup() {
ADMUX = 0x20; // set A0 analog input
ADCSRA = 0xEC;// set 16 prescaler, enable ADC and interrupts and start ADC measurements
Serial.begin(115200);
}

ISR (ADC_vect)
{
Serial.println(ADCH);
results[resultNumber++] = ADCH;
if(resultNumber == MAX_RESULTS)
{
ADCSRA = 0; // turn off ADC
}

}

void loop () {
while (resultNumber < MAX_RESULTS)
{ }

for (int i = 0; i < MAX_RESULTS; i++)
{
Serial.println (results );

  • }*
  • resultNumber = 0;*
  • ADCSRA = 0xEC;*
    }

Don’t do serial I/O in interrupt context and don’t read ADCH twice.
Fix that and get back to us.

Please remember to use code tags when posting code.

razvant19:
Hi there!

I’m building a frequency meter using as input a sine wave between 0.05V-0.55V (from a peak detector) and a frequency range between 5Khz-10Khz. I tried to change the sampling rate (I need at least 20Khz) to 76Khz

If your highest frequency is 10kHz, you do not need to sample any higher than 20kHz. Goggle “nyquist sampling theorem”

And use code tags (</>) while posting codes:

const int MAX_RESULTS = 256;
volatile int results [MAX_RESULTS];
volatile int resultNumber;

void setup() {
  ADMUX = 0x20; // set A0 analog input
  ADCSRA = 0xEC;// set 16 prescaler, enable ADC and interrupts and start ADC measurements
  Serial.begin(115200);
}

  ISR (ADC_vect)
{
    Serial.println(ADCH);
    results[resultNumber++] = ADCH;
    if(resultNumber == MAX_RESULTS)
    {
      ADCSRA = 0;  // turn off ADC
    }

}

void loop () {
  while (resultNumber < MAX_RESULTS)
    { }

 for (int i = 0; i < MAX_RESULTS; i++)
  {
    Serial.println (results );
  }
  resultNumber = 0;
  ADCSRA = 0xEC;
}

TheMemberFormerlyKnownAsAWOL:
Don't do serial I/O in interrupt context and don't read ADCH twice.
Fix that and get back to us.

Please remember to use code tags when posting code.

blh64:
If your highest frequency is 10kHz, you do not need to sample any higher than 20kHz. Goggle "nyquist sampling theorem"

That's why I wrote 20Khz (having in mind the Nyquist sampling theorem)

TheMemberFormerlyKnownAsAWOL:
Don't do serial I/O in interrupt context and don't read ADCH twice.
Fix that and get back to us.

Please remember to use code tags when posting code.

I tried this but nothing changed.

blh64:
If your highest frequency is 10kHz, you do not need to sample any higher than 20kHz. Goggle "nyquist sampling theorem"

Theoretically, 2 times is enough; practically, it is done 5 to 10 times in some critical measurements.

razvant19:
I tried this but nothing changed.

Post your new/changed codes so that we can if you done things correctly. Why have you read only ADCH and why not both ADCL and ADCH?

razvant19:
I tried this but nothing changed.

I can’t see your code.
I can’t see your results

TheMemberFormerlyKnownAsAWOL:
I can’t see your code.
I can’t see your results

const int MAX_RESULTS = 256;
volatile int results [MAX_RESULTS];
volatile int resultNumber;

void setup() {
  ADMUX = 0x20; // set A0 analog input
  ADCSRA = 0xEC;// set 16 prescaler, enable ADC and interrupts and start ADC measurements
  Serial.begin(115200);
}

  ISR (ADC_vect)
{
    results[resultNumber++] = ADCH;
    if(resultNumber == MAX_RESULTS)
    {
      ADCSRA = 0;  // turn off ADC
    }

}

void loop () {
  while (resultNumber < MAX_RESULTS)
    { }

 for (int i = 0; i < MAX_RESULTS; i++)
  {
    Serial.println (results[i]);
  }
  resultNumber = 0;
  ADCSRA = 0xEC;
}

The output: Dropbox - Error
The input: Dropbox - Error