Adafruit data logger - RTC problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mazZz
 
Posts: 7
Joined: Sun Oct 10, 2021 4:54 pm

Adafruit data logger - RTC problem

Post by mazZz »

Hi, I have an Arduino mega 2560 R3 and a Datalogger shield for R3. I get some "strange" behavior when I try to combine it with other modules. For example the temperature and humidity sensor AM2302. I'm pretty new to Arduino and the addon modules. Any help would be appreciated :)

I have tried this:
  • The "pcf8523" sketch from the RTClib examples. This "works", I had to set the time manually, I could not use the rtc.adjust(DateTime(F(__DATE__), F(__TIME__))) function. But it prints the time and date to the serial monitor.
    The "DHT_Unified_Sensor sketch also runs fine, it displays the temperature and humidity to the serial monitor.
Then I went for the combo of logging the temperature and humidity with timestamps using the code in https://learn.adafruit.com/adafruit-dat ... me-clock-3 as base. Now here the strange things starts, I get the correct temperature and humidity logged to file on the SD card. But the time stamp is wrong, it is constantly the same values for the current logging (it changes if I make a new data logging). A short excerpt of the logfile is shown below. Also the date and time for the file is wrong, it is always: 2000-01-01 00:00. On top of that I get no printouts in the serial monitor.

It feels like there is a conflict between the modules. But I can't see/understand where/how?

millis,datetime,temp,humid
999, "2010/23/49 8:0:0", 23.80, 45.00
1999, "2010/23/49 8:0:0", 23.80, 45.00
2999, "2010/23/49 8:0:0", 23.80, 45.30
IMG_5234_2.JPEG
IMG_5234_2.JPEG (535.76 KiB) Viewed 268 times
I also tried the combo with a thermocouple module (MAX31855) and a LCD which worked before I added the data logger shield. After adding the data logger the LCD went bananas. I use a I2C shield for the LCD but it has another address (0x27) than the I2C for the data logger (0x60).

Code: Select all

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 2     // Digital pin connected to the DHT sensor 
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
DHT_Unified dht(DHTPIN, DHTTYPE);

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

RTC_DS1307 RTC; // define the Real Time Clock object

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  while(1);
}

void setup(void)
{
  Serial.begin(9600);
#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
  dht.begin();        // Initialize DHT device.

  // initialize the SD card
  Serial.println();
  Serial.print("Initializing SD card...");
  Serial.flush();
  // 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)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
  
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);

  // connect to RTC
  Wire.begin();  
  if (!RTC.begin()) {
    logfile.println("RTC failed");
    Serial.println("RTC failed");
  }
  logfile.println("millis,datetime,temp,humid");    
  Serial.println("millis,datetime,temp,humid");
}

void loop(void)
{
  DateTime now;

  // delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  
  // log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");    
  Serial.print(m);         // milliseconds since start
  Serial.print(", ");  

  // fetch the time
  now = RTC.now();
  // log time
  logfile.print('"');
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print('"');

  Serial.print('"');
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print('"');


  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  }
  else {
    Serial.print(F("Temperature: "));
    Serial.print(event.temperature);
    Serial.print(F("°C"));
    logfile.print(", ");    
    logfile.print(event.temperature);
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
    logfile.print(", ");    
    logfile.print(event.relative_humidity);
  }
  logfile.println();
  

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
  
  logfile.flush();
  
}

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adafruit data logger - RTC problem

Post by adafruit_support_carter »

The "pcf8523" sketch from the RTClib examples. This "works",
Does this still work as it previously did with all the additional hardware attached?

User avatar
mazZz
 
Posts: 7
Joined: Sun Oct 10, 2021 4:54 pm

Re: Adafruit data logger - RTC problem

Post by mazZz »

It runs fine with the temp/humid sensor attached. It also runs fine with the LCD + MAX31855 modules. When I run the program for the LCD combo everything goes fine until I call a RTC function, then the LCD goes crazy (blinks and displays strange signs)

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adafruit data logger - RTC problem

Post by adafruit_support_carter »

What's the LCD? Not seeing reference to that in the example sketch.

User avatar
mazZz
 
Posts: 7
Joined: Sun Oct 10, 2021 4:54 pm

