I need help with Arduino IDE adding adxl345 to my current code

I have got this problem I am doing school project in arduino this is my like 3rd project first 3 were much easier now I am doing "black box" or Flight data recording I am using Arduino Uno, Breadboard, Neo-6M GPS, BMP280 and adxl345 and MicroSD card module. I have functioning code with everything when I am using code without adxl345 sensor. This code that I provide does work perfectly it basically measures all the data that I have in code and puts into the table. Code:


#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_BMP280.h>

static const int RXPin = 4, TXPin = 3; // Change these pins according to your setup
static const uint32_t GPSBaud = 9600;
File dataFile;
unsigned long lastSaveTime = 0; // Variable to keep track of the last time data was saved

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

// Create an instance of BMP280 sensor
Adafruit_BMP280 bmp280;

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

  // Initialize SD card
  if (!SD.begin(10))
  {
    Serial.println("SD Card initialization failed!");
    return;
  }

  // Initialize BMP280 sensor
  if (!bmp280.begin(0x76))
  {
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }
}

void loop()
{
  while (ss.available() > 0)
  {
    if (gps.encode(ss.read()))
      saveLocation();
  }

  // Check if it's time to save the data
  if (millis() - lastSaveTime >= 5000)
  {
    if (saveLocation()) { // Save sensor data to SD card and print message if successful
      Serial.println("Data has been written");
    }
    lastSaveTime = millis(); // Update the last save time
  }

  delay(100); // Add a short delay to prevent excessive CPU usage
}

bool saveLocation()
{
  // Open the data file in write mode
  dataFile = SD.open("gps_data.txt", FILE_WRITE);

  if (dataFile)
  {
    // Write headers to the file if the file is empty
    if (dataFile.size() == 0) {
      dataFile.println("Latitude\tLongitude\tTemperature (C)\tPressure (hPa)\tAltitude (m)");
    }

    // Write data to the file
    dataFile.print(gps.location.lat(), 6);
    dataFile.print("\t");
    dataFile.print(gps.location.lng(), 6);
    dataFile.print("\t");
    dataFile.print(bmp280.readTemperature(), 2); // 2 decimal places for temperature
    dataFile.print("\t\t"); // Leave space for Pressure and Altitude
    dataFile.print(bmp280.readPressure() / 100.0, 2); // Convert Pa to hPa, 2 decimal places for pressure
    dataFile.print("\t\t");
    dataFile.println(bmp280.readAltitude(1013.25), 2); // Use sea level pressure for altitude calculation, 2 decimal places for altitude

    // Close the file
    dataFile.close();

    // Print to Serial in table format
    Serial.print(gps.location.lat(), 6);
    Serial.print("\t");
    Serial.print(gps.location.lng(), 6);
    Serial.print("\t");
    Serial.print(bmp280.readTemperature(), 2);
    Serial.print("\t");
    Serial.print(bmp280.readPressure() / 100.0, 2);
    Serial.print("\t");
    Serial.println(bmp280.readAltitude(1013.25), 2);

    return true; // Return true indicating successful write
  }
  else
  {
    Serial.println("Error opening data file!");
    return false; // Return false indicating failure
  }
}

As soon as I add code with adxl345 so it measures X,Y,Z acceleration it doesnt print anything to serial other then "Error opening data file!". I need to assume that something wrong is in the code becouse I tried adxl345 work with other components and also standalone and component works fine also I check I2P adress and its 0x53 I have like this GND, 5V and SDA and SCL connected to SDA and SCL pins on arduino uno right above the digital 13 pin. I am using those becouse BMP280 is already on analog pins A4 and A5. This is the code I am trying to use that contains also ADXL345 but it gives Error opening data file!:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h> // Include library for ADXL345 sensor

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
sensors_event_t event; // Declare event variable at a higher scope

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
File dataFile;
unsigned long lastSaveTime = 0;

TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);

Adafruit_BMP280 bmp280;

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

  if (!SD.begin(10))
  {
    Serial.println("SD Card initialization failed!");
    return;
  }

  if (!bmp280.begin(0x76))
  {
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }

  if (!accel.begin())
  {
    Serial.println("Could not find a valid ADXL345 sensor, check wiring!");
    while (1);
  }
}

void loop()
{
  accel.getEvent(&event); // Read accelerometer data in the loop

  while (ss.available() > 0)
  {
    if (gps.encode(ss.read()))
      saveLocation();
  }

  if (millis() - lastSaveTime >= 5000)
  {
    if (saveLocation()) {
      Serial.println("Data has been written");
    }
    lastSaveTime = millis();
  }

  delay(100);
}

