RTC (Datalogger) modules for Wemos D1 Mini & MH ET Live Minikit

Aliexpress presents a large number of various external modules RTC and combined modules RTC + SD card or EEPROM. I will consider the most interesting for use with compact developer boards MH ET Live Minikit (ESP32) (another variant of search) or Wemos D1 Mini (ESP8266).

Tiny RTC with EEPROM at 32 Kbps

Tiny RTC Module for Datalogger
Tiny RTC Module for Datalogger
  • Tiny RTC with EEPROM at 32 Kbps:
    • RTC on the chip DS1307. The power supply Voltage of the chip 5 V.
    • Memory EEPROM 32 Kbps (Chip AT24C32).
    • The Conclusions do not coincide with the Wemos D1 Mini, although the dimensions are compatible, so it is inconvenient to use it.
    • The RTC Module uses the I2C interface, but you cannot display the bus address on the board.
    • Module Dimensions, MM: 28×25.
    • Battery for power supply: CR2032
    • Price with delivery to Russia: 0.34 USD
    • The Module is available from a large number of vendors on Aliexpress.

The Main advantage of the board-low price and the presence of the EEPROM chip on the motherboard. In some cases, a significant capacity SD card is not needed and overpay because of this extra 300 rub do not want.

Unfortunately, the normal version of this module, which would "sit" on the connector of the DEVOPERERSKIH boards could not find.

RTC + SD Card reader module

  •  RTC + SD Card reader module for board Wemos D1 Mini (ESP8266) or MH ET Live Minikit (ESP32):
    • RTC on the chip DS1307. The power supply Voltage of the chip 5 V.
    • Micro SD card Slot for logging.
    • The RTC Module uses the I2C interface, but you cannot display the bus address on the board.
    • Module Dimensions, MM: 28×25.
    • Battery for power supply: CR1220
    • Price with delivery to Russia: 1.55 USD
    • Judging by all the development of the company RobotDyn. There is also a variant of the module without SD card.
    • Diagrams are available In the documentation section.
    • The Module is available from a large number of vendors on Aliexpress.

Functionally the second module is more convenient because of the presence of SD card slot and the possibility of cascading ("sandwich") when using compact development cards MH ET Live Minikit (ESP32) or Wemos D1 Mini (ESP8266). I will Discuss the work with this module in detail.

The Scheme for connection is not required, because when you stack the development boards with Wemo D1 Mini Datalogger The necessary stumps are already docked. To connect the RTC stump:

Wemos D1 Mini
(ESP8266)
DescriptionESP32
D1 (GPIO5) Scl GPIO 22 (SCL)
D2 (GPIO4) Sda GPIO 21 (SDA)
5v5v
GndGnd

Once again I note that the power of the chip 5 V. If It is fed from 3.3 V, it does not work properly. Does not give errors at connection, but at request of date-gives incorrect.

