I’m having trouble getting the LSM303 to work.
First of all I have to comment out:
/*
unsigned int millis_start = millis();
while (Wire.available() < 6) {
if (io_timeout > 0 && ((unsigned int)millis() - millis_start) > io_timeout)
{
did_timeout = true;
return;
}
}*/
For the Sodaq not to hang. Second, I’m only getting -1 values.
Inital communication to the device seems to work through:
if (!lsm303.init(LSM303::device_D, LSM303::sa0_low)) {
debugSerial.println(“Initialization of the LSM303 failed!”);
return;
}
else {
debugSerial.println(“Initialization of the LSM303 complete!”);
lsm303.enableDefault();
}
Anyone that can help? I’m using the version 2 of the Sodaq LoraWan.
I am also using the version 2 of the Sodaq Lora and I would appreciate if you could share some example of how to use the accelerometer.
Do you have an example which works with the interruption? For example, wake up Sodaq Lora when it has moved. In the schematics it is not clear where those interruptions lines go to.
Do you have (or anyone else) a glue of how to access the accelerometer and how to put the board in deep sleep mode to be waked up by the accelerometer?
Could you at least tell us which are the interrupt pins in the MCU? Thanks!
I wrote the code below using the SodaqOne-UniversalTracker-v2 as baseline.
Although it has the code for managing interrupts, It is not activated.
Please improve the code and share.
// Code for LIS3DE accelerometer inside SODAQ v2
#include <Arduino.h>
#include <Wire.h>
#include "Sodaq_RN2483.h"
#include "Sodaq_wdt.h"
#include "LIS3DE.h"
#include "RTCTimer.h"
#include "RTCZero.h"
#define debugSerial SerialUSB
#define loraSerial Serial1
#define MAX_RTC_EPOCH_OFFSET 25
RTCZero rtc;
RTCTimer timer;
LIS3DE accelerometer;
double x, y, z;
String xDoble, yDouble, zDouble;
volatile bool isOnTheMoveActivated;
volatile uint32_t lastOnTheMoveActivationTimestamp;
uint32_t getNow();
void initRtc();
void rtcAlarmHandler();
void resetRtcTimerEvents();
void initSleep();
void accelerometerInt1Handler();
volatile bool minuteFlag;
static bool isOnTheMoveInitialized;
void setAccelerometerTempSensorActive(bool on);
void setup() {
//Power up the LoRaBEE
pinMode(ENABLE_PIN_IO, OUTPUT); // ONE
digitalWrite(ENABLE_PIN_IO, HIGH); // ONE
delay(3000);
Wire.begin();
while ((!SerialUSB) && (millis() < 10000)){
// Wait 10 seconds for the Serial Monitor
}
//Set baud rate
debugSerial.begin(57600);
loraSerial.begin(LoRaBee.getDefaultBaudRate());
debugSerial.println("Starting...");
isOnTheMoveInitialized = true;
// Activate interrupt on the move
initOnTheMove();
}
void loop(){
/*
x = accelerometer.getGsFromScaledValue(.readRegister(LIS3DE::OUT_X));
y = accelerometer.getGsFromScaledValue(readRegister(LIS3DE::OUT_Y));
z = accelerometer.getGsFromScaledValue(a.readRegister(LIS3DE::OUT_Z));
*/
x=accelerometer.getX();
y=accelerometer.getY();
z=accelerometer.getZ();
debugSerial.print("X: "); debugSerial.println(x);
debugSerial.print("Y: "); debugSerial.println(y);
debugSerial.print("Z: "); debugSerial.println(z);
}
/*
/**
* Returns the board temperature.
int8_t getBoardTemperature()
{
setAccelerometerTempSensorActive(true);
int8_t temp = params.getTemperatureSensorOffset() + accelerometer.getTemperatureDelta();
setAccelerometerTempSensorActive(false);
return temp;
}
*/
/**
* Initializes the on-the-move functionality (interrupt on acceleration).
*/
void initOnTheMove()
{
pinMode(ACCEL_INT1, INPUT);
attachInterrupt(ACCEL_INT1, accelerometerInt1Handler, 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, LIS3DE::NormalLowPower10Hz, LIS3DE::XYZ, LIS3DE::Scale4g, true);
sodaq_wdt_safe_delay(10);
accelerometer.enableInterrupt1(
LIS3DE::XHigh | LIS3DE::XLow | LIS3DE::YHigh | LIS3DE::YLow | LIS3DE::ZHigh | LIS3DE::ZLow,
18.75 * 8.0 / 100.0,
100,
LIS3DE::MovementRecognition);
}
/**
* Runs every time acceleration is over the limits
* set by the user (if enabled).
*/
void accelerometerInt1Handler()
{
if (digitalRead(ACCEL_INT1)) {
// debugPrintln("On-the-move is triggered");
isOnTheMoveActivated = true;
lastOnTheMoveActivationTimestamp = getNow();
}
}
/**
* Initializes the accelerometer or puts it in power-down mode
* for the purpose of reading its temperature delta.
*/
void setAccelerometerTempSensorActive(bool on)
{
// if on-the-move is initialized then the accelerometer is enabled anyway
if (isOnTheMoveInitialized) {
return;
}
if (on) {
accelerometer.enable(false, LIS3DE::NormalLowPower100Hz, LIS3DE::XYZ, LIS3DE::Scale2g, true);
sodaq_wdt_safe_delay(30); // should be enough for initilization and 2 measurement periods
}
else {
accelerometer.disable();
}
}
/**
* Returns the current datetime (seconds since unix epoch).
*/
uint32_t getNow()
{
return rtc.getEpoch();
}
void initRtc()
{
rtc.begin();
// Schedule the wakeup interrupt for every minute
// Alarm is triggered 1 cycle after match
rtc.setAlarmSeconds(59);
rtc.enableAlarm(RTCZero::MATCH_SS); // alarm every minute
// Attach handler
rtc.attachInterrupt(rtcAlarmHandler);
// This sets it to 2000-01-01
rtc.setEpoch(0);
}
/**
* Runs every minute by the rtc alarm.
*/
void rtcAlarmHandler()
{
minuteFlag = true;
}
/**
* Initializes the RTC Timer and schedules the default events.
*/
void initRtcTimer()
{
timer.setNowCallback(getNow); // set how to get the current time
timer.allowMultipleEvents();
resetRtcTimerEvents();
}
Dear Pablo and fellows, I am a beginner on Accelerometer LIS3DE. The code above seem to work with serial output. Can any one please attach a code with interrupts enabled? Like accelerometer awakes the board from deep sleep mode. Your kind help is appreciated.
Dear, I have not had time to work further on this. I will update once I have more information. Hope that you manage to get up and running on this. Br, Adeel.
// This sample code was executed on SODAQ ONE Version 2
// Roger Rabbit took this informations from (2017-11-17)…
// https://github.com/SodaqMoja/Sodaq_LIS3DE/blob/master/Readme.md #include <Sodaq_LIS3DE.h> #include <Wire.h> #define debugSerial SerialUSB
// Constructor with optional Wire instance and address parameters.
// Sodaq_LIS3DE(TwoWire& wire = Wire, uint8_t address = LIS3DE_ADDRESS)
Sodaq_LIS3DE accelerometer;
// Enables the sensor with the default values or the given data rate, axes and scale.
accelerometer.enable();
// optinal: Resets all the registers of the sensor.
accelerometer.reboot();
}
void loop()
{
// Returns the temperature delta as read from the sensor (8-bit int).
uint8_t tempDelta = accelerometer.getTemperatureDelta();
debugSerial.println("temp: " + String(tempDelta));
double x = accelerometer.getX(); // Returns a measurement of the X axis.
double y = accelerometer.getY(); // Returns a measurement of the Y axis.
double z = accelerometer.getZ(); // Returns a measurement of the Z axis.