LIS3DH Accelerometer output written in a .txt in an SD card

Hello! I'm encountering the following problem.
I want to connect 3x LIS3DH accelerometers to an Arduino Uno, the output of which I want to write in a .txt file in an SD card. I have tried hooking up all 3 accelerometers via SPI without the SD card, and they work fine. However, when I add the SD card (SPI connection) and the relevant SD card code, the output of all the accelerometers is 0.00. There seems to be a conflict between the SD card and LIS3DH when connected via SPI.

To simplify the problem, I have now hooked up just 1x LIS3DH and the SD card module. I am a newb when it comes to coding, trying to wrap my head around SPI connections etc, but I have the following code:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include "SdFat.h"
SdFat SD;

// Used for software SPI
#define LIS3DH_CLK 13
#define LIS3DH_MISO 12
#define LIS3DH_MOSI 11
// Used for hardware & software SPI
#define LIS3DH_CS 10
#define SD_CS_PIN 4

// software SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK);
File myFile;


void setup(void) {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("LIS3DH test!");

  if (! lis.begin()) {   // change this to 0x19 for alternative i2c address
    Serial.println("Couldnt start");
    while (1) yield();
  }
  Serial.println("LIS3DH found!");

  lis.setRange(LIS3DH_RANGE_4_G);   // 2, 4, 8 or 16 G!

    Serial.print("Initializing SD card...");

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("acceldata.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to acceldata.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening acceldata.txt");
  }
}

void loop() {
  /* Get a new sensor event, normalized */
  sensors_event_t event;
  lis.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("\t\tX: "); Serial.print(event.acceleration.x);
  Serial.print(" \tY: "); Serial.print(event.acceleration.y);
  Serial.print(" \tZ: "); Serial.print(event.acceleration.z);
  Serial.println(" m/s^2 ");
  Serial.println();

  /* Write in the text file */
myFile = SD.open("AccelData.txt", FILE_WRITE);
myFile.print(event.acceleration.x);
myFile.print(" ");
myFile.print(event.acceleration.y);
myFile.print(" ");
myFile.println(event.acceleration.z);
myFile.close();


  delay(200);
}

The output is as follows:

08:07:27.284 -> LIS3DH test!
08:07:27.284 -> LIS3DH found!
08:07:27.331 -> Initializing SD card...initialization done.
08:07:27.489 -> Writing to AccelData.txt...done.
08:07:27.522 -> X: 0.00 Y: 0.00 Z: 0.00 m/s^2
08:07:27.562 ->
08:07:27.690 -> X: -0.01 Y: -0.01 Z: -0.01 m/s^2
08:07:27.733 -> (etc.)

Any ideas? I cannot get any more components like resistors etc, I'm hoping for a software solution, like Hardware & Software SPI.

Thanks in advance.

Welcome to the forum.

The SPI bus is not a well defined standard.
Your SD module probably does not release the MISO signal.

Can you give a link to your SD module (a link to where you bought it) ?
Do you have a schematic of your SD module ?
Sometimes the 3.3V MISO from the SD memory card is directly connected to the Arduino Uno. I think that is okay, the SD memory card releases the MISO when it is not connected.

It could be something else, but I don't see a problem in your sketch :face_with_peeking_eye:

Have you tried the normal "SD" library ?

Did you know that pin 10 must be output for an Arduino Uno to be sure that the SPI hardware turns into Master mode ?
I suppose that the "lis.begin()" does that for you, but you can add it.

pinMode(10,OUTPUT);      // needed for SPI hardware of Arduino Uno
digitalWrite(10,HIGH);   // make the CS signal not active

Thank you for the reply.

  • The SD card module I use is a very cheap one. I haven't managed to find a datasheet for it. Here's a link to what it looks like: SD Card Reader/Writer Module - ElectroPeak

  • The connection is as follows: 5V - 5V, GND - GND, MOSI - 11, MISO - 12, SCK - 13, CS - 4. I have tried different pins for CS while rebuilding the wiring over time, no difference - not that there should be any.

  • I've added the pinMode and digitalWrite lines right after Serial.begin(115200); . The output is, again, showing wrong values from the accelerometer:

