NodeMCU-SDCARD conflicts with I2C-RTC. Separately, I can set and read the DS3231 RTC over I2C, and I can R/W the SDCARD, but I can't do both in the same sketch. When I enable the SDCARD code, it 'breaks' set/read of the RTC. I've stripped the code down to basics so I can post here. Details:
Arduino IDE 2.1.0; NodeMCU 1.0 (ESP-12E Module) selected (I'm using the HiLetGo Node MCU), like this:
I'm using a DS3231S RTC I2C, like this (mine has the DS3231SN chip):
I wired an SDCARD adapter to NodeMCU pins D5,6,7,8 and 3v3/GND as described here (which works on its own). Basically, solder the contacts of a micro-SDCARD/SDCARD adpater to the NodeMCU:
CS (Pin 1) D8 (GPIO15)
DI/MOSI (Pin 2) D7 (GPIO13)
VSS (GND) (Pin 3) GND Pin 3 or Pin6 of SD Card to GND
VDD (Pin 4) 3V3
SCLK (Pin 5) D5 (GPIO14)
VSS/GND (Pin 6) GND Pin 3 or Pin6 of SD Card to GND
DO/MISO (Pin 7) D6 (GPIO12)
I was confused by which libraries to use, but after much reading I settled on:
#include <Adafruit_I2CDevice.h>
#include <RTClib.h> // Use this in place of <DS3231.h>
// https://github.com/adafruit/RTClib/tree/master/examples
// DS3231S RTC I2C address 0x68, EEPROM 0x57
#include <Adafruit_SPIDevice.h> // added for SDCARD read
#include <SD.h>
As stated, I can get either to work independently in my code, but enabling the SDCARD /breaks/ the RTC, I just get odd results from the RTC.
I'll include two sketches. The RTC works in sketch “RTC_NO-SD_AFLIB_v002”, but fails in sketch “RTC_SD_AFLIB_v002”.
My stripped down code loads those libraries, declares a “File myFile;“ for the SDCARD, and “RTC_DS3231 rtc;” for the DS3231, a 10 second delay to allow me to start the Serial Monitor. I then do an “SD.begin” and read a file from the SDCARD, then close the file.
Then I read/print the current time from the RTC, then set it to a dummy time of 2001/02/03 04:05:06, read it back, then go into the main loop to read/print the RTC every 10 seconds. The RTC gives good values with the SDCARD code blocked out after the “File myFile;” declaration.
FYI, I only need to read the SDCARD on boot, and never need to write to it (I'm using it for a config file that I'll create/save on a computer).
( I'll include in the next post)
RTC working code “RTC_NO-SD_AFLIB_v002”:
RTC SD conflict code “RTC_SD_AFLIB_v002”:
The Serial Monitor output for the sketch w/o SDCARD code is like this:
TEN SECOND DELAY COMPLETE...
20:30:20.751 -> RTC_NO-SD_AFLIB_v002
20:30:20.751 -> Current DateTime of RTC at start:
20:30:20.751 -> 2001/02/03 04:08:32
20:30:20.751 ->
20:30:20.751 -> Now set to 2001/02/03 04:05:06:
20:30:20.751 -> 2001/02/03 04:05:06
20:30:30.756 -> 2001/02/03 04:05:16
20:30:40.753 -> 2001/02/03 04:05:26
20:30:50.758 -> 2001/02/03 04:05:36
20:31:00.751 -> 2001/02/03 04:05:46
20:31:10.748 -> 2001/02/03 04:05:56