2

I am running a small project. I want to use an Arduino Mega 2560 v3, a PIR sensor, a micro SD card module, an RTC 1307 module, some LEDs, buttons and a buzzer. To enable and disable motion detection and when detection is found the time is saved to the SD card.

I wired everything together, coded and started to test. During tests, Arduino on bootup was restarting over and over. First, I was thinking that the problem is in the breadboard and connection. I checked it a few times and I didn't notice anything wrong. So I checked the code over and over. Still nothing.

So I started to test modules. And I found a problem. When I don't use in the sketch either the RTC or the SD card (physically they are still connected, because I am lazy) everything works fine. Arduino doesn't restart. I didn't use the SD module to write data, instead I used the serial port to show data. Everything works fine: I can see at what time is motion detected and Arduino prints it on the terminal.

In the second scenario, I didn’t use the RTC, only the SD module. This time again everything works fine. On the SD card I could read how many times motion was detected. This is weird because when I mix all together it doesn't work.

Here is my code:

// *********************************************
// INCLUDE
// *********************************************
#include <Wire.h>                    
#include <SD.h>
#include <SPI.h>
#include "RTClib.h"
// *********************************************
// DEFINE
// *********************************************
#define GREENLED 39
#define BTNON 11 
#define REDLED 41
#define BTNOFF 9 
#define BUZZ 3 
#define PIR_PIN 10 
//SD (na mega)
#define SD_CLK_SD 52
#define SD_MI 50
#define SD_MO 51
#define SD_CS 4

//RTC 
#define RTC_SDA 20
#define RTC_SCL 21
#define RTC_DS 2

// *********************************************
// VARIABLES
// *********************************************
RTC_DS1307 rtc;
File myFile;
char godz[8];
char data[5];

int btnON;
int btnOFF;
int pirD;
bool stan_alarmu;

void wlAlarmu() {
  digitalWrite(REDLED,LOW);
  digitalWrite(GREENLED, HIGH);
  stan_alarmu=true;
  gettime();
  myFile = SD.open("dane.txt", FILE_WRITE);
  if (myFile) {
    myFile.print("### Wlaczenie alarmu ### ");
    myFile.print(" ");
    myFile.print(godz);
    myFile.print(data);
    myFile.println("");
    myFile.close();
  }
}

void wylAlarmu(){
  digitalWrite(GREENLED, LOW);
  digitalWrite(REDLED,HIGH);
  stan_alarmu=false;
  gettime();
  myFile = SD.open("dane.txt", FILE_WRITE);
  if (myFile) {
    myFile.print("### Wylaczenie alarmu ### ");
    myFile.print(" ");
    myFile.print(godz);
    myFile.print(data);
    myFile.println("");
    myFile.close();
  }
}

void alarm(){
  digitalWrite(GREENLED,LOW);
  digitalWrite(REDLED,HIGH);
  digitalWrite(BUZZ,HIGH);
  delay(500);
  digitalWrite(BUZZ,LOW);

  gettime();
  myFile = SD.open("dane.txt", FILE_WRITE);
  if (myFile) {
    myFile.print("Alarm: ");
    myFile.print(" ");
    myFile.print(godz);
    myFile.print(data);
    myFile.println("");
    myFile.close();
  }
  delay(5000);
  digitalWrite(REDLED,LOW);
  digitalWrite(GREENLED,HIGH);
}

void blad() {
  while(1){
    digitalWrite(REDLED,HIGH);
    delay(500);
    digitalWrite(REDLED,LOW);
    delay(500);
  }
}

void gettime() {
  DateTime now = rtc.now();
  sprintf(godz,"%02d:%02d:%02d ",now.hour(),now.minute(),now.second());
  sprintf(data,"%d.%d",now.day(),now.month());
}

