FFT analysis with microphone max9814 using arduinoFFT library

Hey everyone, I've been working on a physical music visualizer project for a little bit but I am really struggling with getting proper realtime signal analysis using my max9814 microphone.

I have had a lot of trouble figuring out the arduinoFFT library, as the documentation is quite lacking.

Here is the code I have:

/* Pins */
const int MIC_IN_PIN = A0; // the pin the mic is using

/* Time Keeping */ 
unsigned long startSampleTime = 0;

/* FFT */ 
const int SAMPLE_COUNT = 1024;
const int SAMPLE_FREQ = 40000;
const long SAMPLE_INTERVAL_US =  round(1000000 * (1 / SAMPLE_FREQ));
const int binSize = round(SAMPLE_FREQ / SAMPLE_COUNT);

// FFT arrays
double fReal[SAMPLE_COUNT];
double fImag[SAMPLE_COUNT];

const int NoBins = 18;
int binValues[NoBins];

ArduinoFFT FFT = ArduinoFFT<double>(fReal, fImag, SAMPLE_COUNT, SAMPLE_FREQ);

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  while (!Serial) {
    // Blink led until serial is acquired
    digitalWrite(LED_BUILTIN, HIGH);  
    delay(1000);                      
    digitalWrite(LED_BUILTIN, LOW);   
    delay(1000);                      
  }

  // Pin mode settings
  pinMode(MIC_IN_PIN, INPUT);
}

