This project is intended for implementing a low cost proximity/motion data logger, useful device that can be used on environmental / animal scientific research for school/university studies. However the goal of this project is to build an open source that can easily assembled that anyone can gathered data for research applications that requires high precision and accuracy which is capable to runs up to 8 ~ 12 months with 2500 mAh lithium ion polymer battery . We assumed that you are familiar with this components. This project are driven on 5 main components, Microcontroller, Proximity Sensor (PIR), Real Time Clock on chip, microSD Card, and microSD card socket for TransFlash.
The DS32354 RTC (Real Time Clock) on chip is an extremely accurate SPI bus clock with integrated-compensated crystal oscillator and crystal. This device has incorporates a precision, temperature-compensated voltage reference and comparator circuit.
The pyroelectric infrared (PIR) / Passive Infrared / IR Motion Sensor. This sensor allows you to sense motion always to detect whether animals/human that moves in or out of the sensor range. This device are small and inexpensive that runs on low-power, easy to use.
Required Components
- Arduino IDE | Atmel Studio
- Microcontroller – Arduino NANO, MINI, MEGA, UNO, LEO, DUE, ZERO, 101, MKR1000, FIO, NodeMCU, Teensy Board, TeensyDuino, ESP8266 12, 12E, ESP32, LinkItOne, ESP8266 NodeMCU, ESPDuino, ATMEGA328 16/12, ATMEGA32u4 16/8/ MHz, ESP8266, ATMEGA250 16 MHz, ATSAM3x8E, ATSAM21D, ATTINY85 16/8 MHz Note: The Diagram below is using NANO. (please refer to each MCU’s respective pin-outs)
- RTC Chip or Module
- PIR / Motion Sensor / Proximity Sensor
- microSD Card (4~8 GB FAT)
- microSD Card Socket / microSD Module
- Capacitors (See below required values)
- Resistors (See below required values)
- Solder Less Breadboard (Optional)
- Jumper Wire
- Prototyping Board
- PCB board (for testing)
Wiring Guide
Wiring with ATMEGA328P
Using 328P chip more convenient you can just program it with your FTDI/USB UART or Arduino SPI and place into the DIP Sockets Solder Tail mounted in the PCB board.
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
#include <SdFat.h> #include <EEPROM.h> #include <PowerSaver.h> #include <DS3234lib3.h> char filename[15] = "corelog.csv"; //Cannot be more than 8 characters in length PowerSaver chip; //Set declare object for PowerSaver class DS3234 myTime; //Set declare object for DS3234 class SdFat sd; //Set declare object for SdFat class SdFile file; //Set declare object for SdFile class #define POWER 4 // pin 4 supplies power to microSD card breakout #define myTimePOWER D // pin 6 supplies power to DS3234 myTime breakout #define statusLED 3 //Set pin 3 for LED status int microSDCSPin = 10; // pin 9 is micrSD card breakout CS pin int state = 0; // this variable stores the state of PIR sensor's output (either 1 or 0) ISR(PCINT0_vect) // Interrupt Service Routine for PCINT0 vector (pin 8) { asm("nop"); //set to-do nothing } /*SETUP*/ void setup() { Serial.begin(19200); // Start open serial at 19200 bps boud rate pinMode(POWER, OUTPUT); // set output pins pinMode(myTimePOWER, OUTPUT); pinMode(statusLED, OUTPUT); digitalWrite(POWER, HIGH); //Set to turn on SD card digitalWrite(myTimePOWER, HIGH); //Set turn on myTime delay(1); //give some delay to ensure SD card is turned on properly if(!sd.init(microSDCSPin, SPI_FULL_SPEED)) //Start initialize SD card on the SPI bus { delay(10); SDcardError(); Serial.print("SD Card Error"); } else { delay(10); file.open(filename, O_CREAT | O_APPEND | O_WRITE); // open file in write mode and append data to the end of file delay(1); String time = myTime.timeStamp(); // get date and time from myTime file.println(); file.print("Date/Time,Occupancy"); // Print header to file file.println(); PrintFileTimeStamp(); file.close(); // close file - very important // give some delay by blinking status LED to wait for the file to properly close digitalWrite(statusLED, HIGH); delay(10); digitalWrite(statusLED, LOW); } chip.sleepInterruptSetup(); //Set to setup sleep function & pin change interrupts } void loop() { digitalWrite(POWER, LOW); // turn off SD card digitalWrite(myTimePOWER, LOW); // turn off myTime delay(1); chip.turnOffADC(); // turn off ADC to save power chip.turnOffSPI(); // turn off SPI bus to save power //chip.turnOffWDT(); // turn off WatchDog timer. This doesn't work for Pro Mini (Rev 11); only works for Arduino Uno chip.turnOffBOD(); chip.goodNight(); //Set to power-down mode chip.turnOnADC(); //Set tp turn ADC once wakes up chip.turnOnSPI(); //Set turn on SPI bus once wakes up delay(1); digitalWrite(POWER, HIGH); //Set turn on SD card digitalWrite(myTimePOWER, HIGH); //Set turn on myTime delay(10); myTime.checkDST(); //Set check and account for Daylight Savings Time printToSD(); // print data to SD card } /*Write to SD function*/ void printToSD() { pinMode(microSDCSPin, OUTPUT); if(!sd.begin(microSDCSPin, SPI_FULL_SPEED)) // very important - reinitialize SD card on the SPI bus { delay(10); SDcardError(); Serial.print("SD Card Error"); } else { delay(10); file.open(filename, O_WRITE | O_AT_END); // open file in write mode delay(1); String settime = myTime.timeStamp(); // get date and time from myTime SPCR = 0; // reset SPI control register state = digitalRead(2); // read current state PIR reads 1 = movement detected, 0 = no movement detected. if(state == 1) {file.print(settime);file.print(",");file.print("0");file.println();file.print(settime);file.print(",");file.print("1");} else if(state == 0) {file.print(settime);file.print(",");file.print("1");file.println();file.print(settime);file.print(",");file.print("0");} file.println(); PrintFileTimeStamp(); file.close(); // Set to close file digitalWrite(statusLED, HIGH); delay(10); digitalWrite(statusLED, LOW); } } /*Write time stamp function*/ void PrintFileTimeStamp() // Print timestamps to data file. Format: year, month, day, hour, min, sec // file timestamps { file.timestamp(T_WRITE, myTime.year, myTime.month, myTime.day, myTime.hour, myTime.minute, myTime.second); // date modified file.timestamp(T_ACCESS, myTime.year, myTime.month, myTime.day, myTime.hour, myTime.minute, myTime.second); // date accessed } // microSD Card Error Handler void SDcardError() { for(int i=0;i<3;i++) // Turn LED blink 3 Times indicates SD card read/write error { digitalWrite(statusLED, HIGH); //Error LED Indicator delay(50); digitalWrite(statusLED, LOW); //Error LED Indicator delay(150); } } |
Downloads
- Download PowerSaver Code Library
- Download SdFat Code Library
- Download DS3234 Code Library
- Download DS3234 Datasheet
- Download PIR Module Datasheet User Manual