Wire/I2c library conflicting with FileIO library??

I am working on a sensor network which will save data to external storage (SD card, flash drive) using the Arduino Yun. Using the datalogger example from the Bridge library, I have come up with the code posted below. I wanted to add the core of the datalogger example (opening the file, writing to it…) into a code I had which polled the sensors and collected the data into a CSV. However, when I tried this, I could not access the file-- the file would not open and I got the “error opening file” debug print on the serial monitor every time the loop executed.

Bringing the code together piece by piece, I found that simply adding the Wire.h library for the BMP180 (not even using it, as all pressure code is commented out) caused the file to not open correctly.

The code below writes my CSV string to the mounted SD/flash just fine, however, I need to add the wire library in order to get the pressure data which is needed for the CSV. I’m not an advanced coder and have no idea what could be causing this issue.

Any ideas on:

  1. Why the wire library would cause the file to not open?

  2. Any i2c library alternatives to Wire.h (preferably with examples for BMP180) or alternative code for the BMP180 which doesn’t use Wire.h?

  3. Any other ideas?? I have already found an analog pressure sensor (MXP4250) which I am considering replacing as the code is so close-- it just needs pressure data!! This is a last resort, however, as changing the sensor would result in changes to my PCB design which is being manufactured as I type.


#include <FileIO.h>
#include <SoftwareSerial.h>
#include <nmea.h>
#include <dht.h>
//#include "Wire.h"
//#include "I2Cdev.h"
//#include "BMP085.h"

NMEA gps(ALL);                   // create GPS object for NMEA
SoftwareSerial mySerial(10, 11); // create GPS object for Software Serial. TX = 10, RX = 11

String dataCSV = "";             //initialize RMC sentence to hold specific data we want

void setup() 

  mySerial.begin(9600);          //Use software serial to save UARTs

//  while(!Serial);
//  Serial.println("Date  Time  Latitude  Longitude  T  RH  PartMatter  SO2  H2S  CO  NO  O3  NO2");

void loop() 
  dataCSV = "";      //re-initialize our dataString for new data
// Get GPS data FIRST 
  if (mySerial.available() > 0 ) 
    // read incoming character from GPS and feed it to NMEA type object
    if (gps.decode(mySerial.read())) 
      dataCSV += gps.term(9);    //add date
      dataCSV += ",";            //separator
      dataCSV += gps.term(1);    //add time
      if (strlen(gps.term(1)) < 2)
      // Data not valid, try again
      dataCSV += ",";            //separator
      dataCSV += gps.term(3);    //add latitude            
      dataCSV += gps.term(4);    //add N, S
      dataCSV += ",";            //separator
      dataCSV += gps.term(5);    //add longitude
      dataCSV += gps.term(6);    //add E, W
      dataCSV += ",";            //separator

      // Create file to store the data
      File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
      // if the file is available, write to it:
      if (dataFile) 
        // print to the serial port too:
        // if the file isn't open, pop up an error:
          Serial.println("error opening datalog.txt");
    // Try to get GPS again
    else return;
  // Try to get GPS again
  else return;

// Got GPS, now get other data

void getTempHum()
  dht DHT;                  // create DHT sensor object
  // RHT03/DHT22 variables
  double temp = 0;     
  double relHum = 0; 

//  SFE_BMP180 pressure;      // BMP180 (pressure sensor module)

//  // BMP180 variables
//  char status;
//  double T, P, p0, a;       // BMP180 temperature, pressure, normalized pressure, altitude
  // POLL DIGITAL SENSORS FOR WEATHER DATA /////////////////////////////////////////////////////////////////////////////
  // Get DHT22 temperature, humidity data
      while(1)         // loop 1000x & break once there is a valid temperature
        int chk = DHT.read22(dhtData);       // request DHT data
        temp = DHT.temperature;              // read temperature
        relHum = DHT.humidity;               // read humidity
        if (temp > -999)                     // if the temperature is valid (invalid is -999)
          dataCSV += temp;
          dataCSV += ",";
          dataCSV += relHum;
          dataCSV += ",";
      dataCSV += ",";
      dataCSV += ",";    

void getAirQuality()
  // Alphasense A4s
  double so2ADC, h2sADC, coADC = 0.0;                     // ADC values        
  double noADC, o3ADC, no2ADC = 0.0;                      // ADC values
  double so2Voltage, h2sVoltage, coVoltage = 0.0;      // Voltage readings (mV)
  double noVoltage, o3Voltage, no2Voltage = 0.0;       // Voltage readings (mV)   
  double so2PPB, h2sPPB, coPPB = 0.0;                  // Concentration (PPB)
  double noPPB, o3PPB, no2PPB = 0.0;                   // Concentration (PPB)

  //Shinyei PPD60PV
  unsigned long pmADC = 0;       // PM ADC value                
  double pmVoltage = 0;              // PM voltage
  double pmConc = 0;             // PM concentration

  // POLL ANALOG SENSORS FOR AIR QUALITY DATA ///////////////////////////////////////////////////////////////////////////

      analog reads, conversion to concentrations

Hi, welcome to the forum.

I don't know what causes this, but the I2C with Wire.h should have nothing to do with the file opening of course. There is something else going on.

The Yun datalogger : http://arduino.cc/en/Tutorial/YunDatalogger

Many things happen in your sketch, maybe there is too much going on.

Which NMEA library do you use ? This one ? http://www.maartenlamers.com/nmea/

Which DHT library do you use ? This one ? http://playground.arduino.cc/Main/DHTLib That is not possible, the variable 'dhtData' is not declared. I don't know which pin you use for the DTH sensor. I hope it is not pin 0 or pin 1 (those are used by the Bridge). I prefer to declare the DHT object global, I'm not sure what happens when it is declared inside a function.

Which BMP085 library did you try ? It might the Adafruit version ?

What is "I2Cdev.h" doing in your sketch ? If you want to speed up the I2C to 400kHz, you can use: Wire.setClock(400000); The normal Wire library is okay. You don't have to look for something else.

Are you sure you want to do "while(!Serial);" somewhere in the sketch ? That is normally done in setup() to let the Arduino board wait until you open the serial monitor.

Can you tell how much the code size is and the dynamic memory use ? That is shown when the sketch is compiled. You might have sram memory (the dynamic memory) overflow. Could you make a few test sketches. For example a sketch that only writes BMP180 data to a file. When developing a project it is normal to have maybe 5 test sketches, to test every single part. When those are okay, they can be combined into the final project.