Arduino Micro SD module - Intruder Alert!

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

OVERVIEW

In this tutorial we’re gonna use the IR Obstacle Avoidance sensor to detect movement, and then write the time and date of the event to a MicroSD card with the help of the RTC1307 real time clock module we saw in the previous tutorial.

The IR sensor, like many others (the sound detection module for example), require only 1 pin that will go either HIGH or LOW when it is triggered.

The Arduino MicroSD module uses the SPI Bus.

The Arduino IDE already comes with a library to make writing to the card very simple.

So when the pin on the IR sensor goes LOW, we will get the time and date from the RTC1307 real time clock, and write the information on the MicroSD card.



SCHEMATIC

Arduino Micro SD and Obstacle avoid sensor module

The MicroSD module uses the SPI Bus and is connected to the SPI pins on the UNO:

SPI: 10 (CS), 11 (MOSI), 12 (MISO), 13 (SCK).

 

The DS1307 real time clock uses the I2C Bus and is connected to the I2C pins of the UNO:

I2C: A4 (SDA) and A5 (SCL).

 

The IR Obstacle Avoidance sensor can be connected to any digital pin.

It’s connected to pin 2 in our tutorial.

 

We use a breadboard to connect the VCC and Ground to both modules from our UNO.



THE CODE

We are using a couple of libraries in this tutorial:

The DSRTC1307 Arduino Library for the DS1307 real time clock.

We also include the Wire.h and Time.h library for the I2C Bus and Time manipulation.

The Wire.h and the SD.h library for the MicroSD Module are included with the Arduino IDE software.

First thing we need to do is initialize our DS1307 module with the current date and time.

We do this by running the “SetTime” sketch that comes with the DSRTC1307 library.

After this, we will upload the main code that will write the Time and Date when motion is detected.

 

As always please watch our Tutorial video for more information.

#include "SD.h"  // SD card libray
#include "Wire.h"  // I2C
#include "Time.h"  // Time Manipulation
#include "DS1307RTC.h"  // DS1307 RTC

int irpin = 2;  // IR sensor pin
char timedatebuf[65];  // Time and Date string buffer
int year4digit;  // 4 digit year

void setup()
{
  Serial.begin(9600);  // Serial monitor used for testing
  pinMode (irpin, INPUT);
  
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
  
  if (!SD.begin(10)) {  // check if card is installed
    Serial.println("No SD Card present in module");
    return;
  }
  Serial.println("SD Card Ready");
}

void loop()
{
   tmElements_t tm;
   
   if (digitalRead (irpin) == LOW) // IR detected 
  {
    if (RTC.read(tm)) {  // Get Time/Date from RTC1307
     
      year4digit = tm.Year + 1970;  // 4 digit year variable
      
      // Format Time & Date string in timedatebuf
      sprintf(timedatebuf, "Time: %02d:%02d:%02d   Date:  %02d/%02d/%02d   ---->INTRUDER DETECTED!!!",tm.Hour, tm.Minute, tm.Second, tm.Day, tm.Month, year4digit);
      
      File dataFile = SD.open("intruder.txt", FILE_WRITE);  // Open or Create file 

      if (dataFile) {  // Check if file exist on SD Card
         dataFile.println(timedatebuf);
         dataFile.close();  // Close file
         Serial.println(timedatebuf);
      }  
      
      else {
        Serial.println("error opening intruder.txt"); // if file not on SD Card
      } 

    }

  }
  while (digitalRead (irpin) == LOW) {  // wait until IR is HIGH again
  }
  
  delay(100); // delay to give time for IR to reset
}



TUTORIAL VIDEO



DOWNLOAD

Copy and paste the above code in the Arduino IDE to program your Arduino.

 

Used Libraries:

Download the RTC1307 library here:  DS1307RTC.zip

Download the Time library here:  Time.zip

 

Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder.

1 Comments

Musi Mumu - Jan 31, 2017

Nice project and can be useful for me but as it it is can bot be used. i need to record 4 or 5 sensors. I know it is possible but I do not know haw to change the program. I am at beginning on arduino programming .
Please help if you can.
Regards

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

Leave a comment

Comments have to be approved before showing up