Sensors

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)

IMU

Accelerometer
  • Looking at the figure below, you can see how the various dimensions (z, x, y) are measured relative to the MCU’s position.
  • The documented measurement range is set at [-4, +4]g -/+0.122 mg.
    • This means that the MCU can measure acceleration as high as 4g or decceleration as low as -4g. This is roughly equivalent to an acceleration from 0 to 60 in 3-4s.
    • This also means that the MCU can measure acceleration changes as sensitive as 0.122mg (not perceptible by human).
Gyroscope
  • Looking at the figure below and observe the rotating directions.
    • The z rotation happens when the MCU is turned vertically.
    • When the MCU is placed horizontally, rotating on the long side of MCU is the x rotation, and rotating on the short side of the MCU is the y rotation.
  • Gyroscope range is set at [-2000, +2000] dps +/-70 mdps.
Magnetometer
  • Looking at the figure below, we have the dimension of the magnetic forces that impacting the MCU (in vertical position).
  • We can use this information to detect magnetic fields affecting the MCU.

Microphone

Overview

Programmatic design

Example: Noise Detector

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.

include/led_controller.h
1
2
3
4
5
void beginLED();
void emitColor(int color);
void setred();
void setyellow();
void setgreen();
src/led_controller.cpp
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.

src/main.cpp
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).

To measure loudness, we need to look at the entire sampleBuffer array (declared in line 7).

Other Sensors

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.