How to Use a Microphone with Arduino

arduino microphone

How can an Arduino capture sound? Just like how your computer captures sound: through a microphone. In this Arduino microphone tutorial, we’ll look at how a sound sensor works. Also, we’ll build a simple clap switch project as an example of how to use a microphone with Arduino.

The Electret Microphone

Most “sound sensor” boards available feature a specific kind of microphone: the electret microphone. The Grove Sound Sensor and Sunfounder Sound Sensor feature this microphone:

Sunfounder sound sensor module

The electret microphone is a type of condenser microphone. Condensers are essentially capacitors and condenser microphones are formed by a thin diaphragm mounted in front of a plate. Recall that a capacitor is formed by two conductors with an insulator, or dielectric, between them. Both the diaphragm and the plate are conductors. The smaller the distance between these two conductors, the higher the capacitance, given that the surface area of the conductors is unchanged.

Electret microphone construction

When sound reaches the condenser, the diaphragm vibrates thereby changing the distance between the conductors and effectively changing the capacitance between the diaphragm and the plate. This capacitance is converted to voltage with the application of a sufficient charge on the condenser. As shown in the picture above, the voltage is boosted through a built-in transistor inside the microphone.

Now condenser microphones need high voltages to maintain that charge. An electret microphone doesn’t need high voltages because the diaphragm is made out of a special material that stores a high enough charge. This material, which is the electret, is like a permanent magnet but stores a permanent charge.

The voltage changes that are produced by sound should be enough to be detected by an Arduino. This is why sound sensors often always have an operational amplifier IC included. For the Sound Sensor board, the LM358 is the op-amp.

Using an Arduino Microphone

Since the sound sensor board produces a changing voltage, we’ll need the Arduino’s analog-to-digital converter to process that voltage. Here’s a sketch that reads the voltage from the sound sensor connected to A0 which can then be seen on the Arduino IDE’s serial plotter.

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  long sum = 0;
  for(int i=0; i<100; i++)
  {
     sum += analogRead(soundPin);
  }

  sum = sum/100;

  Serial.println(sum);

  delay(10);
}

Here's the wiring diagram for the sketch:

Arduino Microphone

Expect the sound sensor board to generate a "noisy" voltage given the nature of ambient sound. Here is the microphone output as shown by the serial plotter:

What we can only deduce from this data is the peak value when there is a loud sound.

For example, the values hover around 300 to 500+ based on the graph above. But when I clap, this shows:

Making a Clap Switch

I can now use this data to create a simple clap switch. Here’s the new sketch:

int soundPin = A0;
int LED = 13;

void setup()
{
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop()
{
  long sum = 0;
  for(int i=0; i<100; i++)
  {
    sum += analogRead(soundPin);
  }

  sum = sum/100;

  if(sum > 600)
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }

  Serial.println(sum);
  delay(10);
}

I added a LED that will turn on every time the sound sensor picks up a loud enough sound, like clapping. Otherwise, it stays off.

Of course, you can create a much more complicated project using an Arduino microphone. Keep in mind though that you won’t be able to create speech recognition projects with this setup. That is unless you use a computer or add another component for voice processing.

Leave a Reply

Your email address will not be published. Required fields are marked *