10:30:29.903 -> Writing to acceldata.txt...File closed. Done.
10:30:31.396 -> X: -0.01 Y: -0.01 Z: -0.01 m/s^2
10:30:31.396 ->
10:30:31.396 -> Writing to acceldata.txt...File closed. Done.
10:30:32.916 -> X: -0.01 Y: -0.01 Z: -0.01 m/s^2
10:30:32.916 ->
10:30:32.916 -> Writing to acceldata.txt...File closed. Done.
10:30:34.433 -> X: -0.01 Y: -0.01 Z: -0.01 m/s^2
10:30:34.433 ->
10:30:34.433 -> Writing to acceldata.txt...File closed. Done.
10:30:35.944 -> X: -0.01 Y: -0.01 Z: -0.01 m/s^2

(I also added a couple of lines to notify me when writing begins and ends on every iteration).

Any further ideas?

I have my doubts with that SD module.
At least try a normal one, such as this one: https://www.adafruit.com/product/254.

A "normal" SD module might also not release the MISO signal :woozy_face:
Can you connect the sensors to the I2C bus ?

Can you give a link to your LIS3DH modules as well ?
You may not connect a output from a 5V Arduino board to a pin of a 3.3V sensor. The Arduino Uno can output up to 40mA with a digital output pin !

If you would have a 3.3V board, such as the Raspberry Pi Pico, then you can use a simple SD module: https://www.adafruit.com/product/4682 and you would not have all this 5V versus 3.3V trouble.
If you would have a ESP32 board, then you can store data on the ESP32 itself.
The Arduino Uno is good for leds and relays, but as soon as you start to use sensors, then a 3.3V board is better.

Hi,

I will try to get hold of an Adafruit SD module. Connecting 1x accelerometer via I2C and the SD module via SPI works fine - I'm just finding it difficult to connect two accelerometers via I2C - complete newb, mind you.

My LIS3DH modules are these: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-lis3dh-triple-axis-accelerometer-breakout.pdf

I'm not sure what you mean by the whole 5V vs 3.3V thing, I'm only using 5V as VCC for the SD card module. I could try connecting it to 3.3V so that 5V do not come into play at all, perhaps?

Thank you.

Adafruit LIS3DH: https://www.adafruit.com/product/2809
That module defaults to a I2C bus. It is compatible with 3.3V and 5V boards.
There is a solder jumper "SJ1" to select the other I2C address, so you can have two of those modules on the I2C bus. Do not connect SDO to 5V !

The I2C bus works with addresses. Each sensor has its own I2C address on the bus.
The Arduino board sends a I2C address to the I2C bus and all the sensors read that I2C address. Only the sensor that recognizes its own I2C address reacts with a "acknowledge" to tell the Master that is it ready to receive or send data.

There are ways to have all three sensors on the I2C bus. Either with extra hardware, such as a I2C bus multiplexer or by keeping all the sensors at the higher I2C address and use one sensor by putting that sensor at the lower I2C address. You can not do that trick, because the SDO of the sensors is not compatible with a 5V signal.

The 3.3V VCC or 5V VCC is important, but I'm talking about the signals: MISO, MOSI, CS, SDA, SCL, and so on.

A 5V Arduino board has 5V signals (a digital signal is either 0V or 5V) and has therefor a 5V I2C bus and a 5V SPI bus.
A 3.3V Arduino board has everything with 3.3V.
A 3.3V sensor has 3.3V signals.

Sometimes it works if 5V signals are connected to a 3.3V chip. Sometimes a chip gets damaged :cry: If you connect a 5V circuit to a 3.3V circuit, you must know what you are doing.

We can go on for weeks about all this 3.3V versus 5V signals, or you can get rid of all that trouble at once with a 3.3V Arduino board :smiley:
Adafruit makes most of their modules compatible with 3.3V and 5V :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.