bool saveLocation()
{
  dataFile = SD.open("gps_data.txt", FILE_WRITE);

  if (dataFile)
  {
    if (dataFile.size() == 0) {
      dataFile.println("Latitude\tLongitude\tTemperature (C)\tPressure (hPa)\tAltitude (m)\tX\tY\tZ"); // Add X, Y, Z headers
    }

    dataFile.print(gps.location.lat(), 6);
    dataFile.print("\t");
    dataFile.print(gps.location.lng(), 6);
    dataFile.print("\t");
    dataFile.print(bmp280.readTemperature(), 2);
    dataFile.print("\t");
    dataFile.print(bmp280.readPressure() / 100.0, 2);
    dataFile.print("\t");
    dataFile.print(bmp280.readAltitude(1013.25), 2);
    dataFile.print("\t");

    // Write X, Y, Z measurements to file
    dataFile.print(event.acceleration.x);
    dataFile.print("\t");
    dataFile.print(event.acceleration.y);
    dataFile.print("\t");
    dataFile.println(event.acceleration.z);

    dataFile.close();

    Serial.print(gps.location.lat(), 6);
    Serial.print("\t");
    Serial.print(gps.location.lng(), 6);
    Serial.print("\t");
    Serial.print(bmp280.readTemperature(), 2);
    Serial.print("\t");
    Serial.print(bmp280.readPressure() / 100.0, 2);
    Serial.print("\t");
    Serial.print(bmp280.readAltitude(1013.25), 2);
    Serial.print("\t");
    Serial.print(event.acceleration.x);
    Serial.print("\t");
    Serial.print(event.acceleration.y);
    Serial.print("\t");
    Serial.println(event.acceleration.z);

    return true;
  }
  else
  {
    Serial.println("Error opening data file!");
    return false;
  }
}

If anyone know what could cause this issue and could help me since I am not really good with coding I would really appreciate it.

have you tested the adxl345 alone?

Yes i did it works fine alone and also with some components. Its only issue when I am trying to write data to the file for some reason with all the components combined.

you are possibly hitting a memory issue on your UNO

do you get a warning at the end of the compilation process that memory is low?

first thing to do, use the F() macro to get the printed texts into flash memory ➜ change all the

    Serial.print("xxx");
    Serial.println("xxx");
    dataFile.print("xxx");
    dataFile.println("xxx");

into

    Serial.print(F("xxx"));
    Serial.println(F("xxx"));
    dataFile.print(F("xxx"));
    dataFile.println(F("xxx"));

when you print only 1 character like

    dataFile.print("\t");

use instead

    dataFile.write('\t');

and see how much RAM you saved and if it works better

So I think u might have helped me with this one and in fact it was the memory I am using this code now and it works very well but I had to remove decimals since macro F doesnt support 2 values. I got this error when I was trying to use decimal numbers

C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:77:43: error: macro "F" passed 2 arguments, but takes just 1
     dataFile.print(F(gps.location.lat(), 6));
                                           ^
C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:79:43: error: macro "F" passed 2 arguments, but takes just 1
     dataFile.print(F(gps.location.lng(), 6));
                                           ^
C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:81:49: error: macro "F" passed 2 arguments, but takes just 1
     dataFile.print(F(bmp280.readTemperature(), 2));
                                                 ^
C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:83:54: error: macro "F" passed 2 arguments, but takes just 1
     dataFile.print(F(bmp280.readPressure() / 100.0, 2));
                                                      ^
C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:85:53: error: macro "F" passed 2 arguments, but takes just 1
     dataFile.print(F(bmp280.readAltitude(1013.25), 2));
                                                     ^
C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:97:41: error: macro "F" passed 2 arguments, but takes just 1
     Serial.print(F(gps.location.lat(), 6));
                                         ^
C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:99:41: error: macro "F" passed 2 arguments, but takes just 1
     Serial.print(F(gps.location.lng(), 6));
                                         ^
C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:101:47: error: macro "F" passed 2 arguments, but takes just 1
     Serial.print(F(bmp280.readTemperature(), 2));
                                               ^
C:\Users\Samuel\AppData\Local\Temp\.arduinoIDE-unsaved2024215-6628-l5yvsk.gs327\sketch_mar15a\sketch_mar15a.ino:103:52: error: macro "F" passed 2 arguments, but takes just 1
     Serial.print(F(bmp280.readPressure() / 100.0, 2));
                                                    ^
exit status 1

Compilation error: macro "F" passed 2 arguments, but takes just 1

But after removing decimals from code it works fine now and it prints everything: Longittude, Lattitude, Alttitude, Pressure, X,Y,Z temperature. Can u help with this code and how could I add decimals different way since I cant use them with this method. Code:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h> // Include library for ADXL345 sensor

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
sensors_event_t event; // Declare event variable at a higher scope

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
File dataFile;
unsigned long lastSaveTime = 0;

TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);

Adafruit_BMP280 bmp280;

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

  if (!SD.begin(10))
  {
    Serial.println(F("SD Card initialization failed!"));
    return;
  }

  if (!bmp280.begin(0x76))
  {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }

  if (!accel.begin())
  {
    Serial.println(F("Could not find a valid ADXL345 sensor, check wiring!"));
    while (1);
  }
}

