High altitude balloon project

Hi, I'm a student who is working on sending a helium balloon into the stratosphere. And this is my first time using a Arduino Uno. I was searching for code that is similar to what I'm doing: an adafruit data logger (P1141) connected to a MPL3115A2 sensor. So I found this link (https://forums.adafruit.com/viewtopic.php?f=22&t=61365&fbclid=IwAR1dFtCgCZWx23HZCLhpvtJcc6t5LSHkHHjsTqJHtJwk5bUCMGw7dHYB8Dg) But I am experiencing the same trouble as the person in the post. Especially the part where he changed his code to

String dataString = String(id) + "," + String (c) + "*C" + "," + String(f) + "*F";

And this is what I have, and it gives an error :'id' is not declared in this scope

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
    
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header
       Pin 4 used here for consistency with other Arduino examples
 
 a combination of sketches by adafruit used for SD card logger 
 and mcp9808 temp sensor
 
 modified 9 Apr 2012 by Tom Igoe

 
 This example code is in the public domain.
     
 */

#include <SPI.h>    //SPI Library
#include <SD.h>     //SD Library
#include <Wire.h>   //I2C Library
#include "Adafruit_MPL3115A2.h"

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

//I2C Temperature Pins
   //SDA = Analog Pin 4
   //SCL = Analog Pin 5

// Create the MPL3115A2 temperature sensor object
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();

File dataFile;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("MPL3115A data"); //Heading line
  // Make sure the sensor is found, you can also pass in a different i2c
  // address with tempsensor.begin(0x19) for example
  if (!baro.begin()){
    Serial.println("Couldn't find MPL3115A2!");
    while (1);
  }

  Serial.println("Initializing SD card");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");
  
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE); //'tempsensor' was previously named 'datalog'
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
}

// loop below needs to read and print data to sd card and screen

void loop()
{
  
  // Here is the code from adafruit for reading the temp data:
  
  // Read and print out the temperature, then convert to *F
  //float c = tempsensor.readTempC();
  //float f = c * 9.0 / 5.0 + 32;
  //Serial.print("Temp: "); Serial.print(c); Serial.print("*C\t"); 
  //Serial.print(f); Serial.println("*F");
  //delay(3000);
  

  float pascals = baro.getPressure();
  // Our weather page presents pressure in Inches (Hg)
  // Use http://www.onlineconversion.com/pressure.htm for other units
  Serial.print(pascals/3377); Serial.println(" Inches (Hg)");

  float altm = baro.getAltitude();
  Serial.print(altm); Serial.println(" meters");

  float tempC = baro.getTemperature();
  Serial.print(tempC); Serial.println("*C");

  delay(3000);

    
  // make a string for assembling the data to log:
  String dataString = String(id) + "," + String (c) + "*C" + "," + String(f) + "*F";

  // read MCP9808 on A4 and append to the string:
  
  
Serial.println(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:
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
    delay(3000);
  }    
}

Thank you so much for your help in advance! :smiley:

Yes I know... I just doesn't understand what he did to fix the code. If I remove String dataString = String(id) + "," + String (c) + "*C" + "," + String(f) + "*F"; Then there is nothing on the SD card. So I guess the question is how to fix this:

Your 'dataString' is initialized to an empty string in your loop. Since there is no string to print, nothing gets written to the file.

Sorry for the confusion, thank you for the quick reply!

Looking at it in context, I'd say that 'id' is just an identifier for that particular unit, or an id for that particular reading (time, reading number, whaterver)
I'd say either just create an int variable/const with a counter that 'identifies' the unit and see what happens
eg

int id = 0;

void setup() {
  ...
}

void loop() {
  ...
  // Read and print out the temperature, then convert to *F
  float c = tempsensor.readTempC();
  float f = c * 9.0 / 5.0 + 32;
  // update reading identifier
  id+=1;

  ...
   
  // make a string for assembling the data to log:
  String dataString = String(id) + "," + String (c) + "*C" + "," + String(f) + "*F";
  ...

or just remove it altogether and likewise, see what happens

  ...
  // make a string for assembling the data to log:
  String dataString = String (c) + "*C" + "," + String(f) + "*F";
  ...
1 Like

Thank you so much, I will try it now

Thank you so much for your help I understand what the code does now!

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
    
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
  
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header
       Pin 4 used here for consistency with other Arduino examples
 
 a combination of sketches by adafruit used for SD card logger 
 and mcp9808 temp sensor
 
 modified 9 Apr 2012 by Tom Igoe

 
 This example code is in the public domain.
     
 */

#include <SPI.h>    //SPI Library
#include <SD.h>     //SD Library
#include <Wire.h>   //I2C Library
#include "Adafruit_MPL3115A2.h"

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

//I2C Temperature Pins
   //SDA = Analog Pin 4
   //SCL = Analog Pin 5

// Create the MPL3115A2 temperature sensor object
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();

File dataFile;


void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("MPL3115A data"); //Heading line
  // Make sure the sensor is found, you can also pass in a different i2c
  // address with tempsensor.begin(0x19) for example
  if (!baro.begin()){
    Serial.println("Couldn't find MPL3115A2!");
    while (1);
  }

  Serial.println("Initializing SD card");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");
  
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE); //'tempsensor' was previously named 'datalog'
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
}

// loop below needs to read and print data to sd card and screen

void loop()
{
  
  // Here is the code from adafruit for reading the temp data:
  
  // Read and print out the temperature, then convert to *F
  //float tempC = baro.getTemperature();
  //float f = c * 9.0 / 5.0 + 32;
  //Serial.print("Temp: "); Serial.print(c); Serial.print("*C\t"); 
  //Serial.print(f); Serial.println("*F");
  //delay(3000);
  

  float pascals = baro.getPressure();
  // Our weather page presents pressure in Inches (Hg)
  // Use http://www.onlineconversion.com/pressure.htm for other units
  //Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
  //Serial.print(pascals); Serial.println("Pa");

  float altm = baro.getAltitude();
  //Serial.print(altm); Serial.println(" meters");

  float tempC = baro.getTemperature();
  //Serial.print(tempC); Serial.println("*C");

  //delay(3000);

    
  // make a string for assembling the data to log:
  String dataString = String (tempC) + "C" + "," + String(altm) + "m" + "," + String(pascals) + "(Pa)";
  delay(3000);
  // read MCP9808 on A4 and append to the string:
  
  
Serial.println(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:
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
    delay(3000);
  }    
}

But it still does not give me the time, the data logger has a RTC embedded within it, and in the code it says that

The circuit:
* SD card attached to SPI bus as follows:
** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)

  • and pin #10 (SS) must be an output*

But I don't see where the CLK, CS, and SS pins are. I have the MOSI, MISO, and SCK connected to #11, #12, #13. Does anyone know what CLK, CS and SS does and where I can find it on a Arduino Uno?

Thanks a lot!

Weigeng:
But I don't see where the CLK, CS, and SS pins are. I have the MOSI, MISO, and SCK connected to #11, #12, #13. Does anyone know what CLK, CS and SS does and where I can find it on a Arduino Uno?

Thanks a lot!

The bit you highlighted tells you

The circuit:
* SD card attached to SPI bus as follows:
** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)

  • and pin #10 (SS) must be an output*

SCK and CLK are the same thing.
CS - chip select.
SS - slave selct.

Get rid of the String altogether. Write characters and put a zero at the end of a "string". The last thing you want is for your sketch to crash in the sky because it ran out of memory.

Does your post have anything to do with high altitude balloonery? Your electronic package will be experiencing temperatures around -40, sudden mechanical shocks (when the balloon bursts), and likely a crash landing. Surely that is more of a challenge than wayward variables.