Re: Adafruit data logger - RTC problem

Post by mazZz »

Sorry if the "LCD example" confused my answer. I have tried the data logger in two different setups. The initial one was with a LCD and a temperature measurement module (MAX31855). It was there I encountered the problem initially. In an attempt to rule out that it was the LCD/MAX modules that caused the problem I setup the more simple case with the AM2303 sensor. In the initial post I only showed the AM2303 setup and sketch (tried to keep it as simple as possible), but I mentioned the LCD case in the last paragraph. So to summarize:

case 1: temp/humid sensor (AM2303) + data logger
- The pcf8523 sketch runs fine with the data logger and sensor connected at the same time
- Using the RTC-functions, e.g. DateTime now; now = RTC.now(); logfile.print(now.year(), DEC);, does not produce the expected result (see the logfile excerpt in the initial post).
- A logfile is created (but with faulty time and date)
- The serial.print does not show up on the serial monitor

case 2: LCD + MAX31855 + data logger
- The pcf8523 sketch runs fine with the data logger and LCD + sensor connected at the same time
- When running the sketch (not shown in the post) everything is fine until the first time I call a RTC function (DateTime now = rtc.now(); in that particular case). Then the LCD starts to flicker and random signs are printed to the screen.


I did not add the sketch for the LCD + logger as I thought it would make the post too long and confusing. Anyway, below is the "LCD sketch". If I remove the lines with the RTC calls it works fine. If not, the last ok line is; lcd.print('3');

Code: Select all

static const int dummyvar = 0; // dummy declaration for older broken IDEs!!!!
// vi:ts=4
// ----------------------------------------------------------------------------
// LCDCustomChars - simple demonstration of lcd custom characters
// Created by Bill Perry 2016-10-06
// [email protected]
//
// This example code is unlicensed and is released into the public domain
// ----------------------------------------------------------------------------
// You can create your own custom characters.
// Here are a couple of web pages that have a tool that will generate the data
// values needed for custom character.
// https://kakedev.github.io/GlyphGenerator/
// http://www.quinapalus.com/hd44780udg.html
// https://omerk.github.io/lcdchargen
//
#include "RTClib.h"             // Lib for the Real Time Clock
#include "Adafruit_MAX31855.h"  // Shield for reading of thermocouple data
#include <SPI.h>
#include <Wire.h>
#include <hd44780.h> // include hd44780 library header file
#include <hd44780ioClass/hd44780_I2Cexp.h> // i/o expander/backpack class
hd44780_I2Cexp lcd; // auto detect backpack and pin mappings

// ----------------------------------------------------------------------------
// LCD geometry
const int LCD_COLS = 20;
const int LCD_ROWS = 4;

const int LongDelay = 5000;
const int ShortDelay = 800;

int customcharRow = 1; // default to printing custom chars on row 1

// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO   3
#define MAXCS   4
#define MAXCLK  5

// ----------------------------------------------------------------------------
// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);
int intT;
char str[10];

// ----------------------------------------------------------------------------
// RTC
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// ----------------------------------------------------------------------------


// Below are some custom characters for demonstration
uint8_t bell[8]  = {0x04,0x0e,0x0e,0x0e,0x1f,0x00,0x04,0x00};
uint8_t note[8]  = {0x02,0x03,0x02,0x0e,0x1e,0x0c,0x00,0x00};
uint8_t clockface[8] = {0x00,0x0e,0x15,0x17,0x11,0x0e,0x00,0x00};
uint8_t heart[8] = {0x00,0x0a,0x1f,0x1f,0x0e,0x04,0x00,0x00};
uint8_t duck[8]  = {0x00,0x0c,0x1d,0x0f,0x0f,0x06,0x00,0x00};
uint8_t check[8] = {0x00,0x01,0x03,0x16,0x1c,0x08,0x00,0x00};
uint8_t cross[8] = {0x00,0x1b,0x0e,0x04,0x0e,0x1b,0x00,0x00};
uint8_t smile[8] = {0x00,0x0a,0x0a,0x00,0x00,0x11,0x0e,0x00};
uint8_t degreeSymbol[8]= {0x06,0x09,0x09,0x06,0x00,0x00,0x00,0x00};
uint8_t degreeC[8]     = {0x18,0x18,0x03,0x04,0x04,0x04,0x03,0x00};
uint8_t degreeF[8]     = {0x18,0x18,0x07,0x04,0x07,0x04,0x04,0x00};

