IOT

How to use IMU & Microphone on XIAO BLE nRF52840 Sense

We will learn how to use the LSM6DS3 IMU Sensor and PDM Microphone on the Seeed XIAO BLE nRF52840 Sense in this tutorial. Getting Started with Seeed XIAO BLE nRF52840 Sense is a follow-up post to the prior lesson.

The Seeed XIAO BLE nRF52840 Sense is used in this advanced tutorial. In this lesson, we’ll read data from the internal LSM6DS3 IMU Sensor’s accelerometer, gyroscope, and temperature sensor. Then we’ll make a pedometer with the IMU Sensor. A pedometer is a device that counts the number of steps taken to estimate the distance traveled on foot. In terms of the microphone, we’ll learn how to detect audio and sound using the PDM Microphone.

Finally, you’ll be able to use the IMU and microphone on the XIAO BLE nRF52840 Sense for Machine Learning and Artificial Intelligence projects.

Seeed XIAO BLE nRF52840 Sense

Let’s start with a quick overview of these boards. Nordic Semiconductor’s nRF52840 BLE 5.0 Chip is used in these boards. One of the boards is called Seeed XIAO BLE nRF52840 and the other is Seeed XIAO BLE nRF52840 Sense.

Seeed XIAO BLE nRF52840 Sense

These are ultra-low-power Bluetooth development boards that are compact in size. One of the boards has an inbuilt Bluetooth antenna as well as a battery charging chip, making it perfect for Internet of Things projects. The XIAO Sense board, on the other hand, offers extra features such as a 6-DOF IMU and a PDM microphone, making it a perfect board for running AI with TinyML and TensorFlow Lite.

A reset button, 6 DOF IMU LSM6DS3TR-C, Bluetooth Antenna, PDM Microphone, RGB LED, and power LED are located on the board’s topside. SWD Pins are located on the backside of the board and are used to debug and reflash the bootloader using JLink. The NFC Antenna and a 3.7V Lithium-Ion Battery are connected via a pair of pins.

There are 11 digital I/O pins that can be used as PWM pins and six analog I/O pins that can be used as ADC pins on this board. It supports all three common serial ports: UART, IIC, and SPI.

.

Reading LSM6DS3 IMU Sensor Value

A high-precision 6-axis Inertial Measurement Unit (IMU) with a 3-axis accelerometer and a 3-axis gyroscope is included in the XIAO BLE Sense. This module also has an inbuilt temperature sensor.

Now it’s time to get down to business. The value of the LSM6DS3 IMU Sensor will be read first from the Seeed XIAO BLE nRF52840 Sense. Connect the XIAO BLE Board to the Type C USB Cable. Then connect the cable’s opposite end to your computer. As a result, serial contact with the PC will be established.

To use the IMU Sensor, we need the LSM6DS3 library. You can download the library from the GitHub repository: LSM6DS3 Library.

Open your Arduino IDE once the library has been downloaded. Then, using the add zip folder, add the library.

Source Code/Program

Now copy the following code and paste it into your Arduino IDE.

#include “LSM6DS3.h”
#include “Wire.h”

//Create an instance of class LSM6DS3
LSM6DS3 myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
//Call .begin() to configure the IMUs
if (myIMU.begin() != 0) {
Serial.println(“Device error”);
} else {
Serial.println(“Device OK!”);
}
}

void loop() {
//Accelerometer
Serial.print(“\nAccelerometer:\n”);
Serial.print(” X1 = “);
Serial.println(myIMU.readFloatAccelX(), 4);
Serial.print(” Y1 = “);
Serial.println(myIMU.readFloatAccelY(), 4);
Serial.print(” Z1 = “);
Serial.println(myIMU.readFloatAccelZ(), 4);

//Gyroscope
Serial.print(“\nGyroscope:\n”);
Serial.print(” X1 = “);
Serial.println(myIMU.readFloatGyroX(), 4);
Serial.print(” Y1 = “);
Serial.println(myIMU.readFloatGyroY(), 4);
Serial.print(” Z1 = “);
Serial.println(myIMU.readFloatGyroZ(), 4);

//Thermometer
Serial.print(“\nThermometer:\n”);
Serial.print(” Degrees C1 = “);
Serial.println(myIMU.readTempC(), 4);
Serial.print(” Degrees F1 = “);
Serial.println(myIMU.readTempF(), 4);

delay(1000);
}
  • Select Seeed XIAO BLE Sense – nRF52840 as a board from the tools menu.
  • Also select the COM port.

