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

Raspberry Pi

Micro SD Card

Power Supply

Ethernet Cord or WiFi dongle (The Pi 3 and 4 has WiFi inbuilt)

USB Microphone or USB Sound Card

Optional

Raspberry Pi Case

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 *