Search code examples
arduinointerruptsamplingatmegaadc

How to set Arduino Mega 2560 ADC sampling frequency to 40 Hz


I want to filter my ADC. I need my ADC to have sampling frequency at 40 Hz, but I don't know how to set the ADC sampling frequency.


Solution

  • It sounds like you want to read the ADC every 25 milliseconds. Since 25 milliseconds is much longer than the time it will take to read the ADC, you don't need to worry about configuring the ADC in any kind of special way: just call analogRead every 25 milliseconds. You could do that with some code like this:

    uint8_t lastReadingTime;
    
    void loop()
    {
      if ((uint8_t)(millis() - lastReadingTime) >= 25)
      {
        lastReadingTime = millis();
        uint16_t result = analogRead(A0);
        // now do something with the result
      }
    }