void loop()
{
  accel.getEvent(&event); // Read accelerometer data in the loop

  while (ss.available() > 0)
  {
    if (gps.encode(ss.read()))
      saveLocation();
  }

  if (millis() - lastSaveTime >= 5000)
  {
    if (saveLocation()) {
      Serial.println(F("Data has been written"));
    }
    lastSaveTime = millis();
  }

  delay(100);
}

bool saveLocation()
{
  dataFile = SD.open(F("gps_data.txt"), FILE_WRITE);

  if (dataFile)
  {
    if (dataFile.size() == 0) {
      dataFile.println(F("Latitude\tLongitude\tTemperature (C)\tPressure (hPa)\tAltitude (m)\tX\tY\tZ")); // Add X, Y, Z headers
    }

    dataFile.print(gps.location.lat());
    dataFile.print('\t');
    dataFile.print(gps.location.lng());
    dataFile.print('\t');
    dataFile.print(bmp280.readTemperature());
    dataFile.print('\t');
    dataFile.print(bmp280.readPressure() / 100.0);
    dataFile.print('\t');
    dataFile.print(bmp280.readAltitude(1013.25));

    // Write X, Y, Z measurements to file
    dataFile.print(event.acceleration.x);
    dataFile.print('\t');
    dataFile.print(event.acceleration.y);
    dataFile.print('\t');
    dataFile.println(event.acceleration.z);

    dataFile.close();

    Serial.print(gps.location.lat());
    Serial.print('\t');
    Serial.print(gps.location.lng());
    Serial.print('\t');
    Serial.print(bmp280.readTemperature());
    Serial.print('\t');
    Serial.print(bmp280.readPressure() / 100.0);
    Serial.print('\t');
    Serial.print(bmp280.readAltitude(1013.25));
    Serial.print('\t');
    Serial.print(event.acceleration.x);
    Serial.print('\t');
    Serial.print(event.acceleration.y);
    Serial.print('\t');
    Serial.println(event.acceleration.z);

    return true;
  }
  else
  {
    Serial.println(F("Error opening data file!"));
    return false;
  }
}

The F() macro is only for text. keep the printing of the variable as it was

dataFile.print(gps.location.lat(), 6);

use F() only when you had text in between double quotes like print("Hello") becomes print(F("Hello") but print(variable) stays as print(variable)

Thank you very much I tried to find solution for 2 days u helped me in less then 1hr can I maybe reccomend u or reward u on this app with something ?

I see you kept

    Serial.print('\t');
    dataFile.print('\t');

no need to use print for just one char - use write ➜ change them into

    Serial.write('\t');
    dataFile.write('\t');

will do thanks.

it's also a good practice to have a "clean" end of a function that returns something to make it easy to read

for example, drop the else part in this. If all worked you enter in the if (datafile) and there is a return there.

bool saveLocation() {
  dataFile = SD.open(F("gps_data.txt"), FILE_WRITE);
  if (dataFile)  {
    if (dataFile.size() == 0) {
      dataFile.println(F("Latitude\tLongitude\tTemperature (C)\tPressure (hPa)\tAltitude (m)\tX\tY\tZ")); // Add X, Y, Z headers
    }

    dataFile.print(gps.location.lat(), 6);
    dataFile.write('\t');
    dataFile.print(gps.location.lng(), 6);
    dataFile.write('\t');
    dataFile.print(bmp280.readTemperature());
    dataFile.write('\t');
    dataFile.print(bmp280.readPressure() / 100.0);
    dataFile.write('\t');
    dataFile.print(bmp280.readAltitude(1013.25));

    // Write X, Y, Z measurements to file
    dataFile.print(event.acceleration.x);
    dataFile.write('\t');
    dataFile.print(event.acceleration.y);
    dataFile.write('\t');
    dataFile.println(event.acceleration.z);

    dataFile.close();

    Serial.print(gps.location.lat(), 6);
    Serial.write('\t');
    Serial.print(gps.location.lng(), 6);
    Serial.write('\t');
    Serial.print(bmp280.readTemperature());
    Serial.write('\t');
    Serial.print(bmp280.readPressure() / 100.0);
    Serial.write('\t');
    Serial.print(bmp280.readAltitude(1013.25));
    Serial.write('\t');
    Serial.print(event.acceleration.x);
    Serial.write('\t');
    Serial.print(event.acceleration.y);
    Serial.write('\t');
    Serial.println(event.acceleration.z);

    return true;
  }
  // <<< no else, just clean exit from the function without wondering what's going on
  Serial.println(F("Error opening data file!"));
  return false;
}

I am kinda new to arduino and arduino ide so I still dont know all the tricks but I am trying my best and u helped a lot thank u again now I might pass my class with this. Thank u !!

have fun - don't ignore compiler's or IDE's warnings

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.