Cannot write LiDAR Lite 2 sensor data to a SD card

Dear all,

I’ve test UNO, SD Card Shield and LiDAR Lite sensor. They are all good.

I can see sensor data printing in the serial monitor. I can also see the logfile when I run the example code from the Arduino library.

However, when I put them together and try to log sensor data to SD card, it shows the error message: error opening log.txt.

Do I miss something here? Please advise.

Here is my simple code:

#include <Wire.h>
#include <LIDARLite.h>
LIDARLite myLidarLite;


#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;


void setup() {
  Serial.begin(115200);
  myLidarLite.begin();
}

void loop() {

  // make a string for assembling the data to log:
  String dataString = "";
  dataString = String(myLidarLite.distance());

  // open the file
  File dataFile = SD.open("log.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
  
  // print to the serial port too:
    Serial.println(dataString);
  }
  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening log.txt");
  }

}

Thanks a lot.

  String dataString = "";
  dataString = String(myLidarLite.distance());

There is NO excuse for doing this, when

    dataFile.println(myLidarLite.distance());

would work just as well.

#include <LIDARLite.h>

Got a link to this library and the hardware that goes with it?

I’m guessing that either there is a memory problem or a pin conflict issue. Stopping pissing memory away unnecessarily will help, if memory is the issue.

Thanks Paul. I also learned from your other posts!

Changks, Were you able to solve the problem of compatibility "SD" and "Lidar"? I have this problem too.