Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- 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:
- 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.
- 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.
- */
- #include <Wire.h>
- #include <RTClib.h>
- #include <ESP8266WiFi.h>
- #include <SD.h>
- #include <SPI.h>
- RTC_DS1307 rtc;
- const int CS_PIN = D8; // Chip Select pin for the Micro SD card reader
- void setup() {
- Serial.begin(9600);
- Wire.begin(D2, D1); // SDA: D2, SCL: D1 for D1 Mini Board
- if (!rtc.begin()) {
- Serial.println("Couldn't find RTC");
- while (1);
- }
- if (!rtc.isrunning()) {
- Serial.println("RTC is NOT running!");
- // Set the RTC to the current date and time (change these values to the current date and time)
- rtc.adjust(DateTime(2023, 3, 31, 12, 0, 0));
- }
- pinMode(CS_PIN, OUTPUT);
- if (!SD.begin(CS_PIN)) {
- Serial.println("Card failed, or not present");
- while (1);
- }
- Serial.println("Card initialized.");
- }
- void loop() {
- DateTime now = rtc.now();
- String datetime = String(now.year()) + "-" + String(now.month()) + "-" + String(now.day()) + " " +
- String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
- Serial.print("Current Date & Time: ");
- Serial.println(datetime);
- saveToSDCard(datetime);
- delay(10000); // Wait 10 seconds between readings
- }
- void saveToSDCard(String datetime) {
- File dataFile = SD.open("datalog.txt", FILE_WRITE);
- if (dataFile) {
- dataFile.println(datetime);
- dataFile.close();
- Serial.println("Data saved to datalog.txt");
- } else {
- Serial.println("Error opening datalog.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement