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);
}
DS1307 Datenblatt PDF
(ENG). SD microSD reader is made by Catalex catalex.taobao.com but I couldn't find documentation on their site :)