In this lecture we look a bit more into the hardware available for the Nano 33 BLE Rev2. More specifically:
- IMU (accelerometer, gyroscope, and magnetometer)
- Microphone
- Other sensors (temperature and humidity, proximity, rbg)
[-4, +4]g -/+0.122 mg.
[-2000, +2000] dps +/-70 mdps.
Goal: We want to develop a noise detector. Some of the questions to consider:
- How does Nano33BLE distinguish level of noises?
- Answer: measure collected data to determine …
- How does Nano33BLE provide signal?
- Answer: the LED, maybe …
- We will use the header files for this example to better organize the structure of the sketch.
We will be reusing the include/led_controller.h and src/led_controller.cpp files.
1
2
3
4
5
void beginLED();
void emitColor(int color);
void setred();
void setyellow();
void setgreen();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Arduino.h>
#include "led_controller.h"
void setred() {
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
}
void setyellow() {
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
}
void setgreen() {
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
}
void beginLED() {
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
}
void emitColor(int color) {
if (color == 0) {
setgreen();
} else if (color == 1) {
setyellow();
} else if (color == 2) {
setred();
}
}
When activated, the Nano33BLE microphone component samples sound and stores the measurements in memory.
setup() using setBufferSize().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <Arduino.h>
#include <PDM.h>
#include "led_controller.h"
//#include "mic_controller.h"
short sampleBuffer[256];
volatile int samplesRead;
void onPDMdata() {
int bytesAvailable = PDM.available();
PDM.read(sampleBuffer, bytesAvailable);
samplesRead = bytesAvailable / 2;
}
void setup() {
// start the serial communication and wait for the port to open
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("Serial ready. Initializing LED...");
// initialize the LED pins as outputs,
beginLED();
setoff();
Serial.println("LED ready. Initializing PDM microphone...");
// initialize the PDM microphoneand set the callback function to be called when data is available
PDM.onReceive(onPDMdata);
if (!PDM.begin(1, 16000)) {
Serial.println("Failed to initialize PDM!");
while (1) {
delay(1000);
}
}
}
void loop() {
int voice = 120;
int noise = 700;
if (samplesRead > 0) {
long sumSquares = 0;
for (int i = 0; i < samplesRead; i++) {
long sample = sampleBuffer[i];
sumSquares += sample * sample;
}
int rms = sqrt(sumSquares / samplesRead);
Serial.println("RMS: " + String(rms));
if (rms >= noise) {
setred();
} else if (rms >= voice) {
setgreen();
} else {
setyellow();
}
samplesRead = 0; // Reset for the next batch of samples
}
}
In setup(), to prior to initalizing the microphone (line 31), we will need to setup a callback function called onPDMdata (line 30).
begin() call (line 31). onPDMdata will run every 256/16000 = 0.016 ms To measure loudness, we need to look at the entire sampleBuffer array (declared in line 7).
loudness. This can be calculated using root-mean-square.loop() section of main.cpp implements the above approach.The remaining sensors of Nano33BLE include:
The approach to getting and analyzing data from these sensors are similar to that of the IMU and Microphone components. Documents and tutorials can be found at.