8  Sensor and LED display

8.1 Accelerometer (LSM6DS3TR-C)

The XIAO nRF52840 Sense has a 6-axis IMU (3-axis accelerometer + 3-axis gyroscope), the LSM6DS3TR-C, connected over I²C.

#include "drv_imu.h"

#if USE_IMU
#include <LSM6DS3.h>
#include <Wire.h>

static LSM6DS3 s_imu(I2C_MODE, 0x6A);

void imu_init()
{
  // the XIAO nRF52840 Sense's IMU must be switched on via its power pin
  pinMode(PIN_LSM6DS3TR_C_POWER, OUTPUT);
  digitalWrite(PIN_LSM6DS3TR_C_POWER, HIGH);
  delay(50);
  s_imu.settings.accelRange = 4; // ±4 g
  if (s_imu.begin() != 0)
  {
    Serial.println("IMU init failed");
  }
}

uint16_t imu_read_magnitude_mg()
{
  // involves I2C communication, so call from a handler (main loop), not an interrupt
  float x = s_imu.readFloatAccelX();
  float y = s_imu.readFloatAccelY();
  float z = s_imu.readFloatAccelZ();
  return (uint16_t)(sqrtf(x * x + y * y + z * z) * 1000.0f);
}

#else
// dummy data for boards without an IMU (slowly oscillates around 1000 mg)
void imu_init() {}

uint16_t imu_read_magnitude_mg()
{
  float t = millis() / 1000.0f;
  return (uint16_t)(1000.0f + 150.0f * sinf(2.0f * PI * 0.2f * t) + random(-20, 21));
}
#endif

8.1.1 Points

  • IMU power pin: the IMU on the Sense version is not powered unless PIN_LSM6DS3TR_C_POWER is driven HIGH. Driving it LOW when not in use saves power
  • Where to read it: I²C communication takes several hundred µs to a few ms, and the library uses interrupts and waits internally. Read it in a handler (the main loop), not inside an ISR or a timer callback. The timer only posts an event saying “it’s time to read”
  • Absorbing board differences: when USE_IMU is 0 it returns dummy data, so even on the XIAO nRF52840 without an IMU you can try the other parts (state transitions, BLE) in the same way

8.1.2 Acceleration magnitude

\[ |a| = \sqrt{a_x^2 + a_y^2 + a_z^2} \]

is the magnitude of motion independent of the device’s orientation. At rest only gravitational acceleration is present, so it is about 1 g (1000 mg), and it fluctuates around that when the device moves.

NoteThe sensor’s FIFO

This sample reads once every 100 ms, but if you want equally spaced data at a higher sampling frequency (e.g. 100 Hz or more), an efficient approach is to let the IMU accumulate data in its internal FIFO, have it raise an interrupt when the FIFO reaches a certain level, and read it out in one go. The CPU can sleep in the meantime. For this interrupt too, the ISR only posts an event and the readout is done in a handler.

8.2 LED display: non-blocking

#include "drv_led.h"

// The XIAO nRF52840's RGB LED lights on LOW (active low)
static void write_rgb(bool r, bool g, bool b)
{
  digitalWrite(LED_RED, r ? LOW : HIGH);
  digitalWrite(LED_GREEN, g ? LOW : HIGH);
  digitalWrite(LED_BLUE, b ? LOW : HIGH);
}

void led_init()
{
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  write_rgb(false, false, false);
}

void led_set(LedColor color)
{
  write_rgb(color == LED_COLOR_RED, color == LED_COLOR_GREEN, color == LED_COLOR_BLUE);
}

void led_blink(LedColor color, uint16_t on_ms, uint16_t period_ms)
{
  // decide on/off from the elapsed time modulo the period (stateless)
  bool on = (millis() % period_ms) < on_ms;
  led_set(on ? color : LED_COLOR_OFF);
}

8.2.2 Computing on/off from millis()

led_blink() decides on or off from the remainder of the current time divided by the period.

bool on = (millis() % period_ms) < on_ms;
  • It holds no state (when it was last turned on), so it never breaks no matter where or how often it is called
  • If it is called often enough (in this sample 10 Hz or more, thanks to the 100 ms timer), it blinks as intended

8.2.3 Where ui_update() sits

ui_update() in main.cpp re-decides the display from the current state every time an event is processed. Making the display “a function of the state” has the advantages that

  • handlers don’t need to manipulate the LED (handlers can concentrate on state transitions and processing)
  • when you add a state, the display change is needed in only one place, ui_update()
State LED
WAIT (not connected) Green, short blink once every 2 s
WAIT (connected) Blue, short blink once every 1 s
MEAS Red, short blink once every 0.5 s
TipExercise

Observe the LED while idle with the 100 ms timer stopped (don’t call timer_init()). What happens to the blinking? Why?

ui_update() is called only when an event is processed, so if no events come the LED is not updated and stays stuck either on or off. This is the limit of a design that makes display updates piggyback on events. You can provide a periodic event (timer) just for the display, or leave LED blinking to hardware (the PWM peripheral).