Arduino nano just stops randomly

Hi guys,
I was already writing here about my project and managed to resolve some coding problems with your help.
I am using Arduino Nano for aquarium lights dimming. It is done by stepper motor which is turning potentiometer which further regulate 1-10V T5 ballast.
My code is working few days, and than it just stops. Stepper motor is not using power from arduino 5v pin, so it shouldn't cause power problem.

Here is the code:

#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
#include <Stepper.h>
DS3231 myRTC;
int count = 0; 
int step = 0;
int clockSec = 0;
bool h24;
bool hPM;
bool secondState = false;
bool lastSecondState = false;
Stepper myStepper = Stepper(1050, 2, 3, 4, 5);
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
  pinMode(9, OUTPUT); 
  pinMode(8, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  myStepper.setSpeed(10);
  Serial.begin(9600);
  time_t epochNow = 1659875100UL;
  Wire.begin();
  /*==============================================================SET THE CLOCK==============================================================*/
  /*
  bool mode12 = false; // sets the clock mode, 12-hour (`true`) or 24-hour (`false`)
  myRTC.setClockMode(mode12); // uploads 'false' (1) to bit 6 of register 0x02
  myRTC.setSecond(5); // uploads 42 to register 0x00
  myRTC.setMinute(9);
  myRTC.setHour(22);
  myRTC.setDoW(2); //Day of the Week
  myRTC.setDate(16); //day of the month
  myRTC.setMonth(1);
  myRTC.setYear(24);
  */
  /*==========================================================/////SET THE CLOCK==============================================================*/
  lcd.init();
  lcd.clear();
  lcd.backlight();
}
void loop() {
  if (digitalRead(8) == LOW) {
      myStepper.step(1);
      step++;
      count = 0;
      lcd.setCursor(0,0);
      lcd.print(step);
  }
  else if(digitalRead(7) == LOW){
      count = 0;
      step=0;
  }
  else if(digitalRead(6) == LOW){
      myStepper.step(-1);
      step--;
      count = 0;
      lcd.setCursor(0,0);
      lcd.print(step);
  }
  else {
    /*===============================================================SUNRISE================================================================*/
    if ((myRTC.getHour(h24,hPM) >= 8) && (myRTC.getHour(h24,hPM) < 10) ){
      if(myRTC.getSecond() == 0 || myRTC.getSecond() == 5  || myRTC.getSecond() == 10 || myRTC.getSecond() == 15 || myRTC.getSecond() == 20 || myRTC.getSecond() == 25 || myRTC.getSecond() == 30  || 
        myRTC.getSecond() == 40 || myRTC.getSecond() == 50 ){
        secondState = true;
        if(secondState != lastSecondState){
          if(step < 1050){
            step  += 1;
            myStepper.step(1);
          }
          else{
            digitalWrite(2, LOW);
            digitalWrite(3, LOW);
            digitalWrite(4, LOW);
            digitalWrite(5, LOW);
          }
        }
      }
      else{
        secondState = false;
      }
      lastSecondState = secondState;
    }
    /*==========================================================/////SUNRISE================================================================*/
    /*================================================================SUNSET================================================================*/
    if ((myRTC.getHour(h24,hPM) >= 17) && (myRTC.getHour(h24,hPM) < 20) ){
     
        if(myRTC.getSecond() == 0 || myRTC.getSecond() == 5  || myRTC.getSecond() == 10 || myRTC.getSecond() == 15 || myRTC.getSecond() == 20 || myRTC.getSecond() == 25 || myRTC.getSecond() == 30  || 
        myRTC.getSecond() == 40 || myRTC.getSecond() == 50 ){
               secondState = true;
        if(secondState != lastSecondState){
          if(step > 0){
            step  -= 1;
            myStepper.step(-1);
          }
          else{
            digitalWrite(2, LOW);
            digitalWrite(3, LOW);
            digitalWrite(4, LOW);
            digitalWrite(5, LOW);
          }
        }
        }
       else{
          secondState = false;
        }
        lastSecondState = secondState;
      
     
      
    }
    /*================================================================/////SUNSET===========================================================*/
    /*================================================================DISPLAY===============================================================*/
    if(myRTC.getSecond() != clockSec){
      if(count == 2 || count == 5){
        lcd.clear();
        } 
      count++;
      if(count == 6){
       count = 0;
      }
      clockSec = myRTC.getSecond();
    }   
     if(count < 3 ){
        /*----------------------------------------LIGHT LEVEL---------------------------------------------*/ 
                  lcd.setCursor(3,0);
                  lcd.print("LIGHT LEVEL");
                  lcd.setCursor(1,1);
                  lcd.print(step);
                  lcd.print(" from 1050");
        /*-----------------------------------/////LIGHT LEVEL---------------------------------------------*/   
      }
      else{
      /*----------------------------------------CLOCK---------------------------------------------------*/            
                lcd.setCursor(4,0);   /*Set cursor to Row 1*/
                lcd.print(myRTC.getDate()); /*print message on LCD*/
                lcd.print(".");
                bool CenturyBit;
                lcd.print(myRTC.getMonth(CenturyBit));
                lcd.print(".");
                lcd.print(myRTC.getYear());
                lcd.setCursor(0,1);   /*set cursor on row 2*/
                bool h24;
                bool hPM;
                lcd.print(myRTC.getHour(h24,hPM));
                lcd.print(":");
                lcd.print(myRTC.getMinute());
                lcd.print(":");
                lcd.print(myRTC.getSecond());
                lcd.print(" ");
                lcd.print( myRTC.getTemperature());
                lcd.print("'C");       
      /*-----------------------------------/////CLOCK---------------------------------------------------*/ 
      }   
    /*===========================================================/////DISPLAY===============================================================*/     
  }
}

If you only press the reset button on the Nano, does it happily restart? Or is it dead until you shut power off? More information is needed.

Interesting problem with several possible causes. Post an annotated schematic and note any wires over 10" / 25 cm in length. Be sure to show all connections especially power, ground, and power sources. Links to technical information on the modules would be very helpful.

Yes, it restarts normally.

I will try and draw something...

I managed to make some simple drawing in paint.
There are no wires linger than 10cm.
Here it is:

I didn't draw three more buttons, they are on pins 6,7,8 and connected to the ground.

You may be getting lockups on the i2c bus.

Are there pullups built in to the rtc or lcd module?

If not, and you are relying on the internal pullups of the Nano, would suggest stronger (lowerResistance) external pullups on the sda/scl lines. You could use 4.7K or even 2K. If there's already 4.7 k on the rtc, you can add additional resistance in parallel to have 2K total on the bus.

I would also put a large capacitor across the Vin and Ground terminals of the Arduino.

You might also want to explore using a reset_on_timeout = true setting in the Wire library. However, fixing the root cause is a better solution.

https://www.arduino.cc/reference/en/language/functions/communication/wire/setwiretimeout/

There are, 4.7KOhm. I can try and do that. How big should be capacitor?

reset_on_timeout sound good, but in my case will fail cause it will not know on which step stepper motor is at the moment. It will start from zero again. Or this doesn't reset whole arduino?

Or this doesn't reset whole arduino?

My understanding from reading Wire.cpp is that it does not reset the whole Arduino.

Sets the TWI timeout.
*

  • This limits the maximum time to wait for the TWI hardware. If more time passes, the bus is assumed
  • to have locked up (e.g. due to noise-induced glitches or faulty slaves) and the transaction is aborted.
  • Optionally, the TWI hardware is also reset

How big should be capacitor?

As big as you've got. 100uf or larger.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.