Advertisement
microrobotics

Mini D1 RTC DS1307 with Micro SD Reader

Mar 31st, 2023
2,912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Mini D1 RTC DS1307 with Micro SD Reader module combines a DS1307 Real-Time Clock (RTC) and a Micro SD card reader. To use this module with a D1 board, you'll need to install the "RTClib" library for the DS1307 RTC and the "SD" library for the Micro SD card reader. You can install both libraries using the Arduino Library Manager.
  3.  
  4. Here's an example of how to use the Mini D1 RTC DS1307 with Micro SD Reader module with a D1 board to read the date and time from the RTC and save it to a file on the Micro SD card:
  5.  
  6. To wire the Mini D1 RTC DS1307 with Micro SD Reader module, connect its VCC pin to the 5V or 3.3V pin on the D1 board (depending on your module's voltage range), the GND pin to the ground, the SDA pin to the D2 pin on the D1 board, and the SCL pin to the D1 pin on the D1 board. Connect the MISO, MOSI, and SCK pins of the module to the D6, D7, and D5 pins on the D1 board, respectively, and the CS pin to the D8 pin on the D1 board.
  7.  
  8. This code will continuously read the date and time from the DS1307 RTC and save it to a file named "datalog.txt" on the Micro SD card. The date and time will also be printed on the Serial Monitor.
  9. */
  10.  
  11. #include <Wire.h>
  12. #include <RTClib.h>
  13. #include <ESP8266WiFi.h>
  14. #include <SD.h>
  15. #include <SPI.h>
  16.  
  17. RTC_DS1307 rtc;
  18.  
  19. const int CS_PIN = D8; // Chip Select pin for the Micro SD card reader
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.   Wire.begin(D2, D1); // SDA: D2, SCL: D1 for D1 Mini Board
  24.  
  25.   if (!rtc.begin()) {
  26.     Serial.println("Couldn't find RTC");
  27.     while (1);
  28.   }
  29.  
  30.   if (!rtc.isrunning()) {
  31.     Serial.println("RTC is NOT running!");
  32.     // Set the RTC to the current date and time (change these values to the current date and time)
  33.     rtc.adjust(DateTime(2023, 3, 31, 12, 0, 0));
  34.   }
  35.  
  36.   pinMode(CS_PIN, OUTPUT);
  37.   if (!SD.begin(CS_PIN)) {
  38.     Serial.println("Card failed, or not present");
  39.     while (1);
  40.   }
  41.  
  42.   Serial.println("Card initialized.");
  43. }
  44.  
  45. void loop() {
  46.   DateTime now = rtc.now();
  47.   String datetime = String(now.year()) + "-" + String(now.month()) + "-" + String(now.day()) + " " +
  48.                     String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
  49.  
  50.   Serial.print("Current Date & Time: ");
  51.   Serial.println(datetime);
  52.  
  53.   saveToSDCard(datetime);
  54.  
  55.   delay(10000); // Wait 10 seconds between readings
  56. }
  57.  
  58. void saveToSDCard(String datetime) {
  59.   File dataFile = SD.open("datalog.txt", FILE_WRITE);
  60.  
  61.   if (dataFile) {
  62.     dataFile.println(datetime);
  63.     dataFile.close();
  64.     Serial.println("Data saved to datalog.txt");
  65.   } else {
  66.     Serial.println("Error opening datalog.txt");
  67.   }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement