WRITE ADXL345 DATA TO SD CARD MODULE

Hi

I want to write my adxl xyz data to sd card module. i have made the code but herei want to add date and time. in the sd card file along with xyz i want date and time to write.

Can someone please help me.

#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library
 
 
#include "Wire.h"    // imports the wire library for talking over I2C 
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out;  // Outputs
 

int chipSelect = 4; //chipSelect pin for the SD card Reader
File AngleData; //Data object you will write your sesnor data to
 
void setup(){
Serial.begin(9600); //turn on serial monitor
//mySensor.begin();   //initialize pressure sensor mySensor
Wire.begin(); // Initiate the Wire library
  // Set ADXL345 in measuring mode
  Wire.beginTransmission(ADXL345); // Start communicating with the device 
  Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
  // Enable measurement
  Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable 
  Wire.endTransmission();
  delay(10); 
pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
SD.begin(4); //Initialize the SD card reader
}
 
void loop() {
  Wire.beginTransmission(ADXL345);
  Wire.write(0x32); 
  Wire.endTransmission(false);
  Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
  X_out = X_out; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
  Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
  Y_out = Y_out;
  Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
  Z_out = Z_out/256;
  
  
AngleData = SD.open("PTData.txt", FILE_WRITE);
if (AngleData) {
Serial.print("X= ");
  Serial.print(X_out);
  Serial.print("   Y= ");
  Serial.print(Y_out);
  Serial.print("   Z= ");
  Serial.println(Z_out);
delay(250); //Pause between readings.
 
 
AngleData.print(X_out);                           
AngleData.print(",");                             
AngleData.println(Y_out);                        
AngleData.print(",");                               
AngleData.println(Z_out); 
AngleData.close();                                  //close the file
}
}

You need a Real Time Clock (RTC) module. These are often based on ds1307 or ds3231 chips and have a coin cell for battery backup.

Your readings are every 250ms? Do you need time to be recorded to that precision, or is it ok if 4 readings have the same time with a precision of 1s?

PaulRB:
You need a Real Time Clock (RTC) module. These are often based on ds1307 or ds3231 chips and have a coin cell for battery backup.

Your readings are every 250ms? Do you need time to be recorded to that precision, or is it ok if 4 readings have the same time with a precision of 1s?

i want to read data every 4 seconds.

With the code which i have posted it is not saving to sd card

bmg1234:
With the code which i have posted it is not saving to sd card

Why are you asking for help writing the date and time to the card, when your code does not write anything to the card?

I want to do it together.

Can you please check the code. i took temperature code and made modification.

It is not writing anything to sd card

Code is working it is writing data to sd card.

Now i want to add date and time.

is there any possibilities to do without RTC?

You can have the user set a time and date and update that when the Arduino clock drifts. Use a time library to find out what day it is based on the last set date plus milliseconds since. It'd be a pain to code and run, get or build an Arduino with a crystal instead of resonator just to not have to reset often. Even with RTC you have to set the date/time at least once and need the library(s).

How it can be done? is there any example?

bmg1234:
It is not writing anything to sd card

bmg1234:
Code is working it is writing data to sd card.

Is there an intermittent fault? Did you change or fix something?

PaulRB:
Is there an intermittent fault? Did you change or fix something?

MISO and MOSI pins were connected wrong and i changed delay (250 ) to delay (4000).

Now i want to add time. can you please help me

bmg1234:
How it can be done? is there any example?

It is difficult for beginners and not convenient to use. Get an RTC.

PaulRB:
It is difficult for beginners and not convenient to use. Get an RTC.

How can rtc be used with ADXL345.

if you have any example can you please send me

bmg1234:
How can rtc be used with ADXL345.

Do you mean because they both need to connect to A4 & A5?

A4 & A5 are the i2c bus and many devices can be connected at the same time. Each device has a different address on the bus. The address of your ADXL345 is 0x53. The RTC will have a different address.

Hi

I bought DS3231 RTC.

Can you please explain me how the connections ti be made. ADXL sensor SDA and SCL Pins are connected to A4 and A5 of arduino. how to i connect RTC device now.

What changes i should make with the code

The RTC will also have SDA and SCL pins. Connect them to A4 & A5 also. Then follow this tutorial to test your RTC is working and set the time.

I Have made the code both date time and adxl value is writing to sd card but date and time is wrong.

How to set present time and date

#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
File sdcard_file;
DS3231  rtc(SDA, SCL);


float output;
#include "Wire.h"    // imports the wire library for talking over I2C 
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out;  // Outputs
 

int chipSelect = 4; //chipSelect pin for the SD card Reader
File AngleData; //Data object you will write your sesnor data to

void setup() {
  Serial.begin(9600);
  
  pinMode(4, OUTPUT);
  //mySensor.begin();   //initialize pressure sensor mySensor
Wire.begin(); // Initiate the Wire library
  // Set ADXL345 in measuring mode
  Wire.beginTransmission(ADXL345); // Start communicating with the device 
  Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
  // Enable measurement
  Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable 
  Wire.endTransmission();
  delay(10); 
  rtc.begin(); 
  //rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  rtc.adjust(DateTime(2019, 7, 15, 20, 3, 0));
  pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
SD.begin(4); //Initialize the SD card reader
  // SD Card Initialization
  if (SD.begin(4))
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  
  
  
}

void loop() {

  Wire.beginTransmission(ADXL345);
  Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
  X_out = X_out; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
  Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
  Y_out = Y_out;
  Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
  Z_out = Z_out/256;
  
  //Serial.print("The angle is: ");
AngleData = SD.open("PTData.txt", FILE_WRITE);
if (AngleData) {
  
  Serial.print(rtc.getDateStr());
  Serial.print("     ");
  Serial.print(rtc.getTimeStr());
  Serial.print("The angle is: ");
Serial.print("X= ");
  Serial.print(X_out);
  Serial.print("   Y= ");
  Serial.print(Y_out);
  Serial.print("   Z= ");
  Serial.println(Z_out);
delay(4000); //Pause between readings.
 
AngleData.print(rtc.getDateStr());
   AngleData.print("     ");   
    AngleData.print(rtc.getTimeStr());
AngleData.print("      "); 
AngleData.print("X= ");
AngleData.print(X_out);                             //write temperature data to card
AngleData.print("      ");  
AngleData.print("Y= ");//write a commma
AngleData.print(Y_out);                        //write pressure and end the line (println)
AngleData.print("      "); 
AngleData.print("Z= ");//write a commma
AngleData.println(Z_out); 
AngleData.close();            
  
} 
 
}

20:15:30.605 -> 1.2000 00:24:38The angle is: X= 60.00 Y= 179.00 Z= 0.57
20:15:30.605 -> 00:24:34The angle is: X= 62.00 Y= 182.00 Z= 0.56
20:15:30.605 -> 01.01.2000 00:24:38The angle is: X= 60.00 Y= 179.00 Z= 0.57
20:15:30.605 -> SD card is ready to use.
20:15:30.641 -> 01.01.2000 00:24:43The angle is: X= 60.00 Y= 181.00 Z= 0.56
20:15:34.655 -> 01.01.2000 00:24:47The angle is: X= 59.00 Y= 181.00 Z= 0.57
20:15:38.687 -> 01.01.2000 00:24:51The angle is: X= 56.00 Y= 180.00 Z= 0.57

Did you follow the tutorial I posted a link to? That tutorial included setting the date/time.

This line:

rtc.adjust(DateTime(2019, 7, 15, 20, 3, 0));

should have set the correct time. Does your RTC have a coin cell fitted? Is there a piece of plastic that must be removed for the battery to connect?