Audio Input isnt working correctly

hi,

I’m trying to capture a sin wave from a microphone and output it on an oscilloscope. I have to do a lot more with the sin wave but I’m facing an issue in the first step itself.

This is the code i’m using

#include <mbed.h>

#define BUFFER_SIZE 256

AnalogIn microphone(PA_3); 
AnalogOut speaker(PA_4); 

float buffer[BUFFER_SIZE];
int bufferIndex = 0;

int main() {
    while (true) {
        // Record
        buffer[bufferIndex] = microphone.read();
        
        // Playback
        speaker.write(buffer[bufferIndex]);

        bufferIndex = (bufferIndex + 1) % BUFFER_SIZE;

        wait_us(1000); // Wait for 1ms
    }
}

The output is absolutely nonsense. When I printf the values of input all I get is 0.5 continuously. Thanks.

Maybe the amplitude of the incoming microphone signal is just too low for the MCU to be able to read it? If you hook up an O-scope to the input, what’s the peak to peak voltage of the signal, and also what’s the center voltage? Also, what Mbed board are you using?

I’m using a STM32F4 discovery board (F429I). When I connect it to the board I get a Pk - Pk of 50mV and Max of 2.9 V. But when I probe it directly to the mic I get the correct display. So my channel 1 is the output from the board and channel 2 is the output from the mic. Ch 2 and ch 1 are not matching. Ch 2 displays a sin wave where as ch 1 is garbled noise.

Hmm… if you connect the ADC pin to 3.3V, does the output match? And if not, what is the value of the floats in buffer? That’s where I’d start debugging,