Skip to content

Commit 4ceb90e

Browse files
authored
Merge pull request #32 from arduino/marqdevx/tutorial/NiclaVision/microphone
Nicla Vision, microphone tutorial [PC-856]
2 parents 9bc875a + be949c6 commit 4ceb90e

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
---
2+
title: 'Testing and Controlling the Microphone'
3+
difficulty: easy
4+
compatible-products: [nano-33-ble-sense]
5+
description: 'Learn how to create a soundmeter using the built-in microphone with the Nicla Vision.'
6+
tags:
7+
- OpenMV
8+
- Microphone
9+
- Sound
10+
- Sensor
11+
author: Pablo Marquínez
12+
libraries:
13+
- name: Arduino PDM
14+
url: https://www.arduino.cc/en/Reference/PDM
15+
hardware:
16+
- hardware/05.nicla/boards/nicla-vision
17+
software:
18+
- OpenMV
19+
- ide-v1
20+
- ide-v2
21+
- web-editor
22+
- cli
23+
---
24+
25+
![Nicla Vision - microphone](assets/nicla-vision-microphone.png)
26+
27+
## Overview
28+
29+
In this tutorial you will use the **Arduino Nicla Vision** board to get the microphone (MP34DT06JTR) readings and change the LED brightness.
30+
31+
## Goals
32+
33+
- Get the microphone data.
34+
- Use the PDM(Pulse-density modulation) library.
35+
- Print the microphone values in the Serial Monitor.
36+
- Change RGB brightness with the stored microphone values.
37+
38+
### Required Hardware and Software
39+
40+
- Arduino Nicla Vision
41+
- Latest mbed Core version
42+
43+
## Set Up
44+
45+
To check that you correctly set up the board please visit our [Getting Started Guide](https://docs.arduino.cc/tutorials/nicla-vision/getting-started) for both **OpenMV** and **Arduino** instructions.
46+
47+
### OpenMV
48+
49+
Open the script by going to **Examples > Arduino > NanoRP2040 > Audio > Audio_fft.py**.
50+
51+
***Using the same sketch as the NanoRP2040, because both boards access the microhpone in the same way***
52+
53+
Make sure the board is connected, if the board is connected to OpenMV you should see a green play button in the bottom left corner of the window. If you do not see this icon, try pressing the connect button in the bottom left corner. If there still is some issue to connect the board take another look at the getting started guide.
54+
55+
When the script is running, you will see an spectrum analyzer in the top right panel that reflects the audio readings input. Try making some noise and see how it reacts.
56+
57+
![OpenMV IDE - Spectrum analyzer](assets/OpenMV_spectrumAnalyzer.png)
58+
59+
### Arduino
60+
61+
## Instructions
62+
63+
### Setting Up the Sketch
64+
65+
We will edit the example from the mbed Core, go to **Examples > PDM > PDMSerialPlotter** and save it into your sketchbook.
66+
67+
You can run the sketch to see the result, it will show the data that the microphone is getting on the **Serial Plotter**.
68+
69+
### Controlling the Blinking LED
70+
71+
Now that you can get the microphone data, let's control the built-in RGB LED and change the speed of its blinking depending on the values.
72+
73+
You can access the example sketch at **Examples > PDM > PDMSerialPlotter** and then edit as shown in this tutorial.
74+
Or find the full edited sketch in our **Arduino_Pro_Tutorials** library.
75+
76+
### Complete Sketch
77+
78+
```arduino
79+
/*
80+
This example reads audio data from the on-board PDM microphones, and prints
81+
out the samples to the Serial console. The Serial Plotter built into the
82+
Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)
83+
Circuit:
84+
- Arduino Nano 33 BLE board, or
85+
- Arduino Nano RP2040 Connect, or
86+
- Arduino Portenta H7 board plus Portenta Vision Shield
87+
This example code is in the public domain.
88+
*/
89+
90+
91+
#include <PDM.h>
92+
93+
// default number of output channels
94+
static const char channels = 1;
95+
96+
// default PCM output frequency
97+
static const int frequency = 16000;
98+
99+
// Buffer to read samples into, each sample is 16-bits
100+
short sampleBuffer[512];
101+
102+
// Number of audio samples read
103+
volatile int samplesRead;
104+
105+
// Blinking
106+
bool state = false;
107+
int timeStart = 0;
108+
109+
void setup() {
110+
Serial.begin(9600);
111+
pinMode(LEDB, OUTPUT);
112+
113+
while (!Serial);
114+
115+
// Configure the data receive callback
116+
PDM.onReceive(onPDMdata);
117+
118+
// Optionally set the gain
119+
// Defaults to 20 on the BLE Sense and 24 on the Portenta Vision Shield
120+
// PDM.setGain(30);
121+
122+
// Initialize PDM with:
123+
// - one channel (mono mode)
124+
// - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
125+
// - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shield
126+
if (!PDM.begin(channels, frequency)) {
127+
Serial.println("Failed to start PDM!");
128+
while (1);
129+
}
130+
131+
132+
}
133+
134+
void loop() {
135+
// Wait for samples to be read
136+
if (samplesRead) {
137+
138+
// Print samples to the serial monitor or plotter
139+
for (int i = 0; i < samplesRead; i++) {
140+
if (channels == 2) {
141+
Serial.print("L:");
142+
Serial.print(sampleBuffer[i]);
143+
Serial.print(" R:");
144+
i++;
145+
}
146+
Serial.println(sampleBuffer[i]);
147+
}
148+
149+
// Clear the read count
150+
samplesRead = 0;
151+
152+
if (millis() - timeStart > sampleBuffer[2]) {
153+
digitalWrite(LEDB, state);
154+
state = !state;
155+
}
156+
}
157+
}
158+
159+
/**
160+
Callback function to process the data from the PDM microphone.
161+
NOTE: This callback is executed as part of an ISR.
162+
Therefore using `Serial` to print messages inside this function isn't supported.
163+
* */
164+
void onPDMdata() {
165+
// Query the number of available bytes
166+
int bytesAvailable = PDM.available();
167+
168+
// Read into the sample buffer
169+
PDM.read(sampleBuffer, bytesAvailable);
170+
171+
// 16-bit, 2 bytes per sample
172+
samplesRead = bytesAvailable / 2;
173+
}
174+
```
175+
176+
177+
### Testing It Out
178+
179+
After you have successfully verified and uploaded the sketch to the board, open the Serial Monitor from the menu on the left. You will now see the new values printed.
180+
181+
If you want to test it, the only thing you need to do is to speak or play some sounds close to the board and see how the blinking of the RGB LED changes based on the input.
182+
183+
### Troubleshoot
184+
185+
- In case the Serial Monitor freezes, unplug and then plug the board into your computer again, now try to upload the sketch
186+
- If the sketch is not working, try to double tap the reset button and upload the sketch once again.
187+
188+
## Conclusion
189+
190+
You have learned how to use the Arduino IDE and OpenMV to get data from the microphone and then use it to change the RGB LED on the board. This can for example be used as an alarm system to wake the board up and take a screenshot with the Camera.

0 commit comments

Comments
 (0)