// *********************************************
// SETUP
// *********************************************
void setup() {
  //RTC
  Wire.begin();
  // Shield I2C pins connect to alt I2C bus on Arduino Due
  rtc.begin();
  if (!rtc.isrunning()) {
    blad();
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  pinMode(BTNON,INPUT);
  pinMode(GREENLED, OUTPUT);
  pinMode(BTNOFF,INPUT);
  pinMode(REDLED, OUTPUT);
  pinMode(PIR_PIN,INPUT);
  pinMode(BUZZ,OUTPUT);

  //test seq
  digitalWrite(GREENLED,HIGH);
  digitalWrite(REDLED,HIGH);
  digitalWrite(BUZZ,HIGH);
  delay(100);
  digitalWrite(BUZZ,LOW);
  digitalWrite(GREENLED,LOW);
  digitalWrite(REDLED,LOW);
  delay(500);  
  pinMode(SD_CS, OUTPUT);
  if (!SD.begin(SD_CS)) {
    blad();
  }
  digitalWrite(GREENLED,HIGH);
  delay(1000);
  digitalWrite(GREENLED,LOW);
  gettime();
  Serial.begin(9600);

  myFile = SD.open("dane.txt", FILE_WRITE);
  if (myFile) {
    myFile.print("####START SYSTEMU: ");
    myFile.print(godz);
    myFile.print(data);
    myFile.print(" ####");
    myFile.println("");
    myFile.close();
  }
  wylAlarmu();
}

void loop() {  
  btnON = digitalRead(BTNON);
  btnOFF = digitalRead(BTNOFF); 
  pirD = digitalRead(PIR_PIN);

  if(btnON == HIGH) {
    wlAlarmu();
    delay(2000);
  }
  if(btnOFF == HIGH) {
    wylAlarmu();
    delay(2000);
  }

  if(pirD==HIGH && stan_alarmu==true) {
    alarm();
  }
  delay(1000);
}
  • Please give an exact specification of the shields you are using. Perhaps there are some clues hidden. – Ariser Apr 5 '15 at 13:29
  • My RTC module looks almost like this: arduino-projekte.de/index.php?n=45 datasheet is on bottom of page under DS1307 Datenblatt PDF (ENG). SD microSD reader is made by Catalex catalex.taobao.com but I couldn't find documentation on their site :) – GarryMoveOut Apr 5 '15 at 14:04
  • It looks, as if you use the RTC-library from adafruit. Same for the SD library? My suggestion is, that the configurations of I²C-Bus for both modules are conflicting. Most probably concerning bitrate. You should have a look inside both libraries and check, which parameters they apply to the Wire-Library. – Ariser Apr 5 '15 at 18:05
  • I coundn't find anything in libs. I found also that someone use rtc an sd library same as me and it works. michellechandra.com/physical-computing/… – GarryMoveOut Apr 6 '15 at 8:41
  • 1
    After long time i build new circut and new sketch. I used only rtc and sd, this time everything was working fine. There is no problem now. Something used to be wrong in past. – GarryMoveOut Jul 23 '15 at 20:36
1

A common issue when using strings (character vectors) is that the vector size is not updated when the format is update. Check the size required for this data. Does it fit for the sprintf() statements?

char godz[8];
char data[5];

void gettime()
{
  DateTime now = rtc.now();
  sprintf(godz,"%02d:%02d:%02d ",now.hour(),now.minute(),now.second());
  sprintf(data,"%d.%d",now.day(),now.month());
}

What is the worst case size required?

Cheers!

1

I ran into the same problem lately. I have a work-around but don't have a clear explanation of this problem. As you initialize the different modules in your setup(), make sure to perform the SD card initialization first, then followed by the RTC's. Doing this completely sorted me out. However, I am yet to explain these results.

Though it is late, just in case anyone else runs into the same problem.

0

You should start to debug your code stepwise. I.e. insert debug output commands (print via serial) at some crucial points to find the block of code which resets your device. Then narrow it down, until you know the very command which breaks your setup.

Maybe, there will be more than one point in your software, which is responsible for the failure. Nonetheless you will be wiser after that.

Then you will probably have to dive into the guts of the libraries.

If you have identified some command or procedure call responsible for the reboot, it may be still a hardware issue. Writing SD-cards consumes considerable amount of power. If you supply your device solely via USB, I recommend buying an additional PSU and see, if the behaviour changes.

The next thing is to check the reset-signal on all of your shields and daughter-boards for unwanted connections like solder-bridges or single wire threads snipped of elswhere. Another funny thing are stacking connectors not matching the mainboard and shorting something on it.

0

Are you using breakout boards as pictured in your links? Or Arduino shields?

Many Uno shields are almost, but not quite, compatible with Mega boards. It is possible there there is one or more pins that are not compatible between them.

If you're using breakout boards, double check all of the connections to the Mega. Could something be dragging down your power supply? Or actually grounding the reset pin? It would probably be one of the programmable I/O pins since the failure only depended on whether you used the SD library and seems not affected by the mere presence of the board.

  • I use very similar breakout boards as in pictures. I don't use shields. I check connections many times. – GarryMoveOut May 17 '15 at 10:58
0

When using Catalex Micro SD Card module, there is a problem with the MISO output. It is not disabled on the SPI bus because of a hardware failure. To correct the problem pin 13 on the IC must be liftet from ground connection and put together with pin 8, that have the CS signal. Now the Card Works fine with other SPI modules. Take care when soldering - it is very small pins. Best regards OZ7LM Leif

0

It is too late to answer but I had the same issue and spent a week to solve it. This will not be the exact solution. It worked and I think this is the easiest way to solve it.

First let me tell you my components:

  • WeMos D1 R2 (ESP8266)
  • MicroSD Card Adapter
  • DS3231 RTC (I tried with DS1302 but it did not work).

How I solved is, on setup part, first I initialized the RTC DS3231 then I set the built in Arduino clock to DS3231 RTC values. After that I initialized SD card. In this case you are not able to get RTC data in loop but you have it in built in Arduino clock and SD card works. If your Arduino lose electricity, then it will get the RTC data in setup again.

This solution solves it in someway.

Also there is no tutorial for how to connect these cards to WeMos D1 R2 so I want to share my connections.

SD Card    WeMos D1 R2
-------    -----------
SC      -> D14/SDA/D5
SCK     -> D13/SCK
MOSI    -> D11/MOSI
MISO    -> D12/MISO
VCC     -> 5V
GND     -> GND

RTC DS3231   WeMos
----------   -----
SCL        -> SCL/D15
SDA        -> SDA/D14
VCC        -> 3.3V
GND        -> GND

Also my code:

#include <Wire.h>
#include "RTClib.h"
#include <SD.h> //Load SD library
#include <SPI.h>
#include <time.h>
#include <TimeLib.h>
File file;
int chipSelect = 5;
RTC_DS3231 rtc;
bool not1024prev;
unsigned long timepast;
unsigned long timecurr;
int cnt = 0;
int rpm;

char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

void setup()
{

#ifndef ESP8266
    while (!Serial)
        ; // for Leonardo/Micro/Zero
#endif

    Serial.begin(9600);

    delay(3000); // wait for console opening

    if (!rtc.begin()) {
        Serial.println("Couldn't find RTC");
        while (1)
            ;
    }

    if (rtc.lostPower()) {
        Serial.println("RTC lost power, lets set the time!");
        // following line sets the RTC to the date & time this sketch was compiled
        //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
        // This line sets the RTC with an explicit date & time, for example to set
        // January 21, 2014 at 3am you would call:
        //rtc.adjust(DateTime(2018, 3, 11, 16, 29, 30));
    }
    DateTime now = rtc.now();
    setTime(now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());

    if (!SD.begin(chipSelect)) { // Initialize SD card
        Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
    }
}

void loop()
{
    String curtime = String(year()) + "." + String(month()) + "." + String(day()) + "-" + String(hour()) + ":" + String(minute()) + ":" + String(second());

    Serial.println(curtime);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.