Guidance with I2C Sensor


hi everyone,
i working on getting pressure measurements i2c pressure sensor , using arduino yun interface sensors. objective of gather high frequency (~500 hz) data relatively short periods of time (5-10 minutes of continuous acquisition). have used arduino uno before never used capture data or control frequency of response/acquisition. i'm in initial phase of project , i've written following code sense of frequency can gather. following code gets data sensor , writes sd card on yun. data being recorded pressure reading (temperature read not recorded), , time in milliseconds.

code: [select]
//arduino datalogger example modified use i2c pressure sensor
#include "wire.h"                  //needed talk i2c devices
#include <fileio.h>
#include <arduino.h>
#include <bridge.h>
#include <console.h>

#define hscdann004ng2a3_i2c 0x28 // each i2c object has unique bus address
unsigned long timeinmilli=0;

void setup()
{
  bridge.begin();
  filesystem.begin();
  wire.begin(); // wake i2c bus
 
  delay(5);   //this delay give pressure sensor time start up
}

void loop()
{
 // getting data sensor
 wire.begintransmission(hscdann004ng2a3_i2c); // begin transmission
 wire.write(1);  // send bit asking register one, data register (as specified pdf)
 wire.endtransmission(); // "thanks, goodbye..."

  //reading data
 wire.requestfrom(hscdann004ng2a3_i2c, 4);
 while(wire.available() == 0);
 byte a     = wire.read(); // first received byte stored here ....example bytes one: 00011001 10000000
 byte b     = wire.read(); // second received byte stored here ....example bytes two: 11100111 00000000
 byte c     = wire.read(); // third received byte stored here
 byte d     = wire.read(); // fourth received byte stored here
 
 // confirming first 2 bits of first byte null
 byte status1 = (a & 0xc0) >> 6;  // first 2 bits first byte

 //getting pressure , temperature (if has) in binary code
 // , b bytes used pressure 12bits of accurancy
 int bridge_data = ((a & 0x3f) << 8) + b;

 //time in microseconds
 timeinmilli=millis();

 string datastring=string(bridge_data);
 datastring += ",";
 datastring += string(timeinmilli);
 
 
  // open file. note 1 file can open @ time,
  // have close 1 before opening another.
  // filesystem card mounted @ following "/mnt/filesystema1"
  file datafile = filesystem.open("/mnt/sda1/datalog.txt", file_append);

  // if file available, write it:
  if (status1 == 0)
  {
      if (datafile)
      {
        datafile.println(datastring);
        datafile.close();
        // print serial port too:
        //serial.println(datastring);
      }
      // if file isn't open, pop error:
      else
      {
        //console.println("error opening datalog.txt");
      }
  }

   delaymicroseconds(500); // minimum delay between measurements per sensor specs
}


this gives me following output (few lines shown):
1629,3450
1627,3523
1629,3581
1627,3636

based on initial test, looks frequency varies considerably (sd card latency?) anywhere 4-24 hz. i've looked @ posts people logging high speed data (analog) using sd card library don't believe can use library arduino yun. can provide suggestions on how should proceed bump frequency or better make switch different arduino/microcontroller?

thanks!

the arduino yun arduino leonardo plus wifi/linux/openwrt module (via bridge using pin 0 (rx) , pin 1 (tx)).

you can add sd logger shield, long pin 0 , pin 1 not used.
if sd logger shield compatible arduino uno , leonardo, test sketch on uno , after try make work yun.

the normal arduino sd library not fastest. based upon older sdfat library, sdfat library has been optimized lot since.
https://github.com/greiman/sdfat

please don't use this:
code: [select]
while(wire.available() == 0);the wire.requestfrom() returns after i2c transmission has ended. there no need wait something.

you can't use string object, when want fast code.

are opening file every sample ? slow.


Arduino Forum > Using Arduino > Sensors > Guidance with I2C Sensor


arduino

Comments