The code can now be uploaded to your XIA BLE Sense Board.

Testing IMU Sensor

Open the Serial Monitor after the code has been uploaded.

The Serial Monitor displays the accelerometer, gyroscope, and thermometer readings. Both the accelerometer and the gyroscope’s X, Y, and Z measurements are in raw format. In the same way, the temperature is shown in both Celsius and Fahrenheit.

DIY Pedometer using IMU

In this second example, we’ll construct a pedometer with the LSM6DS3 IMU Sensor from Seeed XIAO BLE nRF52840 Sense. A pedometer, often known as a step-counter, is a small electrical or electromechanical device that counts each step a person takes by detecting motion in the hands or hips.

The pedometer code for XIAO BLE Sense is available in the example part. You can copy the example code or use the code below.

Source Code/Program

Copy the following code and paste it into your Arduino IDE.

#include “LSM6DS3.h”
#include “Wire.h”

#define CLEAR_STEP true
#define NOT_CLEAR_STEP false

//Create a instance of class LSM6DS3
LSM6DS3 pedometer(I2C_MODE, 0x6A); //I2C device address 0x6A

void setup()
{
Serial.begin(9600);
while (!Serial);
if (pedometer.begin() != 0)
{
Serial.println(“Device error”);
} else
{
Serial.println(“Device OK!”);
}

//Configure LSM6DS3 as pedometer
if (0 != config_pedometer(NOT_CLEAR_STEP))
{
Serial.println(“Configure pedometer fail!”);
}
Serial.println(“Success to Configure pedometer!”);
}

void loop()
{
uint8_t dataByte = 0;
uint16_t stepCount = 0;

pedometer.readRegister(&dataByte, LSM6DS3_ACC_GYRO_STEP_COUNTER_H);
stepCount = (dataByte << 8) & 0xFFFF;

pedometer.readRegister(&dataByte, LSM6DS3_ACC_GYRO_STEP_COUNTER_L);
stepCount |= dataByte;

Serial.print(“Step: “);
Serial.println(stepCount);

delay(500);
}

//Setup pedometer mode
int config_pedometer(bool clearStep)
{
uint8_t errorAccumulator = 0;
uint8_t dataToWrite = 0; //Temporary variable

//Setup the accelerometer******************************
dataToWrite = 0;

// dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_200Hz;
dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_2g;
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_26Hz;


// Step 1: Configure ODR-26Hz and FS-2g
errorAccumulator += pedometer.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);

// Step 2: Set bit Zen_G, Yen_G, Xen_G, FUNC_EN, PEDO_RST_STEP(1 or 0)
if (clearStep)
{
errorAccumulator += pedometer.writeRegister(LSM6DS3_ACC_GYRO_CTRL10_C, 0x3E);
} else
{
errorAccumulator += pedometer.writeRegister(LSM6DS3_ACC_GYRO_CTRL10_C, 0x3C);
}

// Step 3: Enable pedometer algorithm
errorAccumulator += pedometer.writeRegister(LSM6DS3_ACC_GYRO_TAP_CFG1, 0x40);

//Step 4: Step Detector interrupt driven to INT1 pin, set bit INT1_FIFO_OVR
errorAccumulator += pedometer.writeRegister(LSM6DS3_ACC_GYRO_INT1_CTRL, 0x10);

return errorAccumulator;
}

You can upload this code to your XIAO BLE Sense Board.

Testing the IMU-Based Pedometer

After the code is uploaded, open the Serial Monitor.

The Serial Monitor will display 0 steps at first. To correctly measure your steps, you must now place this device in your pocket and begin walking. In my situation, I’m going to share this module such that the step computation is based on the shaking motion. As a result, the more the shaking occurs, the more steps are added to the prior count.

So, by putting this IMU Sensor on the XIAO BLE nRF52840 Sense, you may construct your own pedometer.

Using PDM Microphone of BLE Sense

The XIAO BLE Sense has a PDM (Pulse Density Modulation) microphone that can receive audio data in real-time and be utilized for audio recognition. It can be used for more interesting TinyML applications like remote voice controlling of gadgets, thanks to the wireless connection and excellent performance in processing audio data thanks to the FPU.