void loop() {
  // Get samples from microphone
  for (int i = 0; i < SAMPLE_COUNT; i++) {
    startSampleTime = micros();
    fReal[i] = analogRead(MIC_IN_PIN);
    fImag[i] = 0.0;
    while (micros() < (startSampleTime + SAMPLE_INTERVAL_US)) { /* wait to finish sampling*/ }
  }

 /*  Fake simulated sine wave for testing
  // create sine wave with frequency of 1000
  // double ratio = twoPi * 1000 / SAMPLE_FREQ; // Fraction of a complete cycle stored at each sample (in radians)
  // for (uint16_t i = 0; i < SAMPLE_COUNT; i++)
  // {
  //   fReal[i] = int8_t(100 * sin(i * ratio) / 2.0);/* Build data with positive and negative values*/
  //   fImag[i] = 0.0; //Imaginary part must be zeroed in case of looping to avoid wrong calculations and overflows
  // }

  // testing the input to FFT
  for (int i = 0; i < SAMPLE_COUNT; i++) {
    Serial.print(fReal[i]);
    Serial.print(" ");
  }
  Serial.println("");

  // FFT Computation
  FFT.dcRemoval();
  FFT.windowing(FFTWindow::Hamming, FFTDirection::Forward);
  FFT.compute(FFTDirection::Forward);
  FFT.complexToMagnitude();

// print 18 bins
  for (int i = 1; i < SAMPLE_COUNT / 2; i++) {
  if ((i * binSize) / (5 * binSize) < 18) {
      binValues[(i*binSize) / (5 * binSize)] += (int)fReal[i];
     // collect 5 bins into 5 until all 18 bins are filled in binValues (only for debugging)
    }
  }

The FFT analysis works when i plug in the fake simulated sine wave, but when I use my microphone for sampling all the values seem random. I tried playing a frequency on my phone to the microphone but it didn't match the bin of the frequency at all. All the values increased instead.
I checked the input I give by printing it to Serial. The microphone values all seem to be around 400. I am unsure what the issue is with my sampling. I would appreciate any help.
Here is the output:

Sine wave
0.00 20.00 37.00 47.00 49.00 42.00 28.00 8.00 -12.00 -31.00 -44.00 -49.00 -46.00 -34.00 -16.00 3.00 24.00 39.00 48.00 48.00 40.00 24.00 4.00 -16.00 -34.00 -46.00 -49.00 -44.00 -31.00 -13.00 7.00 27.00 42.00 49.00 48.00 38.00 21.00 0.00 -19.00 -36.00 -47.00 -49.00 -43.00 -28.00 -9.00 11.00 30.00 44.00 49.00 46.00 35.00 17.00 -3.00 -23.00 -39.00 -48.00 -49.00 -40.00 -25.00 -5.00 15.00 33.00 45.00 49.00 45.00 32.00 13.00 -7.00 -26.00 -41.00 -49.00 -48.00 -38.00 -21.00 -1.00 19.00 36.00 47.00 49.00 43.00 29.00 10.00 -10.00 -30.00 -43.00 -49.00 -47.00 -35.00 -18.00 2.00 22.00 39.00 48.00 49.00 41.00 26.00 6.00 -14.00 -33.00 -45.00 -49.00 -45.00 -33.00 -14.00 6.00 26.00 41.00 49.00 48.00 39.00 22.00 2.00 -18.00 -35.00 -47.00 -49.00 -43.00 -30.00 -10.00 10.00 29.00 43.00 49.00 47.00 36.00 19.00 -1.00 -21.00 -38.00 -48.00 -49.00 -41.00 -26.00 -7.00 13.00 32.00 45.00 49.00 45.00 33.00 15.00 -5.00 -25.00 -40.00 -49.00 -48.00 -39.00 -23.00 -3.00 17.00 35.00 46.00 49.00 44.00 30.00 11.00 -9.00 -28.00 -43.00 -49.00 -47.00 -36.00 -19.00 0.00 21.00 38.00 48.00 49.00 42.00 27.00 7.00 -13.00 -31.00 -44.00 -49.00 -46.00 -34.00 -16.00 4.00 24.00 40.00 48.00 48.00 39.00 24.00 3.00 -16.00 -34.00 -46.00 -49.00 -44.00 -31.00 -12.00 8.00 28.00 42.00 49.00 47.00 37.00 20.00 0.00 -20.00 -37.00 -47.00 -49.00 -42.00 -28.00 -8.00 12.00 31.00 44.00 49.00 46.00 34.00 16.00 -3.00 -24.00 -39.00 -48.00 -48.00 -40.00 -24.00 -4.00 16.00 34.00 46.00 49.00 44.00 31.00 13.00 -7.00 -27.00 -42.00 -49.00 -48.00 -38.00 -21.00 0.00 19.00 36.00 47.00 49.00 43.00 28.00 9.00 -11.00 -30.00 -44.00 -49.00 -46.00 -35.00 -17.00 3.00 23.00 39.00 48.00 49.00 40.00 25.00 5.00 -15.00 -33.00 -45.00 -49.00 -45.00 -32.00 -13.00 7.00 26.00 41.00 49.00 48.00 38.00 21.00 1.00 -19.00 -36.00 -47.00 -49.00 -43.00 -29.00 -10.00 10.00 30.00 43.00 49.00 47.00 35.00 18.00 -2.00 -22.00 -39.00 -48.00 -49.00 -41.00 -26.00 -6.00 14.00 33.00 45.00 49.00 45.00 33.00 14.00 -6.00 -26.00 -41.00 -49.00 -48.00 -39.00 -22.00 -2.00 18.00 35.00 47.00 49.00 43.00 30.00 10.00 -10.00 -29.00 -43.00 -49.00 -47.00 -36.00 -19.00 1.00 21.00 38.00 48.00 49.00 41.00 26.00 7.00 -13.00 -32.00 -45.00 -49.00 -45.00 -33.00 -15.00 5.00 25.00 40.00 49.00 48.00 39.00 23.00 3.00 -17.00 -35.00 -46.00 -49.00 -44.00 -30.00 -11.00 9.00 28.00 43.00 49.00 47.00 36.00 19.00 0.00 -21.00 -38.00 -48.00 -49.00 -42.00 -27.00 -7.00 13.00 31.00 44.00 49.00 46.00 34.00 16.00 -4.00 -24.00 -40.00 -48.00 -48.00 -39.00 -24.00 -3.00 16.00 34.00 46.00 49.00 44.00 31.00 12.00 -8.00 -28.00 -42.00 -49.00 -47.00 -37.00 -20.00 0.00 20.00 37.00 47.00 49.00 42.00 28.00 8.00 -12.00 -31.00 -44.00 -49.00 -46.00 -34.00 -16.00 3.00 24.00 39.00 48.00 48.00 40.00 24.00 4.00 -16.00 -34.00 -46.00 -49.00 -44.00 -31.00 -13.00 7.00 27.00 42.00 49.00 48.00 38.00 21.00 0.00 -19.00 -36.00 -47.00 -49.00 -43.00 -28.00 -9.00 11.00 30.00 44.00 49.00 46.00 35.00 17.00 -3.00 -23.00 -39.00 -48.00 -49.00 -40.00 -25.00 -5.00 15.00 33.00 45.00 49.00 45.00 32.00 13.00 -7.00 -26.00 -41.00 -49.00 -48.00 -38.00 -21.00 -1.00 19.00 36.00 47.00 49.00 43.00 29.00 10.00 -10.00 -30.00 -43.00 -49.00 -47.00 -35.00 -18.00 2.00 22.00 39.00 48.00 49.00 41.00 26.00 6.00 -14.00 -33.00 -45.00 -49.00 -45.00 -33.00 -14.00 6.00 26.00 41.00 49.00 48.00 39.00 22.00 2.00 -18.00 -35.00 -47.00 -49.00 -43.00 -30.00 -10.00 10.00 29.00 43.00 49.00 47.00 36.00 19.00 -1.00 -21.00 -38.00 -48.00 -49.00 -41.00 -26.00 -7.00 13.00 32.00 45.00 49.00 45.00 33.00 15.00 -5.00 -25.00 -40.00 -49.00 -48.00 -39.00 -23.00 -3.00 17.00 35.00 46.00 49.00 44.00 30.00 11.00 -9.00 -28.00 -43.00 -49.00 -47.00 -36.00 -19.00 0.00 21.00 38.00 48.00 49.00 42.00 27.00 7.00 -13.00 -31.00 -44.00 -49.00 -46.00 -34.00 -16.00 4.00 24.00 40.00 48.00 48.00 39.00 24.00 3.00 -16.00 -34.00 -46.00 -49.00 -44.00 -31.00 -12.00 8.00 28.00 42.00 49.00 47.00 37.00 20.00 0.00 -20.00 -37.00 -47.00 -49.00 -42.00 -28.00 -8.00 12.00 31.00 44.00 49.00 46.00 34.00 16.00 -3.00 -24.00 -39.00 -48.00 -48.00 -40.00 -24.00 -4.00 16.00 34.00 46.00 49.00 44.00 31.00 13.00 -7.00 -27.00 -42.00 -49.00 -48.00 -38.00 -21.00 0.00 19.00 36.00 47.00 49.00 43.00 28.00 9.00 -11.00 -30.00 -44.00 -49.00 -46.00 -35.00 -17.00 3.00 23.00 39.00 48.00 49.00 40.00 25.00 5.00 -15.00 -33.00 -45.00 -49.00 -45.00 -32.00 -13.00 7.00 26.00 41.00 49.00 48.00 38.00 21.00 1.00 -19.00 -36.00 -47.00 -49.00 -43.00 -29.00 -10.00 10.00 30.00 43.00 49.00 47.00 35.00 18.00 -2.00 -22.00 -39.00 -48.00 -49.00 -41.00 -26.00 -6.00 14.00 33.00 45.00 49.00 45.00 33.00 14.00 -6.00 -26.00 -41.00 -49.00 -48.00 -39.00 -22.00 -2.00 18.00 35.00 47.00 49.00 43.00 30.00 10.00 -10.00 -29.00 -43.00 -49.00 -47.00 -36.00 -19.00 1.00 21.00 38.00 48.00 49.00 41.00 26.00 7.00 -13.00 -32.00 -45.00 -49.00 -45.00 -33.00 -15.00 5.00 25.00 40.00 49.00 48.00 39.00 23.00 3.00 -17.00 -35.00 -46.00 -49.00 -44.00 -30.00 -11.00 9.00 28.00 43.00 49.00 47.00 36.00 19.00 0.00 -21.00 -38.00 -48.00 -49.00 -42.00 -27.00 -7.00 13.00 31.00 44.00 49.00 46.00 34.00 16.00 -4.00 -24.00 -40.00 -48.00 -48.00 -39.00 -24.00 -3.00 16.00 34.00 46.00 49.00 44.00 31.00 12.00 -8.00 -28.00 -42.00 -49.00 -47.00 -37.00 -20.00 0.00 20.00 37.00 47.00 49.00 42.00 28.00 8.00 -12.00 -31.00 -44.00 -49.00 -46.00 -34.00 -16.00 3.00 24.00 39.00 48.00 48.00 40.00 24.00 4.00 -16.00 -34.00 -46.00 -49.00 -44.00 -31.00 -13.00 7.00 27.00 42.00 49.00 48.00 38.00 21.00 0.00 -19.00 -36.00 -47.00 -49.00 -43.00 -28.00 -9.00 11.00 30.00 44.00 49.00 46.00 35.00 17.00 -3.00 -23.00 -39.00 -48.00 -49.00 -40.00 -25.00 -5.00 15.00 33.00 45.00 49.00 45.00 32.00 13.00 -7.00 -26.00 -41.00 -49.00 -48.00 -38.00 -21.00 -1.00 19.00 36.00 47.00 49.00 43.00 29.00 10.00 -10.00 -30.00 -43.00 -49.00 -47.00 -35.00 -18.00 2.00 22.00 39.00 48.00 49.00 41.00 26.00 6.00 -14.00 -33.00 -45.00 -49.00 -45.00 -33.00 -14.00 6.00 26.00 41.00 49.00 48.00 39.00 22.00 2.00 -18.00 -35.00 -47.00 -49.00 -43.00 -30.00 -10.00 10.00 29.00 43.00 49.00 47.00 36.00 19.00 -1.00 -21.00 -38.00 -48.00 -49.00 -41.00 -26.00 -7.00 13.00 32.00 45.00 49.00 45.00 33.00 15.00 -5.00 -25.00 -40.00 -49.00 -48.00 -39.00 -23.00 -3.00 17.00 35.00 46.00 49.00 44.00 30.00 11.00 -9.00 -28.00 -43.00 -49.00 -47.00 -36.00 -19.00 0.00 21.00 38.00 48.00 49.00 42.00 27.00 7.00 -13.00 -31.00 -44.00 -49.00 -46.00 -34.00 -16.00 4.00 24.00 40.00 48.00 48.00 39.00 24.00 3.00 -16.00 -34.00 -46.00 -49.00 -44.00 -31.00 -12.00 8.00 28.00 42.00 49.00 47.00 37.00 20.00 0.00 -20.00 -37.00 -47.00 -49.00 -42.00 -28.00 -8.00 12.00 31.00 44.00 49.00 46.00 34.00 16.00 -3.00 -24.00 -39.00 -48.00 -48.00 -40.00 -24.00 -4.00 16.00 

Mic Values
392.00 393.00 364.00 411.00 360.00 359.00 384.00 393.00 383.00 378.00 410.00 391.00 413.00 378.00 382.00 372.00 400.00 375.00 383.00 380.00 383.00 376.00 390.00 389.00 391.00 416.00 427.00 379.00 396.00 365.00 423.00 361.00 366.00 378.00 365.00 364.00 356.00 364.00 367.00 402.00 411.00 420.00 362.00 364.00 380.00 349.00 363.00 355.00 362.00 349.00 350.00 324.00 375.00 337.00 391.00 368.00 350.00 348.00 364.00 343.00 334.00 354.00 353.00 358.00 384.00 444.00 440.00 383.00 420.00 389.00 409.00 374.00 383.00 353.00 375.00 362.00 341.00 340.00 352.00 411.00 333.00 406.00 384.00 309.00 353.00 312.00 314.00 323.00 319.00 334.00 322.00 318.00 319.00 378.00 374.00 350.00 360.00 344.00 341.00 334.00 344.00 340.00 328.00 339.00 366.00 396.00 422.00 352.00 393.00 354.00 353.00 341.00 340.00 356.00 355.00 352.00 367.00 360.00 411.00 406.00 396.00 372.00 376.00 383.00 376.00 379.00 383.00 394.00 375.00 379.00 392.00 435.00 375.00 443.00 404.00 352.00 373.00 380.00 381.00 358.00 354.00 340.00 369.00 396.00 417.00 388.00 396.00 383.00 381.00 367.00 383.00 380.00 419.00 379.00 414.00 385.00 384.00 368.00 388.00 368.00 367.00 363.00 376.00 349.00 368.00 341.00 318.00 343.00 353.00 367.00 348.00 364.00 366.00 434.00 372.00 430.00 388.00 368.00 360.00 365.00 385.00 418.00 383.00 387.00 384.00 381.00 386.00 414.00 380.00 394.00 439.00 393.00 379.00 362.00 386.00 353.00 345.00 380.00 384.00 378.00 387.00 444.00 408.00 426.00 426.00 383.00 346.00 366.00 363.00 370.00 358.00 366.00 368.00 426.00 428.00 386.00 367.00 354.00 375.00 399.00 347.00 390.00 419.00 406.00 390.00 419.00 387.00 399.00 394.00 445.00 399.00 398.00 412.00 398.00 404.00 390.00 418.00 412.00 439.00 516.00 423.00 447.00 400.00 409.00 421.00 440.00 416.00 423.00 407.00 431.00 430.00 407.00 430.00 418.00 419.00 455.00 422.00 430.00 466.00 408.00 445.00 386.00 434.00 429.00 418.00 412.00 405.00 394.00 390.00 426.00 392.00 392.00 426.00 379.00 413.00 432.00 395.00 368.00 371.00 399.00 391.00 371.00 384.00 379.00 415.00 442.00 371.00 414.00 405.00 426.00 389.00 384.00 380.00 388.00 387.00 406.00 379.00 404.00 414.00 474.00 383.00 403.00 401.00 409.00 409.00 407.00 405.00 410.00 424.00 398.00 384.00 445.00 391.00 420.00 387.00 389.00 381.00 365.00 381.00 371.00 350.00 367.00 345.00 349.00 416.00 352.00 354.00 384.00 306.00 338.00 299.00 317.00 307.00 332.00 310.00 302.00 327.00 327.00 396.00 347.00 381.00 380.00 326.00 375.00 332.00 347.00 347.00 344.00 341.00 354.00 334.00 332.00 394.00 411.00 390.00 367.00 379.00 395.00 353.00 386.00 361.00 343.00 365.00 327.00 366.00 378.00 368.00 336.00 348.00 354.00 357.00 367.00 358.00 363.00 362.00 337.00 371.00 370.00 348.00 343.00 417.00 401.00 409.00 409.00 364.00 407.00 374.00 372.00 368.00 352.00 332.00 358.00 345.00 316.00 369.00 339.00 355.00 374.00 418.00 359.00 384.00 378.00 362.00 364.00 371.00 378.00 382.00 366.00 404.00 370.00 408.00 399.00 375.00 371.00 391.00 383.00 358.00 360.00 381.00 357.00 370.00 369.00 387.00 455.00 378.00 368.00 430.00 375.00 385.00 416.00 394.00 420.00 446.00 404.00 424.00 422.00 428.00 437.00 412.00 411.00 401.00 382.00 388.00 387.00 395.00 389.00 383.00 383.00 444.00 395.00 412.00 402.00 378.00 399.00 380.00 379.00 366.00 374.00 380.00 389.00 396.00 405.00 459.00 433.00 452.00 451.00 397.00 373.00 379.00 388.00 371.00 380.00 385.00 360.00 412.00 452.00 371.00 386.00 358.00 370.00 383.00 357.00 328.00 358.00 361.00 361.00 379.00 438.00 375.00 400.00 398.00 398.00 369.00 352.00 356.00 368.00 368.00 365.00 351.00 419.00 404.00 371.00 383.00 349.00 352.00 337.00 371.00 321.00 340.00 357.00 308.00 326.00 358.00 327.00 368.00 434.00 372.00 414.00 394.00 362.00 370.00 378.00 373.00 367.00 368.00 372.00 359.00 410.00 407.00 407.00 413.00 375.00 413.00 344.00 366.00 374.00 393.00 363.00 369.00 383.00 368.00 445.00 419.00 380.00 397.00 377.00 367.00 366.00 358.00 353.00 392.00 360.00 341.00 388.00 401.00 383.00 359.00 330.00 354.00 311.00 326.00 361.00 322.00 347.00 324.00 350.00 329.00 323.00 339.00 366.00 363.00 345.00 354.00 374.00 307.00 355.00 370.00 338.00 347.00 333.00 362.00 337.00 356.00 364.00 354.00 354.00 336.00 388.00 339.00 332.00 362.00 337.00 383.00 339.00 359.00 367.00 359.00 348.00 333.00 353.00 340.00 345.00 370.00 342.00 421.00 383.00 386.00 392.00 345.00 356.00 331.00 333.00 332.00 330.00 362.00 316.00 367.00 350.00 334.00 348.00 340.00 358.00 329.00 371.00 393.00 353.00 373.00 340.00 344.00 358.00 383.00 366.00 375.00 412.00 433.00 377.00 398.00 383.00 410.00 387.00 369.00 381.00 388.00 360.00 366.00 375.00 376.00 404.00 469.00 389.00 439.00 398.00 371.00 364.00 376.00 330.00 333.00 348.00 326.00 349.00 377.00 338.00 383.00 353.00 343.00 363.00 340.00 343.00 359.00 379.00 373.00 337.00 351.00 370.00 432.00 378.00 387.00 444.00 384.00 403.00 384.00 397.00 416.00 386.00 377.00 372.00 378.00 395.00 435.00 385.00 375.00 426.00 358.00 400.00 369.00 392.00 378.00 380.00 414.00 397.00 410.00 411.00 409.00 428.00 386.00 408.00 400.00 378.00 390.00 410.00 420.00 428.00 417.00 461.00 401.00 440.00 433.00 383.00 385.00 363.00 366.00 356.00 370.00 353.00 336.00 357.00 368.00 385.00 427.00 429.00 365.00 403.00 388.00 395.00 380.00 398.00 397.00 420.00 383.00 424.00 399.00 420.00 406.00 437.00 392.00 417.00 438.00 427.00 429.00 462.00 394.00 405.00 440.00 411.00 416.00 442.00 393.00 411.00 428.00 413.00 416.00 404.00 401.00 418.00 413.00 401.00 415.00 387.00 407.00 430.00 391.00 411.00 407.00 430.00 461.00 410.00 438.00 436.00 434.00 428.00 411.00 432.00 408.00 418.00 390.00 409.00 408.00 474.00 399.00 450.00 415.00 382.00 398.00 373.00 385.00 399.00 386.00 367.00 382.00 362.00 374.00 440.00 383.00 411.00 383.00 367.00 364.00 364.00 360.00 373.00 362.00 340.00 364.00 445.00 418.00 370.00 391.00 383.00 379.00 393.00 392.00 378.00 394.00 381.00 405.00 389.00 462.00 455.00 464.00 437.00 409.00 417.00 411.00 387.00 402.00 403.00 409.00 382.00 423.00 477.00 383.00 398.00 394.00 376.00 363.00 382.00 378.00 408.00 358.00 397.00 370.00 374.00 371.00 374.00 387.00 383.00 368.00 388.00 380.00 413.00 461.00 396.00 407.00 374.00 407.00 378.00 383.00 368.00 380.00 378.00 361.00 365.00 373.00 363.00 444.00 395.00 350.00 383.00 336.00 339.00 366.00 355.00 372.00 336.00 364.00 370.00 376.00 437.00 388.00 380.00 408.00 366.00 382.00 376.00 385.00 389.00 358.00 347.00 368.00 371.00 392.00 453.00 410.00 442.00 437.00 383.00 411.00 388.00 400.00 387.00 383.00 388.00 385.00 399.00 376.00 411.00 375.00 400.00 382.00 380.00 422.00 357.00 389.00 354.00 373.00 386.00 384.00 342.00 354.00 369.00 334.00 389.00 357.00 367.00 339.00 389.00 362.00 380.00 363.00 380.00 437.00 383.00 397.00 408.00 392.00 399.00 415.00 404.00 402.00 367.00 403.00 392.00 379.00 355.00 324.00 331.00 372.00 337.00 323.00 351.00 339.00 320.00 363.00 340.00 330.00 308.00 343.00 329.00 326.00 335.00 342.00 340.00 354.00 397.00 383.00 409.00 428.00 377.00 390.00 378.00 380.00 356.00 378.00 350.00 367.00 362.00 373.00 364.00 402.00 459.00 368.00 412.00 365.00 345.00 368.00 356.00 380.00 370.00 366.00 382.00 383.00 446.00 422.00 402.00 425.00 411.00 389.00 365.00 412.00 404.00 394.00 422.00 409.00 405.00 461.00 439.00 407.00 445.00 436.00 375.00 396.00 380.00 363.00 383.00 

What Arduino are you using?

Have you established that it can and does actually sample at 40 kHz? If not, the frequency values in the output spectrum will be meaningless.

Keep in mind that severe aliasing (output distortion) will result if there are signals in the input with frequency higher than sample frequency/2.

Im using a raspberry pi pico. I have tested simply printing analogRead() to Serial and then looking at the Serial plotter. It does seem to give proper frequencies for that (for example it peaks when i blow on it ).

I found the problem!

const long SAMPLE_INTERVAL_US =  round(1000000 * (1 / SAMPLE_FREQ));

// SAMPLE_INTERVAL_US = 0

This was being set as 0, where it should be 25. The issue was that (1 / SAMPLE_FREQ) was just 0 since it was integer division.

This change fixed my issue:

const long SAMPLE_INTERVAL_US = round(1000000 * (1.0 / SAMPLE_FREQ));

// SAMPLE_INTERVAL_US = 25

Frequency and sampling works now :slight_smile:

I know you have marked this solved, but how can this code actually work if you have no line to include the FFT library?

I just forgot to copy paste it in my post. In the code I do have it included ofcourse.

And what else have you forgotten to include?
We can only know what you tell us, and telling lies helps no one.