Data Logger RTC Shield (Wemos D1 Mini) CAN not be used when battery power 3.7 V.:-(

To connect the SD card to ESP8266/ESP32:

Wemos D1 Mini
(ESP8266)
DescriptionESP32
VSPI
ESP32
HSPI
D5 (GPIO 14) CLK/SCK GPIO18 GPIO14
D6 (GPIO 12) DO/MISO GPIO19GPIO12
D7 (GPIO13) DI/MOSIGPIO23GPIO13
D8 (GPIO 15)CS/SS GPIO5GPIO15

SD card Pins are connected to the Wemos D1 conclusions without any additional static electricity protection schemes. For example, a diode assembly SMF05C.

Program

To work with the module Wemos D1 mini RTC + SD card Datalogger used the following code from the library https://github.com/Makuna/Rtc.

The Adafruit Library does not work.

CONNECTIONS
DS1307 SDA--> SDA
DS1307 SCL--> SCL
DS1307 VCC--> 5v
DS1307 GND--> GND

/* for software wire use below
#include < SoftwareWire. h >//must be included here so this Arduino library object file references work
#include < RtcDS1307. h.
SoftwareWire myWire (SDA, SCL);
RtcDS1307 < SoftwareWire > RTC (myWire);
 For software wire use above */

/* For normal hardware wire use below */
#include < Wire. h >//must be included here so this Arduino library object file references work
#include < RtcDS1307. h.
RtcDS1307 < TwoWire > RTC (Wire);
/* For normal hardware wire use above */

void Setup () 
{
    Serial. Begin (9600);

    Serial. Print ("compiled:");
    Serial. Print (__DATA__);
    Serial. println (__TIME__);

    --------RTC SETUP------------
    If you are using ESP-01 then uncomment the line below to reset the pins to
    The available pins for SDA, SCL
    Wire. Begin (0, 2); Due to limited pins, use PIN 0 and 2 for SDA, SCL
    
    RTC. Begin ();

    RtcDateTime compiled = RtcDateTime (__DATA__, __TIME__);
    printDateTime (compiled);
    Serial. println ();

    if (! RTC. IsDateTimeValid ()) 
    {
        if (RTC. LastError ()! = 0)
        {
            We have a communications error
            See https://www.arduino.cc/en/Reference/WireEndTransmission for 
            What the number means
            Serial. Print ("RTC Communications error =");
            Serial. println (RTC. LastError ());
        }
        Else
        {
            Common Cuases:
            1) First time you ran and the device wasn't running yet
            2) The battery on the device is low or even missing

            Serial. println ("RTC lost confidence in the DateTime!");
            The following line sets the RTC to the date & time this sketch was compiled
            It will also reset the valid flag internally unless the RTC device is
            Having an issue

            RTC. SetDateTime (compiled);
        }
    }

    if (! RTC. GetIsRunning ())
    {
        Serial. println ("RTC was not actively running, starting now");
        RTC. SetIsRunning (True);
    }

    RtcDateTime now = RTC. GetDateTime ();
    if (now < compiled) 
    {
        Serial. println ("RTC is older than compile time!  (Updating DateTime) ");
        RTC. SetDateTime (compiled);
    }
    else if (now > compiled) 
    {
        Serial. println ("RTC is newer than compile time. (This is expected) ");
    }
    else if (now = = compiled) 
    {
        Serial. println ("RTC is the same as compile time! (not expected but all is fine) ");
    }

    Never assume the RTC was last configured by you, so
    Just clear them to your needed state
    RTC. SetSquareWavePin (DS1307SquareWaveOut_Low); 
}

void Loop () 
{
    if (! RTC. IsDateTimeValid ()) 
    {
        if (RTC. LastError ()! = 0)
        {
            We have a communications error
            See https://www.arduino.cc/en/Reference/WireEndTransmission for 
            What the number means
            Serial. Print ("RTC Communications error =");
            Serial. println (RTC. LastError ());
        }
        Else
        {
            Common Cuases:
            1) The battery on the device is low or even missing and the power line was disconnected
            Serial. println ("RTC lost confidence in the DateTime!");
        }
    }

    RtcDateTime now = RTC. GetDateTime ();

    printDateTime (now);
    Serial. println ();

    Delay (10000); Ten seconds
}

#define COUNTOF (a) (sizeof (a)/sizeof ([0]a))

void printDateTime (const RtcDateTime & DT)
{
    Char datestring[20];

    snprintf_P (DateString, 
            Countof (datestring),
            PSTR ("% 02u/% 02u/% 04u% 02u:% 02u:% 02u"),
            Dt. Month (),
            Dt. Day (),
            Dt. Year (),
            Dt. Hour (),
            Dt. Minute (),
            Dt. Second ());
    Serial. Print (datestring);
}

The Code does not require any configuration and starts working immediately.

Work with Wemos D1 mini Datalogger via I2C (Wire. h library) without using additional libraries.

#include "Wire. H"
#define DS1307_I2C_ADDRESS 0x68

Byte decToBcd (byte val)
{
  Return ((Val/10 * 16) + (val% 10));
}

Byte bcdToDec (byte val)
{
  Return ((Val/16 * 10) + (val% 16));
}

void setDateDs1307 (Byte second,//0-59
                   BYTE minute,//0-59
                   BYTE hour,//1-23
                   BYTE dayOfWeek,//1-7
                   BYTE dayOfMonth,//1-28/29/30/31
                   BYTE month,//1-12
                   BYTE year)//0-99
{
   Wire. beginTransmission (DS1307_I2C_ADDRESS);
   Wire. Write (0);
   Wire. Write (decToBcd (second));    
   Wire. Write (decToBcd (minute));
   Wire. Write (decToBcd (hour));     
   Wire. Write (decToBcd (dayOfWeek));
   Wire. Write (decToBcd (dayOfMonth));
   Wire. Write (decToBcd (month));
   Wire. Write (decToBcd (year));
   Wire. endTransmission ();
}

