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.
14 posts - 2 participants