Introduction: STM32CubeMx Microphone (STM32F407VG)

Hi! In this project we will try to get sound by using microphone (external not onboard microphone) and play it through speaker. This tutorial will be very short as I will give explanations of project parts by reference to some videos. So, let's jump into the project :)

Step 1: Software and Hardware Requirements

Hardware requirements:

  • STM32F4 discovery board (or any other STM32F4 board)
  • MAX9814 electret microphone with amplifier
  • PAM8403 audio amplifier module
  • 4 OHM speaker

Software requirements:

  • STM32CubeMX
  • Keil uVision5

Step 2: Determine Project Plan

So, let's understand first what we want to do. First, we want to get sound from electret microphone. As you know, MCU process everything digitally. However, sound is analog signal. So, we need to convert it to digital signal and this is done by ADC (analog to digital converter) and the process is called sampling. You can search for more information. Here there is one important point: In order to get sound from the speaker appropriately, sampling frequency should be at least two times higher than audio frequency at the output. This is called Nyquist-Shannon theorem.

After converting it to digital signal we can process it as we want and then output that sound again. However, speaker needs analog signal. So, we need convert this digital signal to analog back. For that we will use DAC (digital to analog converter). At the end we can output that sound :)

Step 3: How to Setup and Implement ADC and DAC With DMA

As I said, I have learnt this process also from a video. I will give the link to this video. Be patient and listen carefully. He explains perfectly all the process.

Links: Part1 and Part2

*Note: Check this line in your code and enable DMA continuous request:

hadc1.Init.DMAContinuousRequests = ENABLE;

Step 4: Out to Speaker

After doing all the steps, you need to connect the speaker as the image above. After that, play sound on your phone and decrease sound down to the limit which you can hear hardly. Then, take phone near to the microphone and you will hear sound from the speaker loudly. Don't speak to microphone, because it will be hard to catch whether there is output from speaker or not :)

Step 5: Conclusion

So, we have reached end of the project. If you have any question or problem please don't hesitate to ask :)