void getDateDs1307 (BYTE * Second,
          BYTE * minute,
          BYTE * Hour,
          BYTE * dayOfWeek,
          BYTE * dayOfMonth,
          BYTE * month,
          BYTE * year)
{

  Wire. beginTransmission (DS1307_I2C_ADDRESS);
  Wire. Write (0);
  Wire. endTransmission ();

  Wire. requestFrom (DS1307_I2C_ADDRESS, 7);

  * Second = bcdToDec (Wire. Read () & 0x7f);
  * minute = bcdToDec (Wire. read ());
  * Hour = bcdToDec (Wire. Read () & 0x3f); 
  * dayOfWeek = bcdToDec (Wire. read ());
  * dayOfMonth = bcdToDec (Wire. read ());
  * month = bcdToDec (Wire. read ());
  * year = bcdToDec (Wire. read ());
}

void Setup ()
{
  Byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire. Begin ();
  Serial. Begin (9600);

  Second = 45;
  minute = 3;
  hour = 7;
  dayOfWeek = 5;
  dayOfMonth = 17;
  month = 4;
  Year = 8;
  setDateDs1307 (second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void Loop ()
{
  Byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  getDateDs1307 (& Second, & Minute, & Hour, & dayOfWeek, & dayOfMonth, & month, &year);
  Serial. Print (hour, DEC);
  Serial. Print (":");
  Serial. Print (minute, DEC);
  Serial. Print (":");
  Serial. Print (second, DEC);
  Serial. Print ("");
  Serial. Print (month, DEC);
  Serial. Print ("/");
  Serial. Print (dayOfMonth, DEC);
  Serial. Print ("/");
  Serial. Print (year, DEC);
  Serial. Print ("Day_of_week:");
  Serial. println (dayOfWeek, DEC);

  Delay (1000);
}

Micro SD card on Wemos D1 Mini Datalogger

The Scheme for connecting the module is still unnecessary. 🙂 ALL connections Work Normelno after assembling the "sandwich" fro
m Wemos D1 Mini (ESP8266) and Wemos D1 mini Datalogger.

Checked the work of the regular library ESP8266 to work with Micro SD card. The Test case was compiled and worked without problems. So the module works without problems.

/*
  SD Card Read/write

  This example shows how to read and write data to and from an SD card file
  The Circuit:
   SD card attached to SPI bus as follows:
 * * MOSI-pin 11
 * * MISO-pin 12
 * * CLK-pin 13
 * * CS-pin 4

  Created Nov 2010
  by David A. Mellis
  Modified 9 APR 2012
  by Tom Igoe

  This example code is in the public domain.

*/

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

File myFile;

void Setup () {
  Open serial communications and wait for port to open:
  Serial. Begin (9600);
  while (! Serial) {
    ; Wait for serial port to connect. Needed for Leonardo only
  }


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

  if (! SD. Begin (4)) {
    Serial. println ("Initialization failed!");
    Return
  }
  Serial. println ("initialization done.");

  Open the file. Note that only one file can be open at a time,
  So you have to close this one before opening another.
  myFile = SD. Open ("Test. txt", FILE_WRITE);

  If the file opened okay, write to it:
  if (myFile) {
    Serial. Print ("Writing to test. txt...");
    myFile. println ("Testing 1, 2, 3.");
    Close the file:
    myFile. Close ();
    Serial. println ("done.");
  Else
    If the file didn't open, print an error:
    Serial. println ("Error opening Test. txt");
  }

  Re-open the file for reading:
  myFile = SD. Open ("Test. txt");
  if (myFile) {
    Serial. println ("Test. txt:");

    Read from the file until there's nothing else in it:
    while (myFile. Available ()) {
      Serial. Write (myFile. read ());
    }
    Close the file:
    myFile. Close ();
  Else
    If the file didn't open, print an error:
    Serial. println ("Error opening Test. txt");
  }
}

void Loop () {
  Nothing happens after Setup
}

The SD card Connection to ESP32 is described here. The intact is all the same. No Problems should arise.

Useful Links

Spread the love
This entry was posted in IT Recipies. Bookmark the permalink.

Leave a Reply