Using the microphone with the Raspberry Pi

One of the components that most people find missing in the Raspberry Pi is the analog input.

To use the microphone with the Raspberry Pi, you'll need to do one of two things. Buy micro USB or USB sound card. Micro USB could be the cheaper and easier way for microphones to work with the Raspberry Pi.

Alternatively, you can purchase an HAT to expand the sound capabilities of the Raspberry Pi with the addition of the analog input. There are various projects that the microphone comes with, such as setting up a Raspberry Pi as an Alexa or Google assistant.

Connect the microphone to the Raspberry Pi

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

1. Micro USB

Micro USB is the easiest way for microphones to work with the Raspberry Pi. One of the most significant advantages of using micro USB is its simplicity. The Raspbian operating system will automatically detect the microphone when it is plugged in.

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

2. Micro Bluetooth

Connecting a Bluetooth microphone makes things a little more complicated. For this to work, you'll need to either use a Raspberry Pi 3 or higher or use a USB Bluetooth receiver.

To connect the microphone to Raspberry Pi, you can follow the instructions: How to set up Wi-Fi and Bluetooth on Raspberry Pi 3.

After pairing the microphone, you'll be able to use it just like micro USB.

3. 3.5mm microphone

While the Raspberry Pi doesn't have any 3.5mm input jack for connecting to analog microphones, there are ways you can work around this problem.

4. USB sound card

One of the cheapest ways to connect a microphone to a Raspberry Pi is to use a USB sound card.

What a USB sound card does is convert the 3.5mm analog connection into a digital (digital) signal that can be sent over a USB connection. They are very easy to use. The Raspbian operating system should be able to work automatically.

Configuring Alsa on Raspberry Pi for microphone

Before starting this section, make sure you've plugged the microphone into your Raspberry Pi. Whether it's via USB soundcard, HAT or using micro USB.

Step 1. Go ahead and update the operating system by running the following two commands. Running these two commands will ensure that everything is up to date.

sudo apt update sudo apt upgrade

Step 2. With the OS updated, go ahead and locate the microphone seen by the Alsa driver. To view all available recording devices, you need to run the following command.

arecord -l

Step 3. From this command, you should see something like the one below appear in Terminal.

Two things you need to pay attention to are 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 determined the device number of the microphone, proceed to the next step.

If nothing appears, make sure that your device is plugged in properly.

Step 4. With your device and card number now available, go ahead and create the configuration file for Alsa. This configuration file will help Alsa driver understand what device it should use to record audio. Start modifying the file by running the following command.

nano /home/pi/.asoundrc

Step 5 In this file, enter the following text. Make sure 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, and then ENTER.

Recording using the microphone on the Raspberry Pi

Using the microphone with the Raspberry Pi Picture 1Using the microphone with the Raspberry Pi Picture 1

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

Step 1. To record audio on Raspberry Pi, use the following command. This command will create a recording using the microphone and save it as a wav file named out.wav.

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

After completing the recording, you can exit the tool by pressing CTRL + C.

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

Step 2. You can verify that the microphone was successfully recorded by playing it back. To replay the recording you just made, run the command below.

aplay out.wave

If you can hear your recording then you have successfully recorded it with the microphone already.

Gain control for microphones on Raspberry Pi

If you have difficulty hearing recorded sound through the microphone, you can adjust the gain (a measure of signal amplification).

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

alsamixer

2. Using this tool, you can use the arrow keys to increase and decrease gain. If the microphone is not available, press the F6 key to select the microphone.

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

4 ★ | 11 Vote