Hello,
I am attempting to create a system using Nodemcu ESP8266, ADXL345, and a Micro SD card module. My goal is to read acceleration values (x, y, and z) from ADXL345 and write them to an SD card. Both the SD card and ADXL345 modules work flawlessly individually. However, when I combine these modules, I encounter the following error. How can I resolve this issue?
ERROR MESSAGE
�.......................................
Bağlandı. IP Adresi: 192.168.43.125
------------------------------------
Sensor: ADXL345
Driver Ver: 1
Unique ID: 12345
Max Value: -156.91 m/s^2
Min Value: 156.91 m/s^2
Resolution: 0.04 m/s^2
------------------------------------
Data Rate: 3200 Hz
Range: +/- 16 g
SD kart başarıyla başlatıldı.
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Error writing to file !
X: 1.96 Y: 2.04 Z: 2.12 m/s^2
Code Used and Circuit Images:
/*
Connections
SD Card
GND GND
VCC 5V
MISO D6
MOSI D7
SCK D5
CS D2
ADXL 345
SCL D1
SDA D2
VCC 3.3V
GND GND
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <ESP8266WiFi.h>
#include <SD.h> //Sd Kart Kütüphanesi
#include "SPI.h"
// Ağ bilgileri
#define WIFI_SSID "ROKKET"
#define WIFI_PASSWORD "faruk7070"
// ADXL345 örnek
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
// SD kart pini, uygun bir pin seçin
const int chipSelect = 4;
File sensorData;
char dataString[30];
void displaySensorDetails(void)
{
sensor_t sensor;
accel.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" m/s^2");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" m/s^2");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" m/s^2");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void displayDataRate(void)
{
Serial.print ("Data Rate: ");
switch(accel.getDataRate())
{
case ADXL345_DATARATE_3200_HZ:
Serial.print ("3200 ");
break;
case ADXL345_DATARATE_1600_HZ:
Serial.print ("1600 ");
break;
case ADXL345_DATARATE_800_HZ:
Serial.print ("800 ");
break;
case ADXL345_DATARATE_400_HZ:
Serial.print ("400 ");
break;
case ADXL345_DATARATE_200_HZ:
Serial.print ("200 ");
break;
case ADXL345_DATARATE_100_HZ:
Serial.print ("100 ");
break;
case ADXL345_DATARATE_50_HZ:
Serial.print ("50 ");
break;
case ADXL345_DATARATE_25_HZ:
Serial.print ("25 ");
break;
case ADXL345_DATARATE_12_5_HZ:
Serial.print ("12.5 ");
break;
case ADXL345_DATARATE_6_25HZ:
Serial.print ("6.25 ");
break;
case ADXL345_DATARATE_3_13_HZ:
Serial.print ("3.13 ");
break;
case ADXL345_DATARATE_1_56_HZ:
Serial.print ("1.56 ");
break;
case ADXL345_DATARATE_0_78_HZ:
Serial.print ("0.78 ");
break;
case ADXL345_DATARATE_0_39_HZ:
Serial.print ("0.39 ");
break;
case ADXL345_DATARATE_0_20_HZ:
Serial.print ("0.20 ");
break;
case ADXL345_DATARATE_0_10_HZ:
Serial.print ("0.10 ");
break;
default:
Serial.print ("???? ");
break;
}
Serial.println(" Hz");
}
void displayRange(void)
{
Serial.print ("Range: +/- ");
switch(accel.getRange())
{
case ADXL345_RANGE_16_G:
Serial.print ("16 ");
break;
case ADXL345_RANGE_8_G:
Serial.print ("8 ");
break;
case ADXL345_RANGE_4_G:
Serial.print ("4 ");
break;
case ADXL345_RANGE_2_G:
Serial.print ("2 ");
break;
default:
Serial.print ("?? ");
break;
}
Serial.println(" g");
}
void setup(void)
{
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(115200);
Serial.println("Accelerometer Test"); Serial.println("");
// WiFi bağlantısı
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Ağa bağlanıyor");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Bağlandı. IP Adresi: ");
Serial.println(WiFi.localIP());
Serial.println();
/* Initialise the sensor */
if(!accel.begin())
{
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
while(1);
}
/* Set the range to whatever is appropriate for your project */
accel.setRange(ADXL345_RANGE_16_G);
/* Set the data rate to 100 Hz */
accel.setDataRate(ADXL345_DATARATE_3200_HZ);
/* Display some basic information on this sensor */
displaySensorDetails();
/* Display additional settings (outside the scope of sensor_t) */
displayDataRate();
displayRange();
Serial.println("");
if (SD.begin(chipSelect)) {
Serial.println("SD kart başarıyla başlatıldı.");
} else {
Serial.println("SD kart başlatılamadı!");
while (1);
}
}
void loop() {
static unsigned long previousMillis = 0;
static unsigned long intervalRun1 = 2000; // 2 saniye çalışma aralığı
static unsigned long intervalWait1 = 60000; // 1 dakika bekleme süresi
unsigned long currentMillis = millis();
if (currentMillis - previousMillis < intervalRun1) {
// İlk durum: 0 saniye - 2 saniye aralığı
sensors_event_t event;
accel.getEvent(&event);
float xValue = event.acceleration.x;
float yValue = event.acceleration.y;
float zValue = event.acceleration.z;
sprintf(dataString, "X:%.2f, Y:%.2f, Z:%.2f", xValue, yValue, zValue);
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(" "); Serial.println("m/s^2 ");
// SD karta veriyi yaz
sensorData = SD.open("data.csv", FILE_WRITE);
if (sensorData) {
sensorData.println(dataString);
sensorData.close();
Serial.println("Dosyaya yazdırıldı");
} else {
Serial.println("Error writing to file !");
}
} else if (currentMillis - previousMillis < intervalRun1 + intervalWait1) {
// İkinci durum: 2 saniye - (2 saniye + 1 dakika) aralığı
// Program beklemede
} else {
// Üçüncü durum: 2 saniye + 1 dakika sonrası
previousMillis = currentMillis; // Bir sonraki çalışma süresini ayarla
}
}
Thank you in advance for your assistance.