ONE – Accelerometer + Magnetometer

 

Accelerometer + magnetometer

The SODAQ ONE V1 and V2 have a different accelerometer. On the ONE v1 there is the LSM303 which is not only an accelerometer but also an magnetometer. We moved in ONE v2 to the LIS3DE for its low power features.

Go to Sodaq One V1 – LSM303D
Go to Sodaq One V2 – LIS3DE
Go to Sodaq One V3 – LSM303AGR

LSM303D / ONE v1

Install the library

Go to: Sketch → Include Library → Manage Libraries…
Search for “LSM303”
Install the LSM303 by Pololu

Sample Sketch

#include <Wire.h>
#include <LSM303.h>

LSM303 compass;

char report[80];

void setup()
{
  SerialUSB.begin(9600);
  Wire.begin();
  compass.init();
  compass.enableDefault();
}

void loop()
{
  compass.read();

  snprintf(report, sizeof(report), "A: %6d %6d %6d    M: %6d %6d %6d",
    compass.a.x, compass.a.y, compass.a.z,
    compass.m.x, compass.m.y, compass.m.z);
  SerialUSB.println(report);

  delay(100);
}

LIS3DE / ONE v2

Library

Download the Sodaq_LIS3DE library from the Arduino Library Manager
or
Download the LIS3DE Library here: Sodaq_LIS3DE
Move the folder to Documents->Arduino->libraries

/* Example for using the accelerometer with the Sodaq_LSM303AGR library */
/* The example is tested on an Arduino M0 and a Sodaq Explorer with a Sodaq NB-IoT shield */
/* This code prints accelerometer readings every second and registers whether the board is flipped by interrupt */
#include <Arduino.h>
#include <Sodaq_LIS3DE.h>

Sodaq_LIS3DE accelerometer;

// Print the debug information on the SerialUSB 
#define USB SerialUSB

// The interrupt pin for the Accelerometer is attached to D4
#define ACC_INT_PIN 4

// Threshold for interrupt trigger
double threshold = -0.8;
int now = millis();

void setup() {
    USB.begin(9600);
    while ((!USB) && (millis() < 10000)) {
        // Wait 10 seconds for the Serial Monitor
    }

    // Start the I2C bug
    Wire.begin();

    pinMode(ACCEL_INT1, INPUT);
    attachInterrupt(ACCEL_INT1, interrupt_event, CHANGE);

    // Configure EIC to use GCLK1 which uses XOSC32K, XOSC32K is already running in standby
    // This has to be done after the first call to attachInterrupt()
    GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(GCM_EIC) |
        GCLK_CLKCTRL_GEN_GCLK1 |
        GCLK_CLKCTRL_CLKEN;

    accelerometer.enable(true,
        Sodaq_LIS3DE::NormalLowPower10Hz,
        Sodaq_LIS3DE::XYZ,
        Sodaq_LIS3DE::Scale8g,
        true);
    
    delay(100);

    accelerometer.enableInterrupt1(
        Sodaq_LIS3DE::XHigh | Sodaq_LIS3DE::XLow | Sodaq_LIS3DE::YHigh | Sodaq_LIS3DE::YLow | Sodaq_LIS3DE::ZHigh | Sodaq_LIS3DE::ZLow,
        20 * 8.0 / 100.0,
        0,
        Sodaq_LIS3DE::MovementRecognition);
}

void loop() {

    // Print sensor readings every second
    if ((now + 1000) < millis())
    {
        now = millis();
        read_AccMeter();
    }
}

void read_AccMeter()
{
    USB.print("x = ");
    USB.print(accelerometer.getX());

    USB.print("\ty = ");
    USB.print(accelerometer.getY());

    USB.print("\tz = ");
    USB.println(accelerometer.getZ());
}

void interrupt_event()
{
    // Do not print in an interrupt event when sleep is enabled.
    USB.println("Board flipped");
}

LSM303AGR / ONE v3

Download the Sodaq_LSM303AGR library from the Arduino Library Manager or from our GitHub.

/* Example for using the accelerometer with the Sodaq_LSM303AGR library */
/* The example is tested on an Arduino M0 and a Sodaq Explorer with a Sodaq NB-IoT shield */
/* This code prints accelerometer readings every second and registers whether the board is flipped by interrupt */
#include <Arduino.h>
#include <Sodaq_LSM303AGR.h>

Sodaq_LSM303AGR AccMeter;

// Print the debug information on the SerialUSB 
#define USB SerialUSB

// The interrupt pin for the Accelerometer is attached to D4
#define ACC_INT_PIN 4

// Threshold for interrupt trigger
double threshold = -0.8;
int now = millis();

void setup() {
    USB.begin(9600);
    while ((!USB) && (millis() < 10000)) {
        // Wait 10 seconds for the Serial Monitor
    }

    // Start the I2C bug
    Wire.begin();

    AccMeter.rebootAccelerometer();
    delay(1000);

    // Enable the Accelerometer
    AccMeter.enableAccelerometer();

    // Attach interrupt event fot the Accelerometer
    pinMode(ACC_INT_PIN, INPUT);
    attachInterrupt(ACC_INT_PIN, interrupt_event, RISING);

    // Enable interrupts on the SAMD 
    GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(GCM_EIC) |
        GCLK_CLKCTRL_GEN_GCLK1 |
        GCLK_CLKCTRL_CLKEN;

    // If Z goes below threshold the interrupt is triggered
    AccMeter.enableInterrupt1(AccMeter.ZLow, threshold, 0, AccMeter.PositionRecognition);
}

void loop() {

    // Print sensor readings every second
    if ((now + 1000) < millis())
    {
        now = millis();
        read_AccMeter();
    }
}

void read_AccMeter()
{
    USB.print("x = ");
    USB.print(AccMeter.getX());

    USB.print("\ty = ");
    USB.print(AccMeter.getY());

    USB.print("\tz = ");
    USB.println(AccMeter.getZ());
}

void interrupt_event()
{
    // Do not print in an interrupt event when sleep is enabled.
    USB.println("Board flipped");
}