1
\$\begingroup\$

I'm learning to use the Adafruit LSM9DS0 and have cobbled this code together such that an LED will turn on when it detects motion on the accelerometer, specifically when the magnitude is over a certain threshold (MOVE_THRESHOLD). I ran the code on an Uno and it worked great. Now that I'm trying to run it on a 3.3V Mini Pro, hence needing pin 10 always on to power the sensor, it's not working. When I comment out acceldata(), the pin is registering 5V unlike how it should register 3.3V as it's a 3.3V board (If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. ). Once acceldata() is implemented, the pin is not reading 3.3V or even 5V, but 0V. I'm sure this is something obvious in acceldata() that I'm missing, but I'm still missing it. I'm also not clear as to why pin 10 is reading 5V when it should be reading 3.3V.

Circuit Diagram

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS0.h>
#include <Adafruit_Sensor.h>

// i2c, calls the Flora 9DOF
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0();
// You can also use software SPI
//Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(13, 12, 11, 10, 9);
// Or hardware SPI! In this case, only CS pins are passed in
//Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(10, 9);

// mess with this number to adjust blinking
// lower number = more sensitive
#define MOVE_THRESHOLD 100

void setupSensor()
{
  // 1.) Set the accelerometer range
  lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G);

  // 2.) Set the magnetometer sensitivity
  lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS);

  // 3.) Setup the gyroscope
  lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
  //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS);
  //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS);
}

void setup() 
{
//Pin 10 to always be on to power the LSM9DS0 
pinMode(10,OUTPUT);
//Pin 13 to turn on when a change in acceleration is detected
pinMode(13,OUTPUT);


#ifndef ESP8266
  while (!Serial);     // will pause Zero, Leonardo, etc until serial console     opens
#endif
  Serial.begin(9600);
  Serial.println("LSM raw read demo");

  // Try to initialise and warn if we couldn't detect the chip
  if (!lsm.begin())
  {
    Serial.println("Oops ... unable to initialize the LSM9DS0. Check your wiring!");
    while (1);
  }
  Serial.println("Found LSM9DS0 9DOF");
  Serial.println("");
  Serial.println("");
}

void loop() 
{
  //Pin 10 to always be on to power the LSM9DS0 
  digitalWrite(10, HIGH);
  //Takes the acceleration data, produces vector, detects change in vector to turn on pin 13 LED
  acceldata();
}

void acceldata()
{
  lsm.read();
  Serial.print("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" ");
  Serial.print("Y: "); Serial.print((int)lsm.accelData.y);       Serial.print(" ");
  Serial.print("Z: "); Serial.println((int)lsm.accelData.z);     Serial.print(" ");
/*  Serial.print("Mag X: "); Serial.print((int)lsm.magData.x);     Serial.print(" ");
  Serial.print("Y: "); Serial.print((int)lsm.magData.y);         Serial.print(" ");
  Serial.print("Z: "); Serial.println((int)lsm.magData.z);       Serial.print(" ");
  Serial.print("Gyro X: "); Serial.print((int)lsm.gyroData.x);   Serial.print(" ");
  Serial.print("Y: "); Serial.print((int)lsm.gyroData.y);        Serial.print(" ");
  Serial.print("Z: "); Serial.println((int)lsm.gyroData.z);      Serial.println(" ");
  Serial.print("Temp: "); Serial.print((int)lsm.temperature);    Serial.println(" ");
*/
  delay(1000);

  // Get the magnitude (length) of the 3 axis vector
  // http://en.wikipedia.org/wiki/Euclidean_vector#Length
  double storedVector = lsm.accelData.x*lsm.accelData.x;
  storedVector += lsm.accelData.y*lsm.accelData.y;
  storedVector += lsm.accelData.z*lsm.accelData.z;
  storedVector = sqrt(storedVector);
  Serial.print("Len: "); Serial.println(storedVector);

  // wait a bit
  delay(50);

  // get new data!
  lsm.read();
  double newVector = lsm.accelData.x*lsm.accelData.x;
  newVector += lsm.accelData.y*lsm.accelData.y;
  newVector += lsm.accelData.z*lsm.accelData.z;
  newVector = sqrt(newVector);
  Serial.print("New Len: "); Serial.println(newVector);



  // are we moving 
  if (abs(newVector - storedVector) > MOVE_THRESHOLD) {
    Serial.println("Twinkle!");
    Serial.print("Delta_a: "); Serial.println(newVector - storedVector);
    digitalWrite(13, HIGH); 
  }
  else
  {
    digitalWrite(13, LOW);
  }
}
\$\endgroup\$
  • 2
    \$\begingroup\$ A schematic would be very helpful here. It's hard for us to guess what's going on without one. Also, are you powering the accelerometer directly from the digital output pin of the MCU? \$\endgroup\$ – Dan Laks Aug 15 '16 at 21:58
  • \$\begingroup\$ Thanks for the reply - I've added the circuit diagram. And yes, I am powering the accelerometer directly from the digital output pin (pin 10). \$\endgroup\$ – Proserpia Aug 16 '16 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.