Skip to content

Add example for analog ICS-40180 microphone #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 14, 2021
Merged

Add example for analog ICS-40180 microphone #135

merged 12 commits into from
Oct 14, 2021

Conversation

matiasilva
Copy link

@matiasilva matiasilva commented Jul 9, 2021

This PR adds an example for an analog ICS-40180 based microphone from SparkFun (but really any analog microphone should do). It includes a simple Python script that takes information directly from the serial and plots it for easy visualization.

Checklist

  • documentation
  • working example
  • Fritzing schematic and image
  • added to CMakeLists.txt
  • added to repo README

@matiasilva matiasilva marked this pull request as ready for review July 9, 2021 17:04
while (1) {
adc_raw = adc_read(); // raw voltage from ADC
printf("%.2f\n", adc_raw * ADC_CONVERT);
sleep_ms(10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the 10ms sleep here implies there's a maximum frequency that this code will be able to detect? (only mentioning this because you talked about FFT analysis earlier)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Though I'm not 100% sure where the botteneck is/will be, I don't think it's with the ADC but rather on the Python side with polling values from the UART, so I just set 10ms as a sensible value.


def serial_getter():
# grab fresh ADC values
# note sometimes UART drops chars so we try a max of 5 times
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, this is probably related to raspberrypi/pico-sdk#504 !


The Pico has a 12-bit ADC, meaning that a read operation will return a number ranging from 0 to 4095 (2^12 - 1) for a total of 4096 possible values. Therefore, the resolution of the ADC is 3.3/4096, so roughly steps of 0.8 millivolts. The SparkFun breakout uses an OPA344 operational amplifier to boost the signal coming from the microphone to voltage levels that can be easily read by the ADC. An important side effect is that a bias of 0.5*Vcc is added to the signal, even when the microphone is not picking up any sound.

The ADC provides us with a raw voltage value but when dealing with sound, we're more interested in the amplitude of the audio signal. This is defined as one half the peak-to-peak amplitude. Included with this example is a very simple Python script that will plot the values it receives via the serial port. By tweaking the sampling rates, and various other parameters, the data from the microphone can analyzed in various ways, such as in a Fast Fourier Transform to see what frequencies make up the signal.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why say "we're more interested in the amplitude of the audio signal. This is defined as one half the peak-to-peak amplitude." when the Python code just plots the raw voltage?

Copy link
Author

@matiasilva matiasilva Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plotting the voltage lets us see everything, including the peaks. It's just background info I guess.

@aallan aallan added the enhancement New feature or request label Jul 12, 2021
printf("Beep boop, listening...\n");

bi_decl(bi_program_description("Analog microphone example for Raspberry Pi Pico")); // for picotool
bi_decl(bi_1pin_with_name(ADC_PIN, "ADC input pin"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kilograham Is ADC_PIN here correct, or should it be ADC_PIN + 26 as used with adc_gpio_init below?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amusingly, I faced the same dilemma. adc_select_input expects 0-3 while adc_gpio_init expects a proper pin number.

@aallan
Copy link

aallan commented Aug 3, 2021

Where are we on this, are we ready to pass it to @kilograham for a final review? @lurch opinions?

Copy link
Collaborator

@JamesH65 JamesH65 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aallan aallan changed the base branch from develop to merge-intern-examples October 14, 2021 09:16
@aallan aallan merged commit a6cbffc into raspberrypi:merge-intern-examples Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants