SparkFun Forums 

Where electronics enthusiasts find answers.

Questions &/or issues related to products in the Qwiic category should be posted in this forum. To learn more about Qwiic, consider checking out our pillar page on the Qwiic system.
User avatar
By laurentchivot
#208291
Hello there,

I have a simple setup that logs temperature over time.

I use the RTC from my board https://support.sodaq.com/Boards/ExpLoRer/ and the onboard temp sensor.
formatting my log file is important, so here is the code i wrote from the examples
Code: Select all
#include <Wire.h>
#include "SparkFun_Qwiic_OpenLog_Arduino_Library.h"
#define debugSerial SerialUSB
OpenLog myLog; //Create instance
#include <RTCZero.h>


/* Create an rtc object */
RTCZero rtc;

/* Change these values to set the current initial time */
byte seconds = 0;
byte minutes = 0;
byte hours = 0;

/* Change these values to set the current initial date */
byte day = 1;
byte month = 1;
byte year = 1970;

void setup() {
  // put your setup code here, to run once:
    delay(10000);
    debugSerial.begin(9600);
    debugSerial.println("starting logger...waiting for 10 sec");
    Wire.begin(); //Initialize I2C
    myLog.begin(); //Open connection to OpenLog (no pun intended)
    rtc.begin(); // initialize RTC
    rtc.setEpoch(0);
    myLog.println("format descriptor : heures, minutes, secondes,Temp °C");
    
    
    //Set the temperature sensor pin as input
  pinMode(TEMP_SENSOR, INPUT) ;
  //Set ADC resolution to 12 bits
  analogReadResolution(12) ;
  //myLog.println(rtc.getMinutes());
  //debugSerial.println(rtc.getSeconds());
  

}

void loop() {
  // put your main code here, to run repeatedly:
  float temperature = getTemperature();
  myLog.print(String(rtc.getHours()));
  myLog.print(",");
  myLog.print(String(rtc.getMinutes()));
  myLog.print(",");
  myLog.syncFile();
  myLog.print(String(rtc.getSeconds()));
  myLog.print(",");
  myLog.println(String(temperature));
  delay(15);
  myLog.syncFile();
  debugSerial.println(temperature);
  delay(6000);
  
  
  
  

}
void print2digits(int number) {
  if (number < 10) {
    debugSerial.print("0"); // print a 0 before if the number is < than 10
  }
  debugSerial.print(number);
}
float getTemperature()
{
  uint8_t samples = 10;

  float sum = 0.0 ;    // reset averaged reading to start at 0
  // This loop does 10 readings of the voltage, and adds this to the variable
  for(int i = 0; i < samples; i++)
    sum += 3.3/4095.0 * (float)analogRead(TEMP_SENSOR) ;

  // Divide the voltage to get average
  float voltage = sum / samples ;
  
  // Convert voltage to temperature
  double temperature = (voltage - 0.5) * 100.0 ;
  return temperature;
}
it records well, but from time to time the println is not taken into account and produces inconsictency in my file

EX : See Attached logfile

Any help appreciated
You do not have the required permissions to view the files attached to this post.
User avatar
By TS-Mark
#208373
Hi laurentchivot,

If you can, try adding some short delays in between each print to the OpenLog and that should help with the inconsistency. The Troubleshooting Section of our Hookup Guide for the Qwiic OpenLog has a good example of that along with some other helpful troubleshooting tips for the board.
User avatar
By TS-Mark
#208400
Ah, v2.0 firmware could also be an issue here since the syncfile command only works on v3.0. Luckily, the Qwiic OpenLog can have its firmware updated fairly easily if you have a serial to USB converter. We briefly cover how to update the firmware in the Qwiic OpenLog Hookup Guide.

Let us know if adding delays does not help and if you need any assistance updating the firmware and we would be happy to help.