Build an Intruder Alert using a Micro SD Module

//Build an Intruder Alert using a Micro SD Module

Build an Arduino Intruder Alert using a Micro SD module and an IR Sensor


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.


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.


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");
  Serial.println("SD Card Ready");

void loop()
   tmElements_t tm;
   if (digitalRead (irpin) == LOW) // IR detected 
    if ( {  // 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 ="intruder.txt", FILE_WRITE);  // Open or Create file 

      if (dataFile) {  // Check if file exist on SD Card
         dataFile.close();  // Close file
      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

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

Used Libraries:

Download the RTC1307 library here:

Download the Time library here:

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

By | 2018-02-23T17:23:19-05:00 February 21st, 2015|Tutorials|


  1. jim July 26, 2020 at 7:59 pm - Reply

    i am having the same problem with tmElements_t not declared in this scope. i see some say to use #include but that says no such library can anyone help

  2. DAVID HOUSEAGO February 4, 2019 at 7:31 am - Reply

    After I compiled it said “‘tmElements_t’ was not declared in this scope”, do you have any solution?


  3. Denny Roozano February 2, 2019 at 12:31 pm - Reply

    After I compiled it said “‘tmElements_t’ was not declared in this scope”, do you have any solution?


    • DAVID HOUSEAGO February 4, 2019 at 11:26 am - Reply

      #include It worked for me

      • DAVID HOUSEAGO February 8, 2019 at 5:11 am - Reply


  4. santhosh nayan January 6, 2019 at 3:56 am - Reply

    can i use both fingerprint sensor and sdcard module at a time using mega
    will i get serial communication problem

  5. santhosh nayan January 6, 2019 at 3:53 am - Reply

    can i use finger print sensor insted of IR by using mega will this program work

Leave A Comment