Using a Microphone with a Raspberry Pi

In this guide, we will be walking you through the process of using a microphone with the Raspberry Pi.

Raspberry Pi Microphone

One of the components that most people will notice is missing from a Raspberry Pi is that lack of any analog inputs.

To make use of a microphone with the Raspberry Pi, you will need to do one of two things.

Purchase a USB microphone or a USB soundcard. A USB microphone is likely to be a cheaper and easier way to get a microphone working with your Raspberry Pi.

You could alternatively purchase a HAT that expands the audio capabilities of the Raspberry Pi with the addition of analog input.

There are various projects that a microphone comes handy with, such as setting up your Raspberry Pi as an Alexa or Google assistant.

There are a couple of different reasons that the Raspberry Pi does not feature any support for a microphone in its hardware.

  • Limited space on the board. An additional 3.5mm jack takes up quite a large amount of space on the board that can be used for other things.
  • A price limit. The Raspberry Pi is aimed at being an affordable board; reducing components helps keep the price down.

Equipment List

Below is a list of equipment that you will need to get a microphone working with the Raspberry Pi.

Recommended

Optional

For this guide on using a microphone with a Raspberry Pi, we made use of the Raspbian operating system.

Connecting a Microphone to the Raspberry Pi

There are three different ways you can connect a microphone to your Raspberry Pi.

Most methods are plug and play, but we will quickly run over the various ways you can connect a microphone.

USB Microphone

USB Microphones are the easiest way of getting a microphone working with your Raspberry Pi.

One of the most significant advantages of using a USB microphone is that it is plug and play.

The Raspbian operating system will automatically detect the microphone when its plugged in.

In some cases, you will need to modify the .asoundrc file to help the Alsa sound driver understand which device it should be using.

Bluetooth Microphone

Connecting a Bluetooth microphone makes things a little bit more complicated. For this to work, you will need to be either using a Raspberry Pi 3 or newer or be making use of a USB Bluetooth receiver.

To connect your microphone to your Raspberry Pi, you can follow our guide on dealing with Bluetooth devices.

Once you have paired the microphone, you should be able to use it just like your USB microphone.

3.5mm Microphones

While the Raspberry Pi does not feature any input 3.5mm jacks to connect an analog microphone with, there are ways you can work around this.

USB Soundcard

One of the cheapest ways of connecting your microphone to a Raspberry Pi is to make use of a USB soundcard.

What a USB soundcard, does is convert the analog 3.5mm connection to a digital signal that can be sent over the USB connection.

These are super straightforward to use and are plug and play. The Raspbian operating system should be able to work automatically

Configuring Alsa on your Raspberry Pi for your Microphone

Before we begin this section, make sure that you have your microphone plugged into your Raspberry Pi. Whether that be through a USB soundcard, a HAT or a by using a USB microphone.

1. Let’s go ahead and update our operating system by running the following two commands.

Running these two commands will make sure that everything is up to date.

sudo apt update
sudo apt upgrade

2. With the operating system up to date, let’s go ahead and identify our microphone, as seen by the Alsa driver.

To see all the available recording devices, we need to run the following command.

arecord -l

3 From this command, you should see something like below appear in the terminal.

The two things you need to pay attention to is the card number and the device number.

**** List of CAPTURE Hardware Devices ****
card 1: Microphone [Yeti Stereo Microphone], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Once you have identified your microphone’s device numbers, proceed to the next step.

If nothing appears, then make sure you have plugged your device in properly.

4 With our device and card numbers now handy, let’s go ahead and create a configuration file for Alsa.

This configuration file will help the Alsa driver understand what device it should be using for capturing audio.

Begin modifying the file by running the following command.

nano /home/pi/.asoundrc

5 Within this file, enter the following text.

Make sure that you replace [card number] and [device number] with the values you retrieved in step 3 of this section.

pcm.!default {
  type asym
  capture.pcm "mic"
}
pcm.mic {
  type plug
  slave {
    pcm "hw:[card number],[device number]"
  }
}

Once done, save the file by pressing CTRL + X, followed by Y, then ENTER.

Recording with your Microphone on your Raspberry Pi

In this section, we will show you how you can record using your microphone on a Raspberry Pi.

1 To make a recording on your Raspberry Pi, make use of the following command.

This command will make a recording using the microphone device and save it as a wav file called out.wav.

arecord --format=S16_LE --rate=16000 --file-type=wav out.wav

Once you have finished making your recording, you can exit out of the tool by pressing CTRL + C.

If you receive an error while running this command, make sure that your microphone is properly plugged in. Also, make sure you have configured everything correctly by verifying the contents of the .asoundrc file.

2 You can verify that your microphone made a successful recording by playing it back.

To playback the recording that you just made run the command below.

aplay out.wave

If you can hear your recording, then you have successfully made a recording using your microphone.

Controlling your Microphone Gain on the Raspberry Pi

If you find that you are struggling to hear noise recorded through your microphone, then it is possible to adjust the gain.

1 The easiest way to do this is to make use of the Alsamixer tool by running the following command.

alsamixer

2 Using this tool, you can use the arrow keys to increase and decrease the gain.

If your microphone is not available, press the F6 key to select your microphone.

Raspberry Pi AlsaMixer Microphone settings

Once done, exit out of the program by pressing the ESC key.

At this point you should now have an understanding of how to use your microphone with a Raspberry Pi.

If you have issues with getting your microphone working or have feedback, feel free to drop a comment below.

Leave a Reply

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

5 Comments

  1. Avatar for Negrosam Oluwatobi
    Negrosam Oluwatobi on

    Please, am working on speech to text command using raspberry pi zero. I got my USB mic connected but anytime I run my python code on it, it gives ALSA lib pcm error in which I don’t understand anything about it.
    The code:

    import speech_recognition as sr
    from datetime import date
    from time import sleep
    r = sr.Recognizer()
    mic = sr.Microphone()
    print("hello")
    while True:
            with mic as source:
                    audio = r.listen(source)
            words = r.recognize_google(audio)
    print(words)
    if words == "today":
            print(date.today())
    if words == "exit":
            print("...")
            sleep(1)
            print("...")
            sleep(1)
            print("...")
            sleep(1)
    1. Avatar for Emmet
      Emmet on
      Editor

      Hi,

      Sadly I’m not entirely sure why that would be occuring, ALSA can finnicky so hopefully someone else may have a suggestion for you.

      Cheers,
      Emmet

  2. Avatar for Tachy
    Tachy on

    Can you be more specific about what to put into this line?

    pcm “hw:[card number],[device number]”

    I get the following info from the arecord -l command

    *** List of CAPTURE Hardware Devices ****
    card 2: webcam [webcam], device 0: USB Audio [USB Audio]
    Subdevices: 1/1
    Subdevice #0: subdevice #0

    Do I put in [2] or [card 2: webcam] or something else?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Tachy,

      You strictly will want the identifying numbers.

      With the data you have shown there you can see that the card number is 2 (card 2) and that the device number is 0 (device 0).

      So you would use the following.

      pcm "hw:2,0"

      I hope this helps you out.

      Cheers,
      Emmet

  3. Avatar for CVBruce
    CVBruce on

    Thanks, this worked perfectly. My USB microphone is in my webcam, and I was surprised at th sound quality.