In this tutorial, we’ll see if the voice has been detected. The raw data from the PDM Microphone will be seen in real-time on a Serial Monitor and Serial Plotter. The microphone detects the volume of sound, which determines the magnitude of voice detection.

For the coding part, you need to download Arduino Mic Library. This library is used to connect microphone sensors to Arduino-compatible hardware devices for sound recording and additional processing, such as computing FFT and MFCC.

Source Code/Program

Copy the following code and paste it into your Arduino IDE.

#include <mic.h>
#if defined(WIO_TERMINAL)
#include “processing/filters.h”
#endif

// Settings
#if defined(WIO_TERMINAL)
#define DEBUG 1 // Enable pin pulse during ISR
#define SAMPLES 16000*3
#elif defined(ARDUINO_ARCH_NRF52840)
#define DEBUG 1 // Enable pin pulse during ISR
#define SAMPLES 800
#endif

mic_config_t mic_config{
.channel_cnt = 1,
.sampling_rate = 16000,
.buf_size = 1600,
#if defined(WIO_TERMINAL)
.debug_pin = 1 // Toggles each DAC ISR (if DEBUG is set to 1)
#elif defined(ARDUINO_ARCH_NRF52840)
.debug_pin = LED_BUILTIN // Toggles each DAC ISR (if DEBUG is set to 1)
#endif
};

#if defined(WIO_TERMINAL)
DMA_ADC_Class Mic(&mic_config);
#elif defined(ARDUINO_ARCH_NRF52840)
NRF52840_ADC_Class Mic(&mic_config);
#endif

int16_t recording_buf[SAMPLES];
volatile uint8_t recording = 0;
volatile static bool record_ready = false;

#if defined(WIO_TERMINAL)
FilterBuHp filter;
#endif

void setup() {

Serial.begin(115200);
while (!Serial) {delay(10);}

#if defined(WIO_TERMINAL)
pinMode(WIO_KEY_A, INPUT_PULLUP);
#endif

Mic.set_callback(audio_rec_callback);

if (!Mic.begin()) {
Serial.println(“Mic initialization failed”);
while (1);
}

Serial.println(“Mic initialization done.”);

}

void loop() {

#if defined(WIO_TERMINAL)
if (digitalRead(WIO_KEY_A) == LOW && !recording) {

Serial.println(“Starting sampling”);
recording = 1;
record_ready = false;
}
#endif

#if defined(WIO_TERMINAL)
if (!recording && record_ready)
#elif defined(ARDUINO_ARCH_NRF52840)
if (record_ready)
#endif
{
Serial.println(“Finished sampling”);

for (int i = 0; i < SAMPLES; i++) {

//int16_t sample = filter.step(recording_buf[i]);
int16_t sample = recording_buf[i];
Serial.println(sample);
}

record_ready = false;
}
}

static void audio_rec_callback(uint16_t *buf, uint32_t buf_len) {

static uint32_t idx = 0;
// Copy samples from DMA buffer to inference buffer
#if defined(WIO_TERMINAL)
if (recording)
#endif
{
for (uint32_t i = 0; i < buf_len; i++) {

// Convert 12-bit unsigned ADC value to 16-bit PCM (signed) audio value
#if defined(WIO_TERMINAL)
recording_buf[idx++] = filter.step((int16_t)(buf[i] – 1024) * 16);
//recording_buf[idx++] = (int16_t)(buf[i] – 1024) * 16;
#elif defined(ARDUINO_ARCH_NRF52840)
recording_buf[idx++] = buf[i];
#endif

if (idx >= SAMPLES){
idx = 0;
recording = 0;
record_ready = true;
break;
}
}
}

}

You can upload this code to your XIAO BLE Sense Board.

Testing the PDM Microphone

After uploading the code, reopen the Serial Monitor. A random value is presented in the serial monitor when voice input is recognised. Go to tools and open the serial plotter to visualize the audio graph.

The rising and decreasing graph based on voice can be seen in the serial plotter. You can test the microphone by speaking into it.

IMU Microphone Seeed XIAO BLE Sense

So that’s how the Seeed XIAO BLE nRF52840 Sense’s PDM microphone works.

Conclusion

I hope all of you understand how to use IMU & Microphone on XIAO BLE nRF52840 Sense. We MATHA ELECTRONICS will be back soon with more informative blogs.

Leave a Reply

Your email address will not be published.