ADXL345 Set Range

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL345 Set Range

Post by adafruit_support_bill »

You should be able to just update your library from Github. The necessary changes are in the version Kevin posted this morning.

ScienceRules
 
Posts: 24
Joined: Mon Jun 24, 2013 8:09 am

Re: ADXL345 Set Range

Post by ScienceRules »

After uploading the new library a second time, my program is compiling.

However the results of the logging are still not at the proper scale.

Thank you for all of your efforts towards resolving this.

Ed

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL345 Set Range

Post by adafruit_support_bill »

Hmmm. Let me look into that. For reference, can you post the code you are using?

ScienceRules
 
Posts: 24
Joined: Mon Jun 24, 2013 8:09 am

Re: ADXL345 Set Range

Post by ScienceRules »

This code appears to run ok with the exception that since the +_16 range is selected the output needs to be multiplied by 8 (2(default range) x8 = 16).

Ed

Code: Select all

#include "SD.h"
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345.h>

/* A combination of ADXL345 sensor test sketch from github library and
data logger from  https://
github.com/adafruit/Light-and-Temp-logger/blob/master/lighttemplogger.pde*/


// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 10 / mills between entries

// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to 
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 100 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 3 //originally pin 2
#define greenLEDpin 4 //originally pin 3

/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345 accel = Adafruit_ADXL345(12345);

RTC_DS1307 RTC; // define the Real Time Clock object  
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10; 

// the logging file
File logfile;

//void error(char *str)
//{  
//   Serial.print("error: ");  
 //  Serial.println(str);    

   // red LED indicates error  digitalWrite(redLEDpin, HIGH);

//   while(1);
//}

void displaySensorDetails(void)
{
   sensor_t sensor;
   accel.getSensor(&sensor);
}
/////////

void displayDataRate(void)
{
  Serial.print  ("Data Rate:    "); 
  
  switch(accel.getDataRate())
  {
    case ADXL345_DATARATE_3200_HZ:
      Serial.print  ("3200 "); 
      break;
    case ADXL345_DATARATE_1600_HZ:
      Serial.print  ("1600 "); 
      break;
    case ADXL345_DATARATE_800_HZ:
      Serial.print  ("800 "); 
      break;
    case ADXL345_DATARATE_400_HZ:
      Serial.print  ("400 "); 
      break;
    case ADXL345_DATARATE_200_HZ:
      Serial.print  ("200 "); 
      break;
    case ADXL345_DATARATE_100_HZ:
      Serial.print  ("100 "); 
      break;
    case ADXL345_DATARATE_50_HZ:
      Serial.print  ("50 "); 
      break;
    case ADXL345_DATARATE_25_HZ:
      Serial.print  ("25 "); 
      break;
    case ADXL345_DATARATE_12_5_HZ:
      Serial.print  ("12.5 "); 
      break;
    case ADXL345_DATARATE_6_25HZ:
      Serial.print  ("6.25 "); 
      break;
    case ADXL345_DATARATE_3_13_HZ:
      Serial.print  ("3.13 "); 
      break;
    case ADXL345_DATARATE_1_56_HZ:
      Serial.print  ("1.56 "); 
      break;
    case ADXL345_DATARATE_0_78_HZ:
      Serial.print  ("0.78 "); 
      break;
    case ADXL345_DATARATE_0_39_HZ:
      Serial.print  ("0.39 "); 
      break;
    case ADXL345_DATARATE_0_20_HZ:
      Serial.print  ("0.20 "); 
      break;
    case ADXL345_DATARATE_0_10_HZ:
      Serial.print  ("0.10 "); 
      break;
    default:
      Serial.print  ("???? "); 
      break;
  }  
  Serial.println(" Hz");  
}

//////////
void displayRange(void)
{
  Serial.print  ("Range:         +/- "); 
  
 switch(accel.getRange())
  {
    case ADXL345_RANGE_16_G:
      Serial.print  ("16 "); 
      break;
    case ADXL345_RANGE_8_G:
      Serial.print  ("8 "); 
      break;
    case ADXL345_RANGE_4_G:
      Serial.print  ("4 "); 
      break;
    case ADXL345_RANGE_2_G:
      Serial.print  ("2 "); 
      break;
    default:
      Serial.print  ("?? "); 
      break;
  }  
  }

void setup(void) 
{
   Serial.begin(9600);
   /* Initialise the sensor */
   Wire.begin(); 
   if(!accel.begin())
   {
      /* There was a problem detecting the ADXL345 ... check your connections */
      Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
      while(1);
   }
   accel.setRange(ADXL345_RANGE_16_G);


   //  Serial.println("Accelerometer Readout"); 
   //  Serial.println("");
   // initialize the SD card
   // Serial.print("Initializing SD card...");
   // make sure that the default chip select pin is set to  
   // output, even if you don't use it:
   pinMode(10, OUTPUT);
   // see if the card is present and can be initialized:
   if (!SD.begin(chipSelect)) 
   {
    //  error("Card failed, or not present");
   }
   //   Serial.println("card initialized.");  

   // create a new file
   char filename[] = "LOGGER00.CSV";
   for (uint8_t i = 0; i < 100; i++) 
   {
      filename[6] = i/10 + '0';
      filename[7] = i%10 + '0';
      if (! SD.exists(filename)) 
      {
         // only open a new file if it doesn't exist
         logfile = SD.open(filename, FILE_WRITE);
         break;  // leave the loop!
      }
      // use debugging LEDs
      pinMode(redLEDpin, OUTPUT);
      pinMode(greenLEDpin, OUTPUT);
      // connect to RTC
      Wire.begin();  
      if (!RTC.begin()) {
         logfile.println("RTC failed");
      }
   }

   if (! logfile) 
   {
    //  error("couldnt create file");
   }

}

void loop(void) 
{
   /* Get a new sensor event */
   sensors_event_t event; 
   accel.getEvent(&event);

   logfile.print(", ");
   logfile.print("X: "); 
   logfile.print(", ");   
   logfile.print(event.acceleration.x/9.8); 
   logfile.print(", ");   
   logfile.print("Y: "); 
   logfile.print(", ");   
   logfile.print(event.acceleration.y/9.8); 
   logfile.print(", ");   
   logfile.print("Z: "); 
   logfile.print(", ");   
   logfile.print(event.acceleration.z/9.8);   
   logfile.println(" ");   

   DateTime now;

   digitalWrite(greenLEDpin, HIGH);

   // fetch the time
   now = RTC.now();

   // You know, 
   // you really only need to log the timestamp once per second.
   //Do that, and you can hit 100Hz easily.

   // log time
   // logfile.print(now.unixtime()); // seconds since 1/1/1970
   // logfile.print(", ");
   // logfile.print('"');
   // logfile.print(now.year(), DEC);
   // logfile.print("/");
   // logfile.print(now.month(), DEC);
   // logfile.print("/");
   //  logfile.print(now.day(), DEC);
   //  logfile.print(" ");
   //logfile.print(now.hour(), DEC);
   // logfile.print(":");

   logfile.print(now.minute(), DEC);
   logfile.print(":");
   logfile.print(now.second(), DEC);
   logfile.print('"');

   digitalWrite(greenLEDpin, LOW);
   digitalWrite(redLEDpin, HIGH);
   logfile.flush();
   digitalWrite(redLEDpin, LOW);

}

User avatar
ktownsend
 
Posts: 1447
Joined: Thu Nov 05, 2009 2:18 am

Re: ADXL345 Set Range

Post by ktownsend »

I just pushed a small update (I introduce a bug with the last push), but setting the gyroscope to +/- 16g I can see values > 512, but you have to shake the thing pretty hard to get values in that range. 3G is about 105km/h (65mph), but I was able to generate that shaking it pretty hard for an accel value around 890.

890 (raw units from the ADXL345) * 0.0039G (G per LSB) = 3.471g
3.471g = 122.54 km/h of acceleration ... 3.471G * 9.80665 m/s² = 34.03888km/s = 122.54 km/h

ScienceRules
 
Posts: 24
Joined: Mon Jun 24, 2013 8:09 am

Re: ADXL345 Set Range

Post by ScienceRules »

Thank you.

My intended use for this setup (at the +-16 range) is with my Auto Body Repair and Refinishing students at the career and technical education high school where I teach science integrated into a number of programs.

To illustrate the theory behind crush/crumple zones in cars, the students build a model car that has an egg as a passenger. They build a crumple zone on the model car using glue gun glue, spaghetti, soda straws, and a limited amount of paper. The car is run down a ramp into fixed block. If they do a good job, the egg survives. If not, we have a hose and squeegee ready to clean the shop floor.

In the past, we estimated the force of collision by using the crushing distance as a surrogate. With a +- 16 g accelerometer I hope to capture the appropriate measurements. If not, I see that Adafruit sells accelerometer with a range up to 200g !

My model is a prototype for this. The actual ones I plan to use will be built my Computer Technology students.

I also have various biomechanics applications (including the accelerations to the lower back from sneezing in various postures) in mind for this logger/accelerometer (using the +-2g range).

In the past I have worked with basic and Fortran IV (I'm showing my age here). C++ is brand new to me and quite a different critter.

Thank you again

Ed

ScienceRules
 
Posts: 24
Joined: Mon Jun 24, 2013 8:09 am

Re: ADXL345 Set Range

Post by ScienceRules »

It works like a champ.

When I slam the sensor I am getting readings from 9.86g to -6.48g.

Excellent!

:D

Ed

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”