Interfacing ADXL 345 with Arduino Uno with SIM 900 GPRS Arduino Shield & a Micro SD Card Module

Hello,

I am facing some problems achieving a project's objective.

I am developing a low-cost accelerometer to measure seismic activity in an area.I have used Arduino Uno, MEMS ADXL 345, Micro SD card Module with a 16 GB SD card, SIM 900 GPRS Arduino Shield with a sim.

I have a thingspeak accout, with an API key and 3 channels to take data from the ADXL 345. My goal is that, the setup will take data in each 0.005 seconds and store that data into the sd card. Every hour, the data in sd card will be uploaded to the Thingspeak IoT. The SD card will be cleared after 30 days ensuring all the data is on the Thingspeak platform.

There will be a trigger mechanism. If resultant acceleration in the XY plane exceeds a certain value and if the acceleration in Z axis exceeds a certain value, the accelerometer will send a message to, say 5 phone numbers, that "The acceleration value has exceeded, Take Precaution". This process has to be instantaneous as the warning will save lives and sooner the message is delivered, the better.

I have already wired the whole connections.

My connections are

ADXL345 GND -> Arduino GND
ADXL345 VCC -> Arduino 5V
ADXL345 SDA -> Arduino A4 (SDA)
ADXL345 SCL -> Arduino A5 (SCL)

MicroSD VCC -> Arduino 5V
MicroSD GND -> Arduino GND
MicroSD MISO -> Arduino 12 (MISO)
MicroSD MOSI -> Arduino 11 (MOSI)
MicroSD SCK -> Arduino 13 (SCK)
MicroSD CS -> Arduino 4

  • SIM900 RX to Arduino digital pin 8
  • SIM900 TX to Arduino digital pin 7
  • SIM900 GND to Arduino GND
  • SIM900 VCC to external power supply

I am using the code attached below. But I am not getting any results. Is the data sampling rate 200 samples/ second achievable? And what was wrong with my code?

..............

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>

// Constants for accelerometer
#define ADXL345_ADDRESS (0x53)
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

// Constants for SD card
const int chipSelect = 4;

// Constants for SIM900
SoftwareSerial sim900(7, 8); // RX, TX

// Constants for Thingspeak
const char* thingspeakServer = "api.thingspeak.com";
const char* apiKey = "YOUR_API_KEY";
const int channelID = 12345;

// Variables for data and timing
unsigned long lastUploadTime = 0;
unsigned long uploadInterval = 3600000; // 1 hour
unsigned long currentTime;
float accelerationX, accelerationY, accelerationZ;
bool triggerSent = false;
float triggerThresholdXY = 10.0; // Set your desired threshold
float triggerThresholdZ = 5.0; // Set your desired threshold

void setup() {
Serial.begin(9600);
sim900.begin(9600);

// Initialize accelerometer
if (!accel.begin()) {
Serial.println("Could not find a valid ADXL345 sensor, check wiring!");
while (1);
}

// Initialize SD card
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
while (1);
}

// Connect to GPRS network
sim900.print("AT+CGATT?\r");
delay(1000);
sim900.println("AT+SAPBR=3,1,"APN","your_apn"");
delay(2000);
sim900.println("AT+SAPBR=1,1");
delay(2000);
sim900.println("AT+HTTPINIT");
delay(2000);
}

void loop() {
currentTime = millis();

// Read accelerometer data
sensors_event_t event;
accel.getEvent(&event);
accelerationX = event.acceleration.x;
accelerationY = event.acceleration.y;
accelerationZ = event.acceleration.z;

// Store data on SD card
String dataString = String(currentTime) + "," + String(accelerationX) + "," + String(accelerationY) + "," + String(accelerationZ);
File dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}

// Check for trigger conditions
if (abs(accelerationX) > triggerThresholdXY && abs(accelerationY) > triggerThresholdXY && abs(accelerationZ) > triggerThresholdZ && !triggerSent) {
sendTriggerSMS();
triggerSent = true;
}

// Upload data to Thingspeak every hour
if (currentTime - lastUploadTime >= uploadInterval) {
uploadDataToThingspeak();
lastUploadTime = currentTime;
}
}

void sendTriggerSMS() {
sim900.println("AT+CMGF=1");
delay(1000);
sim900.println("AT+CMGS="+1234567890""); // Replace with the recipient phone numbers
delay(1000);
sim900.println("The acceleration value has exceeded, Take Precaution");
delay(100);
sim900.println((char)26);
delay(1000);
sim900.println();
delay(1000);
}

void uploadDataToThingspeak() {
File dataFile = SD.open("data.txt");
if (dataFile) {
sim900.println("AT+HTTPPARA="CID",1");
delay(2000);
sim900.println("AT+HTTPPARA="URL","http://api.thingspeak.com/update\"");
delay(2000);
sim900.print("AT+HTTPDATA=");
sim900.println(dataFile.size() + 20);
delay(1000);
sim900.println("Content-Type: application/x-www-form-urlencoded");
delay(1000);
sim900.println();
delay(1000);
sim900.print("field1=");
while (dataFile.available()) {
sim900.write(dataFile.read());
}
dataFile.close();
delay(2000);
sim900.println();
delay(5000);
sim900.println("AT+HTTPACTION=1");
delay(10000);
sim900.println("AT+HTTPTERM");
delay(2000);
}
}
<CODE>
..............

Moved to Project Guidance..... Wasn't related to the development of the Arduino boards.

What does this mean? Is anything being logged to the SD card? Do you get the SMS messages?

Some suggestions/observations for you:
Open the SD card file once in setup and keep it open.
Don't use the String class - stick to null terminated char strings.

200 samples/sec is probably too fast - especially if you are writing the data as text.

It looks like you are writing text to the SD card in a Comma Separated Variable (CSV) format for easy import to Excel (or some other spreadsheet). If memory serves, there's a 512 byte buffer that fills up before it gets written to the SD card. The writing of 512 bytes takes time and can block your code. The SD card will ignore further commands until it has completed the writing of the buffer.

Thank you for the insights. Actually the Serial Monitor doesn’t show anything. I have checked the SD card and SIM 900 module separately and they worked fine.

I thing the sampling rate is a problem then. I will try other ways regarding the things you have pointed out.

Looking at the code you posted, the only time I could see any output to the serial monitor was during setup and only if the accelerometer or SD card initialisation failed. I couldn't see any additional output to the serial monitor inside the main loop or any called functions from the main loop.

What were you expecting to see on the serial monitor?

With this code, no messages came to the phone number I had provided and no data was uploaded to the Thingspeak. I really don't have any idea what was wrong, the wiring was okay too. As the separate tests gave results. I don't know if my code is suitable or not. Can you provide me with some suggestions regarding the code?

I would suggest really slowing down the sampling rate to maybe once per second. Then add in some print statements to see where your code is or isn't going. You might want to up the hardware serial baud rate to 115200 for the serial monitor.

Thank you, I will do that and see if it works or I can find the error.