I'm trying to write a sketch that will pull data from a BME280 and ADXL345 using the I2C protocol and then write that data to an SD card. Using different sketches I've found online I got something that I think should work, but isn't and I'm not sure what the issue is. During setup, I ask it to create a file HAB Flight Data.txt, write a header then close for later use. I can't even get that to work. Another problem I'm having is my BME sensor not working. I've got both sensors connected on a FeatherWing Proto as follows

Feather      ADXL     BME       
SCL ----->  SCL-----> SCK
SDA------>  SDA-----> SDI
3V------->  VIN-----> VIN
GND------>  GND-----> GND
3V------->  CS

Hardware specs: 1x Feather M0 Adalogger 1x ADXL345 1x BME280 1x FeatherWing Proto

Any insight as to where I went wrong would be much appreciated!

#include <SdFat.h>                 // SD card library
#include <Wire.h>               // I2C wire lbrary
#include <Adafruit_BME280.h>    // Pressure Sensor library
#include <Adafruit_Sensor.h>    // Adafruit Sensor library
#include <Adafruit_ADXL345_U.h> // ADXL345 library

#define SEALEVELPRESSURE_HPA (1013.25)
#define RANGE_2_G 0;

int chipSelect = 4;
float hgInches = 29.97;

SdFat SD;
Adafruit_BME280 bme;
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {
  //turn on serial monitor
  while (!Serial) {
  }// end while - serial

  // initialize I2C bus

  bool status;
  //initialize BME sensor
  status =  bme.begin();
  if (!status) {
    Serial.println("No BME");
    while (1);
  } // end if - BME

  //initialize ADXL sensor, range and data rate
  status = accel.begin();
  accel.setRange( ADXL345_RANGE_2_G );
  accel.setDataRate( ADXL345_DATARATE_50_HZ );
  if (!status) {
    Serial.println("No ADXL");
    while (1);
  } // end if - accel

  // initialize the SD card
  Serial.print("Looking for SD: ");
  if (!SD.begin(chipSelect)) {
    Serial.println("SD not found ");
  } // end if - SD
  Serial.println(" SD found");
  // print a header in HAB Flight Data.txt
  File flightData = SD.open("HAB Flight Data.txt", FILE_WRITE);
  if (flightData) {
    flightData.print("HAB FLight Data");
    flightData.println("Temperature, Pressure, Altitude, Humidity, X-Axis, Y-Axis, Z-Axis");
  } //end if - flightdata
} // end main setup

void loop() {
  //var to hold sensor data
  float BMEt1 = (bme.readTemperature());
  float BMEh1 = (bme.readHumidity());
  float BMEp1 = (bme.readPressure() / 100.0F);
  float BMEa1 = (bme.readAltitude(SEALEVELPRESSURE_HPA));
  // register events for ADXL
  sensors_event_t event;

  // print out BME data to serial monitor
  Serial.println("BME Data.");
  Serial.print("Temperature = "); Serial.print(BMEt1); Serial.println(" *F");
  Serial.print("Pressure = "); Serial.print(BMEp1); Serial.println(" hPa");
  Serial.print("Approx. Altitude = "); Serial.print(BMEa1); Serial.println(" meters");
  Serial.print("Humidity = "); Serial.print(BMEh1); Serial.println(" %");

  // print out ADXL data to serial monitor
  Serial.println("Accelerometer Data");
  Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print("  ");

  delay(1000); //Pause between readings.

  // Create a txt file adn record data
  File flightData = SD.open("HAB Flight Data.txt", FILE_WRITE);
  if (flightData) {
    flightData.print(BMEt1); flightData.print(","); flightData.print(BMEp1); flightData.print(",");
    flightData.print(BMEa1); flightData.print(","); flightData.print(BMEh1); flightData.print(",");
    flightData.println("X: "); flightData.print(event.acceleration.x); flightData.print(",");
    flightData.println("Y: "); flightData.print(event.acceleration.y); flightData.print(",");
    flightData.println("Z: "); flightData.print(event.acceleration.z); flightData.print(",");
  } //  end if - data recorder
  else {
    Serial.println("error opening HAB Flight Data.txt");
  } // end else - SD error

} //end Main loop
  • 2
    reduce your code to only create a file on the SD card and write some fixed data to it (like "abc123") ..... do not include anything else in your code .... run a test and update your question with the result .... also update your program listing so that it shows the minimized code
    – jsotola
    Nov 29 '18 at 22:59
  • 1
    I took the sensors and code apart and tested individually before slowly incorporating things back together. Thank you for the advice, it was what I needed! Nov 30 '18 at 21:10

[SOLVED] - Based on the advice of a few different people I slowly took the code apart and tested each part individually before plugging sections back in. I also tested the code on new sensors individually after seeing it working, I made a new wiring harness and retested. Worked perfectly!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.