[Solved] How to fix ESP8266 crash when connected to RTC DS1302?

Hello,

I have managed to set up and have the code working when the RTC DS1302 module is connected to an Arduino UNO, but i want to improve the project with Wi-Fi so i am trying to replace the UNO with a NodeMCU 1.0 ESP-12E but the same code crashes the ESP8266. The sketch turns on a relay for 40s every hour. I think that maybe it's a problem with the RTC library that i'm using or maybe i need to add some extra library?

ds1302-real-time-clock-module-connections-600x315.jpg

Here is my complete sketch which works fine for the UNO:

#include <virtuabotixRTC.h> // DS1302 RTC module library

// Creation of the Real Time Clock Object
virtuabotixRTC myRTC(5, 6, 7);  // Wiring of the RTC (CLK,DAT,RST)

int setMinutes = 0, setSeconds = 0; // set minutes and seconds for pump activation
long pumpRelayTime = 40000; //set pump operation time in ms. int datatype can only hold max value 32767.
const int pumpRelay = 8;  // set pump relay pin

unsigned long interval = 2000;  // print RTC time every 2s
unsigned long time_now = 0;

void setup() {
  Serial.begin(9600); // open serial port and set the baud rate
  Serial.println("Automated pump relay control with RTC (Fixed Reset)");
  Serial.println("**************************************************");
  pinMode(pumpRelay, OUTPUT); // set digital pin pumpRelay as an output
  
  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year
  myRTC.setDS1302Time(50, 59, 19, 5, 26, 1, 2020); // uncomment line, upload to reset RTC and then comment, upload.
  
  myRTC.updateTime(); // update of variables for time or accessing the individual elements.
  
  // Start printing elements as individuals                                                                 
  Serial.print("Running from preset RTC Date / Time: ");                                                                  
  Serial.print(myRTC.dayofmonth);                                                                         
  Serial.print("/");                                                                                      
  Serial.print(myRTC.month);                                                                              
  Serial.print("/");                                                                                      
  Serial.print(myRTC.year);
  Serial.print(" ");                                                                                      
  Serial.print(myRTC.hours);                          
  Serial.print(":");                                                                                      
  Serial.print(myRTC.minutes);                                                                            
  Serial.print(":");                                                                                      
  Serial.println(myRTC.seconds); 
}

void loop() {
  myRTC.updateTime(); // update of variables for time or accessing the individual elements.                                                                                 

  // Start printing RTC time elements as individuals                                                                   
  if (millis() - time_now >= interval)  // non-blocking method. prints RTC time every 2 seconds.
  {
    time_now = millis();
    Serial.print("RTC Time: ");                                                                                                                                                    
    Serial.print(myRTC.hours);                                                                              
    Serial.print(":");                                                                                      
    Serial.print(myRTC.minutes);                                                                            
    Serial.print(":");                                                                                      
    Serial.println(myRTC.seconds);
  }
  if (myRTC.minutes == setMinutes && myRTC.seconds == setSeconds)
  {
    Serial.print("Pump activated at: ");
    Serial.print(myRTC.hours);  // display the current hour from RTC module                                                                              
    Serial.print(":");                                                                                      
    Serial.print(myRTC.minutes);  // display the current minutes from RTC module                                                                              
    Serial.print(":");                                                                                           
    Serial.println(myRTC.seconds);  // display the seconds from RTC module  
                                                                                   
    digitalWrite(pumpRelay, HIGH);  //turns on the pump relay
        
    Serial.println("*** Pump relay turned ON ***");
    Serial.print("Pump operation time: ");
    Serial.print(pumpRelayTime / 1000); // calculate time of operation in seconds      
    Serial.println(" seconds");
    delay(pumpRelayTime); // pump relay operation time
    digitalWrite(pumpRelay, LOW); // turn off the pump relay
    Serial.println("*** Pump relay turned OFF ***");                                                                                                                    
  }
}

