0

Two ADXL 345 sensors on Arduino
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Two ADXL 345 sensors on Arduino

by Onurb on Wed Apr 11, 2018 8:46 am

Hi, I am currently working on a project, where I need 2 ADXL 345 (Product ID: 1231) in a single setup. I tried to set the second ADXL to the alternate address (0x1D) by setting SDO/ALT Adress Pin to high, as I have found it in the ADXL data sheet. But when I do that my SW, can't access the adxl anymore. I tried setting the address in the constructor:

Adafruit_ADXL345_Unified gsensor_2 = Adafruit_ADXL345_Unified(0x1D);

But this didn't work. It basically doesn't matter what I write there instead of 0x1D, even with 0000 it still works with the default address of 0x53.

I also tried setting the address in the begin() method, but this doesn't work either. I get this error:

/Users/Bruno/Documents/Arduino/libraries/Adafruit_ADXL345-master/Adafruit_ADXL345_U.h:111:14: note: candidate expects 0 arguments, 1 provided
exit status 1
no matching function for call to 'Adafruit_ADXL345_Unified::begin(int)'

Can you tell me how I can address the two sensors? My whole sketch is below, I was trying to access the sensor on the alternate address first, before I fully set up the two sensors in my software.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

// Gib dem Sensor eine eindeutige Kennung
// Adafruit_ADXL345_Unified gsensor_1 = Adafruit_ADXL345_Unified(0x53);
Adafruit_ADXL345_Unified gsensor_2 = Adafruit_ADXL345_Unified(0x1D);

int Power = LOW;
int FeedbackCount = 0;
double Sensor_x = 0;
double Sensor_y = 0;
double Sensor_z = 0;
int count = 0;
int errorCode = 0;
int g_SensorMittelung = 10;
int SensorMitteilungsrate = 500; // Tiefer ist schneller 5000 = ca. 10 sekunden

void error(int code) {
   
  if (code == 1) {
   
      Serial.println("Problem mit der Kommunikation zum Sensor");
      while(1){
        digitalWrite (4, HIGH);
        delay (1000);
        digitalWrite (4, LOW);
        delay (1000);
      }
    }
   else {
      Serial.println("Unbekanntes Problem");
      while(1){
        digitalWrite (2, HIGH);
        delay (200);
        digitalWrite (2, LOW);
        delay (200);
      }
    }
}

void recordSensor(){
     int RateZwei = 2*SensorMitteilungsrate;
     Serial.println("Starte g-Daten Aufnahme");
     Serial.println("x    y    z");

     while (Power == HIGH) {
        Power = digitalRead(3);

        // Lese Sensor und berechne einen Durchschnittswert
        while (count < g_SensorMittelung) {

           // Erstelle neuen Sensor Event
           sensors_event_t event;
           gsensor_2.getEvent(&event);
   
          int currentSensor_x = event.acceleration.x;
          int currentSensor_y = event.acceleration.y;
          int currentSensor_z = event.acceleration.z;
   
          Sensor_x = Sensor_x + currentSensor_x;
          Sensor_y = Sensor_y + currentSensor_y;
          Sensor_z = Sensor_z + currentSensor_z;
          count++;
          FeedbackCount++;
          if (FeedbackCount > SensorMitteilungsrate) {
            digitalWrite(4, HIGH);
          }
          if (FeedbackCount > RateZwei) {
            digitalWrite(4, LOW);
            FeedbackCount = 0;
          }
        }
        Sensor_x = Sensor_x / count;
        Sensor_y = Sensor_y / count;
        Sensor_z = Sensor_z / count;
        count = 0;

        // Speichere die durchschnittlichen Sensorwerte
        Serial.print(Sensor_x); Serial.print("; ");
        Serial.print(Sensor_y);  Serial.print("; ");
        Serial.println(Sensor_z);     
     }
 
}

void setup() {
  pinMode(3, INPUT); // Power ON/OFF
  pinMode(4, OUTPUT); // ca. 1Hz Signal an Rakete
  pinMode(5, OUTPUT); // Befehl an Kamera
  pinMode(2, OUTPUT); // LED

  Serial.begin(9600);
  Serial.println("Initialisiere Beschleunigungssensor");
 
  // Initialisiere den Sensor
  if(!gsensor_2.begin())
  {
    // Problem mit der Kommunikation zum Sensor
    errorCode = 1;
    error(errorCode);
  }
  else{
      gsensor_2.setRange(ADXL345_RANGE_16_G);
      Serial.println("Erfolgreich initialisiert");
    }
}

void loop() {

  Serial.println("Warte auf Einschaltbefehl");
  while (Power == LOW) {

    Power = digitalRead(3);
    delay(1000);
  }
   
    Serial.println("Starte Kamera");
    digitalWrite (5, HIGH);
    delay (5000);
    digitalWrite (5, LOW);
    delay(5000);
 
     // Starte Aufnahme
     Serial.println("Starte Aufnahme");
     digitalWrite (2, HIGH);
     digitalWrite (5, HIGH);
     delay (200);
     digitalWrite (5, LOW);
     Serial.println("Aufnahme gestartet");
     delay (2000);
   
    recordSensor();

    // Beende Aufnahme
    Serial.println("Beende Aufnahme");
    digitalWrite (5, HIGH);
    delay (200);
    digitalWrite (5, LOW);
    Serial.println("Aufnahme beendet");
    delay (4000);
    digitalWrite (2, LOW);

    // Schalte Kamera aus
    Serial.println("Schalte Kamera aus");
    digitalWrite (5, HIGH);
    delay (7000);
    digitalWrite (5, LOW);
    Serial.println("Kamera ausgeschaltet");
    digitalWrite (4, LOW);
 }


Onurb
 
Posts: 2
Joined: Wed Apr 11, 2018 7:35 am

Re: Two ADXL 345 sensors on Arduino

by adafruit_support_carter on Thu Apr 12, 2018 1:23 pm

Looks like the library currently only support the default address. There's an outstanding PR that adds this feature:
https://github.com/adafruit/Adafruit_ADXL345/pull/15

adafruit_support_carter
 
Posts: 6635
Joined: Tue Nov 29, 2016 2:45 pm

Re: Two ADXL 345 sensors on Arduino

by Onurb on Fri Apr 13, 2018 2:51 pm

adafruit_support_carter wrote:Looks like the library currently only support the default address. There's an outstanding PR that adds this feature:
https://github.com/adafruit/Adafruit_ADXL345/pull/15


Thank you! This helped me out.

Onurb
 
Posts: 2
Joined: Wed Apr 11, 2018 7:35 am

Please be positive and constructive with your questions and comments.