Show Posts
Pages: [1]
1  Using Arduino / Storage / Re: SD Card and ADXL345 on: April 08, 2013, 11:37:48 pm
Oh I forgot to mention that I am using Sparkfun IMU 6 DOF (https://www.sparkfun.com/products/10121) and the connection between Arduino and ADXL345:
Arduino             ADXL345
3.3 V                 VCC
Analog 4             SDA
Analog 5             SCL
GND                  GND

I've also tried to read the Accelerometer data without using the SD card shield and it worked fine. It gave the X,Y,Z data on serial monitor. Here is the code I used to test it:
Code:
#include <Wire.h>
#include <ADXL345.h>

ADXL345 adxl;

const int rangeSetting = 2; //ADXL345 data range setting +- 2 G
const double rateSetting = 100; //ADXL345 rate setting 100 Hz

int x,y,z; // variables to hold ADXL345 data

const int button = 2;
int buttonVal = HIGH;
int lastButtonVal;

void setup() {
  Serial.begin(57600);
  adxl.powerOn();
  
  adxl.setRangeSetting(rangeSetting);
  adxl.setRate(rateSetting);
  
  pinMode(button, INPUT_PULLUP);
  delay(1);
  
  Serial.println("Push the button to start");
  while(buttonVal == HIGH) {
    buttonVal = digitalRead(button);
    //Serial.println(buttonVal);
    delay(1);
  }
  Serial.println("Start!");
  delay(10);    
}

void loop() {
  adxl.readAccel(&x, &y, &z);
  Serial.print(x);
  Serial.print(",");
  Serial.print(y);
  Serial.print(",");
  Serial.println(z);
  
  cekButton();
  
  delay(10);
}

void cekButton() {
  buttonVal = digitalRead(button);
  //Serial.print("buttonVal: ");
  //Serial.println(buttonVal);


  if(buttonVal != lastButtonVal) {
    if(buttonVal == LOW) {
      Serial.println("Stop");
      //myFile.close();
      while(1) {
      }
    }
  }

  lastButtonVal = buttonVal;
}

However, when I connected the ADXL345 through that SD card shield using the same code above, there wasn't any data at the serial monitor.
2  Using Arduino / Storage / Problem saving ADXL345 data to SD Card on: April 07, 2013, 11:43:11 pm
Hi all,
I want to save data from ADXL345 to SD Card using this module http://www.seeedstudio.com/wiki/SD_Card_Shield_V3.0 and the latest SdFat libraries from http://code.google.com/p/sdfatlib/. A push button (connected to digital pin 2 and ground) is used to start and stop writing to SD Card. However, the program didn't run as I expected because even the "Push the button to start" command didn't show up on the serial monitor.

Here is the code I used:
Code:
#include <SdFat.h>
#include <Wire.h>
#include <ADXL345.h>

SdFat sd;
SdFile myFile;

ADXL345 adxl;

// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 10; //CS pin for the SD Card module

const int rangeSetting = 2; // range setting for accelerometer = +- 2 G
const double rateSetting = 100; // rate setting for accelerometer = 100 Hz
int x,y,z; // variables hold accelerometer datas

const int button = 2; // button is connectod digital pin 2 and ground
int buttonVal = HIGH;
int lastButtonVal;
int i = 1;

void setup() {
  Serial.begin(57600);
  while(!Serial) {
  }

  adxl.powerOn();
  adxl.setRangeSetting(rangeSetting);
  adxl.setRate(rateSetting);

  pinMode(button, INPUT_PULLUP);
  delay(1);

  //Serial.println("Type any chracter to start");
  //while(Serial.read() <= 0) {}
  //delay(1);

  Serial.println("Push the button to start");
  while(buttonVal == HIGH) {
    buttonVal = digitalRead(button);
    //Serial.println(".");
    //delay(1);
  }

  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!myFile.open("Tulis1.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }

  // if the file opened okay, write to it:
  Serial.println("Start");

  //tanda buat awal file
  myFile.println("8/4/2013");
}

void loop() {
  /*
  for(int x = 0; x < 15; x++) {
   myFile.print(x);
   myFile.print("\t");
   myFile.print("tes");
   myFile.println(x);
   //delay(10);
   }
   */

  adxl.readAccel(&x, &y, &z);  
  
  myFile.print(i);
  myFile.print("\t");
  myFile.println(x);
  i++;

  cekButton();
}


void cekButton() {
  buttonVal = digitalRead(button);
  //Serial.print("buttonVal: ");
  //Serial.println(buttonVal);


  if(buttonVal != lastButtonVal) {
    if(buttonVal == LOW) {
      Serial.println("Stop");
      myFile.close();
      while(1) {
      }
    }
  }

  lastButtonVal = buttonVal;
}

I tried to use the SD card module and push button without the ADXL345 and it worked to save all the data until I push the button. I used this code:
Code:
#include <SdFat.h>
SdFat sd;
SdFile myFile;

// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 10;

const int button = 2;
int buttonVal = HIGH;
int lastButtonVal;
int i = 1;

void setup() {
  Serial.begin(57600);
  while(!Serial) {
  }

  pinMode(button, INPUT_PULLUP);
  delay(1);

  //Serial.println("Type any chracter to start");
  //while(Serial.read() <= 0) {}
  //delay(1);

  Serial.println("Tekan tombol untuk mulai");
  while(buttonVal == HIGH) {
    buttonVal = digitalRead(button);
    //Serial.println(".");
    //delay(1);
  }

  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!myFile.open("Tulis1.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }

  // if the file opened okay, write to it:
  Serial.println("Mulai");

  //tanda buat awal file
  myFile.println("31/3/2013");
}

void loop() {
  /*
  for(int x = 0; x < 15; x++) {
   myFile.print(x);
   myFile.print("\t");
   myFile.print("tes");
   myFile.println(x);
   //delay(10);
   }
   */

  myFile.print(i);
  myFile.print("\t");
  myFile.print("tes");
  myFile.println(i);
  i++;

  cekButton();
}


void cekButton() {
  buttonVal = digitalRead(button);
  //Serial.print("buttonVal: ");
  //Serial.println(buttonVal);


  if(buttonVal != lastButtonVal) {
    if(buttonVal == LOW) {
      Serial.println("Stop");
      myFile.close();
      while(1) {
      }
    }
  }

  lastButtonVal = buttonVal;
}


So, why the SD card couldn't save any data when use with the ADXL345? Is there any problem with my code?

Thanks

Fino
3  Using Arduino / Sensors / Re: ADXL345 data rate & method to read it on: February 26, 2013, 04:03:30 am
Okay, I got it. Thanks a lot for the answers  smiley
4  Using Arduino / Sensors / Re: ADXL345 data rate & method to read it on: February 25, 2013, 11:45:46 pm
Hi michinyon, thanks for the suggestions
Your sample rate is limited by two things:

the rate at which the device samples it's own hardware
the rate at which the I2C operation to the arduino works.

Ohh I see, the ADXL345 data rate is the rate at which the I2C operation to the arduino works while its bandwidth is the rate at which the device samples it's own hardware. Is that right?
So if I set the ADXL345 data rate at 100 Hz, I have to set the Arduino to sample at 200 Hz?

@ robtillaart, I have test the code and measure the execution time. I found that when I set the serial rate at 9600, actually, the longest execution time was not when it did the read function but at the serial print. I think this is my problem and speed up the serial rate solved it.
5  Using Arduino / Sensors / Re: ADXL345 data rate & method to read it on: February 24, 2013, 10:24:20 pm
Hi rob, thanks for the reply. I want to make a wireless pedometer using this digital Accelerometer and Arduino UNO.
Yes, I have already measured the time needed to read the data and compared the time when it was using the library and when it wasnt. It required around 4000 us to read the data using the library. Codes to get that value:

Code:
#include <Wire.h>
#include <ADXL345.h>

ADXL345 adxl;

const int rangeSetting = 2; // +- 2 G
const double rateSetting = 100.00; // 100 Hz

int x,y,z;
unsigned long int time;

void setup() {
  Serial.begin(9600);
  adxl.powerOn();
 
  adxl.setRangeSetting(rangeSetting);
  adxl.setRate(rateSetting);
   
}

void loop() {
  adxl.readAccel(&x, &y, &z);
  time = micros();
 
  Serial.println(time);
 
  delay(20);
}


Also, I measured the time but didnt use the library and got around 2500 us. It used this codes:
Code:
#include <Wire.h>

// Definisi2 ADXL345
#define ACC (0x53)  //Alamat register ADXL345
#define REG_POWER_CTL 0x2D //register Power Control
#define ACC_TO_READ (6)// Axis Accelermoter yg akan dibaca

//unsigned int icr1;
int x,y,z;
int data_acc[3];
int regAddress = 0x32;
byte buff[ACC_TO_READ];
unsigned long time;

void setup() {
  Serial.begin(9600);
  delay(1);
 
  Wire.begin();
 
 
  //mengatur accelerometer agar dalam mode measurement dan menonaktifkan semua
  //fitur powersaving melalui register 0x2D
  Wire.beginTransmission(ACC); //start transmission to ACC
  Wire.write(REG_POWER_CTL);        // register address: 0x2D
  Wire.write(0x08);        // 0x08
  Wire.endTransmission(); //end transmission
 
 
*/
void loop() {
  //Serial.print("A");
     
  Wire.beginTransmission(ACC);
  Wire.write(regAddress);
  Wire.endTransmission(ACC);
 
  Wire.beginTransmission(ACC);
  Wire.requestFrom(ACC, ACC_TO_READ);
 
  int i = 0;
  while(Wire.available()) {
    buff[i] = Wire.read();
    i++;
  }
  Wire.endTransmission();
 
  //masking!!
  // each axis reading comes in 10 bit resolution, ie 2 bytes
  // thus we are converting both bytes in to int
  data_acc[0] = (((int)buff[1]) << 8) | buff[0];
  data_acc[1] = (((int)buff[3]) << 8) | buff[2];
  data_acc[2] = (((int)buff[5]) << 8) | buff[4];
 
  x = data_acc[0];
  y = data_acc[1];
  z = data_acc[2];
 
  time = micros();
  Serial.println(time);

 
  delay(20);
 
}

6  Using Arduino / Sensors / ADXL345 data rate & method to read it on: February 24, 2013, 10:44:43 am
Hi all, I am currently using ADXL345 in my project but i'm little confused about method to read it. I am using library from https://github.com/jenschr/Arduino-libraries/tree/master/ADXL345 and want to read it at 100 Hz.
Does the read function on that library guarantee that I will get data every 0.01 s (if I put the read function on main loop) or should I use timer based interrupt every 0.01 s and put the read function on the ISR?

Thanks  smiley
7  Using Arduino / Sensors / Re: Guide to gyro and accelerometer with Arduino including Kalman filtering on: May 09, 2011, 03:45:28 am
Hi Lauszus, I bought the same gyro(http://www.sparkfun.com/products/9165) used by IMU on your project. I just want to read angle from the gyro but having a little confuse about the HP, PD and ST pin. Should I just left them all unconnect or do you have any suggestion?

~Thanks  smiley
~Fino
Pages: [1]