edit

Getting Started with IMU

This repo demonstrates how to read IMU values from Wio Terminal.

3-Axis Digital Accelerator Initialization

To initialize the accelerator on Wio Terminal:

  • Setting output data rate: lis.setOutputDataRate(), 5Hz up to 5kHz.

  • Setting scale range: lis.FullScaleRange(), 2g up to 16g.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include"LIS3DHTR.h"
LIS3DHTR<TwoWire> lis;

void setup() {
    ...
    lis.begin(Wire1); 
    lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); // Setting output data rage to 25Hz, can be set up tp 5kHz 
    lis.setFullScaleRange(LIS3DHTR_RANGE_2G); // Setting scale range to 2g, select from 2,4,8,16g
    ...
}

Reading IMU Values

To read IMU values of each axis on Wio Terminal, simply follow this:

1
2
3
4
5
6
7
8
void loop() {
    float x_values, y_values, z_values;
    x_values = lis.getAcceleratationX();
    y_values = lis.getAcceleratationY();
    z_values = lis.getAcceleratationZ();
    delay(50);  // delay to avoid large amount of data being read
    ...
}

Note: Delay is strongly recommended to avoid large amount of data at once.

Example Code

This exmaple prints all 3-axis values of IMU on the serial monitor.

 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
#include"LIS3DHTR.h"
LIS3DHTR<TwoWire> lis;

void setup() {
  Serial.begin(115200);
  lis.begin(Wire1);

  if (!lis) {
    Serial.println("ERROR");
    while(1);
  }
  lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); //Data output rate
  lis.setFullScaleRange(LIS3DHTR_RANGE_2G); //Scale range set to 2g
}

void loop() {
  float x_values, y_values, z_values;
  x_values = lis.getAccelerationX();
  y_values = lis.getAccelerationY();
  z_values = lis.getAccelerationZ();

  Serial.print("X: "); Serial.print(x_values);
  Serial.print(" Y: "); Serial.print(y_values);
  Serial.print(" Z: "); Serial.print(z_values);
  Serial.println();
  delay(50);
}