Node-MCU-ESP-12E-Pin-Out-Diagram2.jpg

For the NodeMCU, in the Arduino IDE, the same code compiled and uploaded with no errors. The only changes are the GPIO pins. Maybe i should use different pins?

virtuabotixRTC myRTC(10, 13, 15);  // Wiring of the RTC (CLK,DAT,RST)
const int pumpRelay = 9;  // set pump relay pin

But when i open the Serial Monitor, i get this repeating error:

21:33:24.465 -> 
21:33:24.465 ->  ets Jan  8 2013,rst cause:4, boot mode:(3,2)
21:33:24.512 -> 
21:33:24.512 -> wdt reset
21:33:24.512 -> load 0x4010f000, len 1392, room 16 
21:33:24.512 -> tail 0
21:33:24.512 -> chksum 0xd0
21:33:24.512 -> csum 0xd0
21:33:24.512 -> v3d128e5c
21:33:24.512 -> ~ld
21:33:24.559 ->

ds1302-real-time-clock-module-connections-600x315.jpg

Node-MCU-ESP-12E-Pin-Out-Diagram2.jpg

Possibly there is a collision, or smiliar, between pins, timers, interrupts,.... when using NodeMCU 1.0 ESP-12E. Libraries… yes, possible.

What about continue using the well working UNO and add a Wifi device to the UNO?

Railroader:
Possibly there is a collision, or smiliar, between pins, timers, interrupts,.... when using NodeMCU 1.0 ESP-12E. Libraries… yes, possible.

What about continue using the well working UNO and add a Wifi device to the UNO?

I want to replace the UNO with the ESP8266 as i don't want to 'lose' my currently only one Arduino UNO into this project which will be a permanent or long-term setup. The NodeMCU should be perfectly capable of running this project on its own and it has built-in Wi-Fi which makes the final setup more compact with less parts and decreases the overall cost of the project. I can't understand why the code doesn't work. Hopefully someone with some more experience with the ESP8266 can enlighten me. :frowning:

There are surely several skilled helpers knowing a lot more that can find out. Buying a second UNO clone does not cost much so that is like an emergncy exit in my opinion.

So, i changed the pin configuration to the following based on another DS1302 library that i downloaded from the Arduino IDE Library Manager and it is no longer crashing but then i could not set the time on the RTC, so i connected the VCC and GND pins of the RTC to the NodeMCU and now everything seems to work properly:

virtuabotixRTC myRTC(5, 4, 2);  // Wiring of the RTC (CLK,DAT,RST)
const int pumpRelay = 14;  // set pump relay pin

Here is the Serial Monitor output:

22:05:29.037 -> Automated pump relay control with RTC (Fixed Reset)
22:05:29.105 -> **************************************************
22:05:29.138 -> Running from preset RTC Date / Time: 26/1/2020 19:59:50
22:05:30.957 -> RTC Time: 19:59:51
22:05:32.983 -> RTC Time: 19:59:53
22:05:34.967 -> RTC Time: 19:59:55
22:05:36.957 -> RTC Time: 19:59:57
22:05:38.975 -> RTC Time: 19:59:59
22:05:39.042 -> Pump activated at: 20:0:0
22:05:39.075 -> *** Pump relay turned ON ***
22:05:39.075 -> Pump operation time: 40 seconds
22:06:19.022 -> *** Pump relay turned OFF ***
22:06:19.055 -> RTC Time: 20:0:40
22:06:21.053 -> RTC Time: 20:0:42
22:06:23.028 -> RTC Time: 20:0:44
22:06:25.033 -> RTC Time: 20:0:46
22:06:27.039 -> RTC Time: 20:0:48

How do You pwr the UNO and what devices get their pwr from the UNO? A diagram showing the powering of all Your stuff could be good to see.

When using the DS1302 library with ESP8266 you need to specify the GPIO numbers, not the digital pin numbers. So: DS1302 RTC(14, 12, 13);