const PROGMEM uint8_t vsigbar[][8] = {
	{0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // 0 bars, same as <space>
	{0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x1F}, // 1 bars
	{0x00, 0x00,0x00,0x00,0x00,0x00,0x1F,0x1F}, // 2 bars
	{0x00, 0x00,0x00,0x00,0x00,0x1F,0x1F,0x1F}, // 3 bars
	{0x00, 0x00,0x00,0x00,0x1F,0x1F,0x1F,0x1F}, // 4 bars
	{0x00, 0x00,0x00,0x1F,0x1F,0x1F,0x1F,0x1F}, // 5 bars
	{0x00, 0x00,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}, // 6 bars
	{0x00, 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}, // 7 bars
	{0x1F, 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}, // 8 bars
};

void setup()
{
  rtc.start();
  int status;
	status = lcd.begin(LCD_COLS, LCD_ROWS);
	if(status) // non zero status means it was unsuccesful
	{
		// begin() failed so blink error code using the onboard LED if possible
		hd44780::fatalError(status); // does not return
	}

	// initalization was successful, the backlight should be on now
 
 // Initialize the MAX31855 adapter
  lcd.clear();
  lcd.print("Initializing sensor");
  delay(1000);
  if (!thermocouple.begin()) {
    lcd.print("ERROR!");
    while (1) delay(LongDelay);
  }
  lcd.clear();
  lcd.setCursor(8,1);
  lcd.print("DONE!");
  delay(1000);
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("KOLHUGGET.SE");
  delay(1000);
  lcd.setCursor(1, 1);
  lcd.print('1');  
  delay(1000);
  lcd.print('2');  
    delay(1000);
  lcd.print('3');
    delay(1000);  
  DateTime now = rtc.now();
  lcd.print('4');  
  lcd.print('5');  
  delay(1000);
  lcd.setCursor(1, 1);
  lcd.print('2');  

  
//  sprintf(str," %d-%d-%d %d:%d:%d",now.year(),now.month(),now.day(),now.hour(),now.minute(),now.second());
//  lcd.setCursor(0, 2); 
//  delay(1000);
//  lcd.setCursor(1, 1);
//  lcd.print('3');  
//  lcd.write(str);

  delay(1000);
 
}
void loop(void)
{
//  DateTime now = rtc.now();
//	lcd.clear();
  
	// create custom characters
	// int rval = createChar(charval, charmap[]);
	//
	// createChar() creates a custom character
	// for the character at the charval codepoint.
	// It returns zero if successful.
	//
	// to display the custom character, simply call write()
	// with the charval use in createChar()
	//
	// The display must be initialized *before* you attempt
	// to create custom characters.
	//
	// Note: On hd44780 displays there are 8 custom characters.
	// They are assigned to character codepoint values 0x00 to 0x07
	// The codepoints 0x08 to 0x0f are duplicates for 0x00 to 0x07
	// i.e. 0x08 is the same as 0x00, 0x09 same as 0x01, etc...

	// create custom characters
	lcd.createChar(1, degreeSymbol);
	lcd.createChar(2, degreeC);


	// To use write() pass the charval of the desired custom character.
	//    lcd.write(charval);
	// To use print() pass in the charval of the desired custom character
	// as a character *not* an integer.
	// This requires using a literal character with an octal escape sequence.
	//    lcd.print('\###');
	//

  // TC : inre
  double TC = thermocouple.readCelsius();
  intT = (int)TC;
  sprintf(str,"T_i %d\001C ",intT);
	lcd.setCursor(0, 1); 
  lcd.write(str);
  // TC : mitten
  TC = thermocouple.readCelsius();
  intT = (int)TC;
  sprintf(str,"T_m %d\001C ",intT);
  lcd.setCursor(0, 2); 
  lcd.write(str);
  // TC : yttre
  TC = thermocouple.readCelsius();
  intT = (int)TC;
  sprintf(str,"T_y %d\001C ",intT);
  lcd.setCursor(0, 3); 
  lcd.write(str);
  // TC : ved
  TC = thermocouple.readCelsius();
  intT = (int)TC;
  sprintf(str,"Tved %d\001C ",intT);
  lcd.setCursor(10, 1); 
  lcd.write(str);
  // TC : gas
  TC = thermocouple.readCelsius();
  intT = (int)TC;
  sprintf(str,"Tgas %d\001C ",intT);
  lcd.setCursor(10, 2); 
  lcd.write(str);
  // TC : internt (Arduino)
  double Tinternal = thermocouple.readInternal();
  intT = (int)Tinternal;
  sprintf(str,"Tard %d\001C ",intT);
  lcd.setCursor(10, 3); 
  lcd.write(str);

  delay(1000);

}

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adafruit data logger - RTC problem

Post by adafruit_support_carter »

Since we are troubleshooting, the simplest setup is the best - your case1 configuration.

This seems odd:
- The serial.print does not show up on the serial monitor
Why would the serial monitor also stop working? Do you get anything in the serial monitor? If so, what?

User avatar
mazZz
 
Posts: 7
Joined: Sun Oct 10, 2021 4:54 pm

Re: Adafruit data logger - RTC problem

Post by mazZz »

No, nothing shows up in the serial monitor. Yes, that is odd :)

Comment on my previous post.
- A logfile is created (but with faulty time and date)

should be:

- A logfile is created (but with faulty time and date)
- The (faulty) log time and date is written to the file including the correct reading of the temperature and humidity

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adafruit data logger - RTC problem

Post by adafruit_support_carter »

In your original sketch:

Code: Select all

    void setup(void)
    {
      Serial.begin(9600);
    #ifndef ESP8266
      while (!Serial); // wait for serial port to connect. Needed for native USB
    #endif
      dht.begin();        // Initialize DHT device.

      // initialize the SD card
      Serial.println();
      Serial.print("Initializing SD card...");
Change that to:

Code: Select all

    void setup(void)
    {
      Serial.begin(9600);
      while (!Serial); // wait for serial port to connect. Needed for native USB
      Serial.println("RTC Logger Test.");

      dht.begin();        // Initialize DHT device.

      // initialize the SD card
      Serial.println();
      Serial.print("Initializing SD card...");
and run it again. You should at least see "RTC Logger Test." in the serial monitor.

User avatar
mazZz
 
Posts: 7
Joined: Sun Oct 10, 2021 4:54 pm

Re: Adafruit data logger - RTC problem

Post by mazZz »

Doh, found the problem with the LCD setup... A bug in the sketch, rtc.start() should be rtc.begin()
void setup()
{
rtc.start();
int status;
status = lcd.begin(LCD_COLS, LCD_ROWS);
That points to that I have a bug in the sketch with the AM2303 sensor too. Can't see it ... But I bet its there x)

User avatar
mazZz
 
Posts: 7
Joined: Sun Oct 10, 2021 4:54 pm

Re: Adafruit data logger - RTC problem

Post by mazZz »

Serial.println("RTC Logger Test.");
Sorry, no luck with that change. Still no output in the serial monitor.

I suspect that I screw up the initialization of something?

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adafruit data logger - RTC problem

Post by adafruit_support_carter »

What do you get with this? Should figure out why serial monitor is no longer working.

Code: Select all

int count = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(count++);
  delay(1000);
}

User avatar
mazZz
 
Posts: 7
Joined: Sun Oct 10, 2021 4:54 pm

Re: Adafruit data logger - RTC problem

Post by mazZz »

What do you get with this? Should figure out why serial monitor is no longer working.
The serial monitor printout works now. Seem like the serial monitor had lost connection (?) When I tried your program it did not work so I restarted the monitor and then I got print outs from my sketch also.

The time and date object are still wrong tho. I get the following printout in the serial monitor:
215000, "2012/21/20 8:0:0"Temperature: 21.60°CHumidity: 44.50%

It seems like there is a "shift" in the values
now.month() gives 21 but that is actually the hour
now.day() gives 20 but that is the current minute

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”