Logging sensor data from BMP280 and BNO055

I want to log some data from a BMP280 and BNO055. I found some code online, but don't know how to use it! Could someone maybe walk me through what to do?

I think that the line: " ``` String dataString = "";

// read three sensors and append to the string:

"        is important, but I don't know what to do. 
Please let me know if anyone has some ideas!!

(Code link below)
https://www.arduino.cc/en/Tutorial/LibraryExamples/Datalogger

-Lucas Rozanski

Please Auto format your code in the IDE, select all of it, use Copy for forum in the IDE and paste it here so that it is in code tags to make it easier to read and copy for examination

See How to get the best out of this forum

/*

  SD card datalogger

  This example shows how to log data from three analog sensors

  to an SD card using the SD library. Pin numbers reflect the default

  SPI pins for Uno and Nano models

  The circuit:

   analog sensors on analog ins 0, 1, and 2

   SD card attached to SPI bus as follows:

 ** SDO - pin 11

 ** SDI - pin 12

 ** CLK - pin 13

 ** CS - depends on your SD card shield or module.

        Pin 10 used here for consistency with other Arduino examples

    (for MKRZero SD: SDCARD_SS_PIN)

  created  24 Nov 2010

  modified  24 July 2020

  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;

void setup() {

  // Open serial communications and wait for port to open:

  Serial.begin(9600);

  // wait for Serial Monitor to connect. Needed for native USB port boards only:

  while (!Serial);

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

  if (!SD.begin(chipSelect)) {

    Serial.println("initialization failed. Things to check:");

    Serial.println("1. is a card inserted?");

    Serial.println("2. is your wiring correct?");

    Serial.println("3. did you change the chipSelect pin to match your shield or module?");

    Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");

    while (true);

  }

  Serial.println("initialization done.");
}

void loop() {

  // make a string for assembling the data to log:

  String dataString = "";

  // read three sensors and append to the string:

  for (int analogPin = 0; analogPin < 3; analogPin++) {

    int sensor = analogRead(analogPin);

    dataString += String(sensor);

    if (analogPin < 2) {

      dataString += ",";

    }

  }

  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

  File dataFile = SD.open("datalog.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 datalog.txt");

  }
}

The below reads the first 3 analog inputs

  // read three sensors and append to the string:

  for (int analogPin = 0; analogPin < 3; analogPin++) {

    int sensor = analogRead(analogPin);

    dataString += String(sensor);

    if (analogPin < 2) {

      dataString += ",";

    }

  }

It is equivalent to below

  // read first analog input
  int sensor = analogRead(0);
  dataString += String(sensor);
  dataString += ",";
  
  // read second analog input
  sensor = analogRead(1);
  dataString += String(sensor);
  dataString += ",";
  
  // read third analog input
  sensor = analogRead(2);
  dataString += String(sensor);

So what you need to do is read you sensors and add the result to the String as shown in below pseudo code; and next you can save the String to file. Replace the .. by the actual reading of the sensors and assign the reading to the variables BMP280_result and BNO055_result.

  // read your BMP280
  ..
  ..
  dataString += String(BMP280_result);
  dataString += ",";

  // read your BNO055
  ..
  ..
  dataString += String(BNO055_result);

Thanks so much!! I will try this, and get back to you with my progress!

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