Data logging shield

[ [ images & links ] ]

https://learn.adafruit.com/adafruit-data-logger-shield?view=all :

Gambar 1. learn.adafruit.com

 

adafruit_products_labeled.jpgGambar 2.

 

Place the SD library folder your sketchbook libraries folder. You may need to create the libraries subfolder if its your first library.

 

 

https://learn.adafruit.com/adafruit-data-logger-shield?view=all :

adafruit_products_Logger_bb.jpgGambar 3.

Adafruit library examples.

Deek-Robot: Data logging shield V1.0

D10 – Chip Select
D11 – SPI MOSI
D12 – SPI MISO
D13 – SPI SCK

[code lang=”C”] RTC_DS1307 RTC; // define the Real Time Clock object

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

void error(char *str)
{
Serial.print("error: ");
Serial.println(str);

// red LED indicates error
digitalWrite(redLEDpin, HIGH);

while(1);
}
[/code]

“Next is the error() function, which is just a shortcut for us, we use it when something Really Bad happened, like we couldn’t write to the SD card or open it. It prints out the error to the Serial Monitor, turns on the red error LED, and then sits in a while(1); loop forever, also known as a halt

[code lang=”C”] // initialize the SD card
Serial.print("Initializing SD card…");
// make sure that the default chip select pin is set to
// output, even if you don’t use it:
pinMode(10, OUTPUT);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don’t do anything more:
return;
}
Serial.println("card initialized.");

// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + ‘0’;
filename[7] = i%10 + ‘0’;
if (! SD.exists(filename)) {
// only open a new file if it doesn’t exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}

if (! logfile) {
error("couldnt create file");
}

Serial.print("Logging to: ");
Serial.println(filename);
[/code]

“Now the code starts to talk to the SD card, it tries to initialize the card and find a FAT16/FAT32 partition. Next it will try to make a logfile. We do a little tricky thing here, we basically want the files to be called something like LOGGERnn.csv where nn is a number. By starting out trying to create LOGGER00.CSV and incrementing every time when the file already exists, until we get to LOGGER99.csv, we basically make a new file every time the Arduino starts up To create a file, we use some Unix style command flags which you can see in the logfile.open() procedure. FILE_WRITE means to create the file and write data to it. Assuming we managed to create a file successfully, we print out the name to the Serial port.”

[code lang=”C”] Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}

logfile.println("millis,time,light,temp");
#if ECHO_TO_SERIAL
Serial.println("millis,time,light,temp");
#if ECHO_TO_SERIAL// attempt to write out the header to the file
if (logfile.writeError || !logfile.sync()) {
error("write header");
}

pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);

// If you want to set the aref to something other than 5v
//analogReference(EXTERNAL);
}
[/code]

“OK we’re wrapping up here. Now we kick off the RTC by initializing the Wire library and poking the RTC to see if its alive. Then we print the header. The header is the first line of the file and helps your spreadsheet or math program identify whats coming up next. The data is in CSV (comma separated value) format so the header is too: “millis,time,light,temp” the first item millis is milliseconds since the Arduino started, time is the time and date from the RTC, light is the data from the CdS cell and temp is the temperature read. You’ll notice that right after each call to logfile.print() we have #if ECHO_TO_SERIAL and a matching Serial.print() call followed by a #if ECHO_TO_SERIAL this is that debugging output we mentioned earlier. The logfile.print() call is what writes data to our file on the SD card, it works pretty much the same as the Serial version. If you set ECHO_TO_SERIAL to be 0 up top, you won’t see the written data printed to the Serial terminal. Finally, we set the two LED pins to be outputs so we can use them to communicate with the user. There is a commented-out line where we set the analog reference voltage. This code assumes that you will be using the ‘default’ reference which is the VCC voltage for the chip – on a classic Arduino this is 5.0V. You can get better precision sometimes by lowering the reference. However we’re going to keep this simple for now! Later on, you may want to experiment with it.”

Main loop

Now we’re onto the loop, the loop basically does the following over and over:

  • Wait until its time for the next reading (say once a second – depends on what we defined)
  • Ask for the current time and date froom the RTC
  • Log the time and date to the SD card
  • Read the photocell and temperature sensor
  • Log those readings to the SD card
  • Sync data to the card if its time
[code lang=”C”] void loop(void)
{
DateTime now;

// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) – (millis() % LOG_INTERVAL));

digitalWrite(greenLEDpin, HIGH);

// log milliseconds since starting
uint32_t m = millis();
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif

// fetch the time
now = RTC.now();
// log time
logfile.print(now.get()); // seconds since 2000
logfile.print(", ");
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
#if ECHO_TO_SERIAL
Serial.print(now.get()); // seconds since 2000
Serial.print(", ");
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL
[/code]

  “The first important thing is the delay() call, this is what makes the Arduino wait around until its time to take another reading. If you recall we #defined the delay between readings to be 1000 millseconds (1 second). By having more delay between readings we can use less power and not fill the card as fast. Its basically a tradeoff how often you want to read data but for basic long term logging, taking data every second or so will result in plenty of data! Then we turn the green LED on, this is useful to tell us that yes we’re reading/writing data now. Next we call millis() to get the ‘time since arduino turned on’ and log that to the card. It can be handy to have – especially if you end up not using the RTC. Then the familiar RTC.now() call to get a snapshot of the time. Once we have that, we write a timestamp (seconods since 2000) as well as the date in YY/MM/DD HH:MM:SS time format which can easily be recognized by a spreadsheet. We have both because the nice thing about a timestamp is that its going to montonically increase and the nice thing about printed out date is its human readable.”

https://learn.adafruit.com/adafruit-data-logger-shield?view=all :

Plotting with a spreadsheet

 

adafruit_products_dataselect.gifGambar 4.

 

Gambar 5.

https://learn.adafruit.com/adafruit-data-logger-shield?view=all :

Using Gnuplot

set xlabel "Time"              # set the lower X-axis label to 'time'

set xtics rotate by -270       # have the time-marks on their side


set ylabel "Light level (qualitative)"    # set the left Y-axis label

set ytics nomirror             # tics only on left side

set y2label "Temperature in Fahrenheit"   # set the right Y-axis label
set y2tics border              # put tics no right side

set key box top left           # legend box
set key box linestyle 0 

set xdata time                 # the x-axis is time
set format x "%H:%M:%S"        # display as time
set timefmt "%s"               # but read in as 'unix timestamp'

plot "LOGTEST.CSV" using 2:4 with lines title "Light levels" 
replot "LOGTEST.CSV" using 2:5 axes x1y2 with lines title "Temperature (F)"

adafruit_products_gnuplotcmd.gifGambar 6.

adafruit_products_gnuplotted.gifGambar 7.

https://learn.adafruit.com/adafruit-data-logger-shield?view=all :

adafruit_products_adafruitlogshieldsch.pngGambar 8.

Gambar 9.

Gambar 10.

Gambar 11.

Gambar 12.

 

Save

Save

Save

Save

RTC DS1302

[ [ images & links ] ]

Gambar 1. [sumber]

Sebelumnya, kutipan informasi dari dua RTC telah pernah diungkap di situs ini. Pertama DS1307, dan yang kedua adalah DS3231. Di halaman ini akan saya kutip keterangan mengenai RTC DS1302, yang bersama dua RTC terdahulu saya beli di situs online lokal.

 

The DS1302 trickle-charge timekeeping chip contains a real-time clock/calendar and 31 bytes of static RAM. It communicates with a microprocessor via a simple serial interface. The real-time clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM indicator.

Interfacing the DS1302 with a microprocessor is simplified by using synchronous serial communication. Only three wires are required to communicate with the clock/RAM: CE, I/O (data line), and SCLK (serial clock). Data can be transferred to and from the clock/RAM 1 byte at a time or in a burst of up to 31 bytes. The DS1302 is designed to operate on very low power and retain data and clock information on less than 1µW.

The DS1302 is the successor to the DS1202. In addition to the basic timekeeping functions of the DS1202, the DS1302 has the additional features of dual power pins for primary and backup power supplies, programmable trickle charger for VCC1, and seven additional bytes of scratchpad memory.

~maximintegrated.com

 

Gambar 2. domoticx.com/arduino-rtc-tijdklok-ds1302/

#include <DS1302.h>

// Init the DS1302
// DS1302 rtc([CE/RST], [I/O], [CLOCK]);
DS1302 rtc(8, 7, 6);

void setup() {
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  
  // Setup Serial connection
  Serial.begin(9600);

  // The following lines can be commented out to use the values already stored in the DS1302
  rtc.setDOW(FRIDAY);        // Set Day-of-Week to FRIDAY
  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(6, 8, 2010);   // Set the date to August 6th, 2010
}

void loop() {
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  delay (1000);
}

Pustaka yang diperlukan untuk mengeksekusi kode bisa diakses di link ini.

Berdasarkan pengalaman saya menguji coba sistem ini dan pengalaman cukup banyak orang lain yang saya pantau di berbagai halaman di Internet, tampaknya sistem RTC DS1302 yang banyak beredar memiliki permasalahan dalam hal operasi. Banyak pengguna yang melaporkan kesalahan pembacaan jika mempergunakan IC ini. Ada beberapa kemungkinan, termasuk kemungkinan IC yang banyak diperjual-belikan yang memang memiliki grade yang lebih rendah.

Secara praktis, sampai ditemukan solusi yang baik untuk IC ini (versi yang banyak dijual), akan lebih baik mempergunakan RTC DS1307 atau DS3231.

RTC DS3231

[ [ images, codes & links ] ]

Keterangan mengenai RTC (Real-Time Clock) sudah pernah disampaikan melalui kutipan di post terdahulu. Di halaman itu yang dibahas adalah komponen RTC DS1307, yang lazim diperdagangkan dalam bentuk papan yang ditandai sebagai Tiny RTC. Kali ini saya coba rangkumkan mengenai DS3231 dari berbagai sumber sebagai awalan untuk memperlajarinya. RTC ini dilaporkan memiliki akurasi yang lebih baik dari RTC DS1307.

Gambar 1.

 

With a backup button cell (e.g. CR2032) on the underside of the module, these DS1307 modules will keep time even when disconnected from the main power source for months and even years on end. However, in our experimental projects (using this RTC with an Arduino for dataloggers amongst other things), we have found these DS1307 modules to vary hugely in their time-keeping accuracy – some gaining/losing a few seconds per day, and others gaining/losing as much as 3-5 minutes per day. While they have proved to be very consistent – i.e. a unit which gains 3 minutes per day will gain 3 minutes per day every day – having to test each unit individually over a few days and then modifying the Arduino project code to cancel out errors is not practical.

Some of the error is caused by ambient temperature changes affecting the accuracy of the timing of the crystal resonator. Some more of the error is also caused by the quality of the crystal itself and its attachment to the PCB in these economical modules.

In extensive testing we have found the time-keeping of these modules to be excellent. The DS3231 chip on the module is marketed as being accurate to 2ppm (parts per million), which means less than one second lost or gained every 5 to 6 days. The units we have tested thus far have all come in at under 1ppm accuracy, so a couple of seconds at most lost or gained per month.

This accuracy is achieved in part by the incorporation of a temperature sensor in the DS3231 which can compensate for changes in ambient temperature. The measurements from this temperature sensor are also accessible to the user (accurate to +/- 3 Celcius) which makes for a handy extra feature. These DS3231 modules also have 32kb of available EEPROM memory which can be utilised by your projects, and many other useful features.

~reuk.co.uk

 

sumber: Benchmarks: Real Time Clocks – Results for Raspberry Pi/Arduino – DS3231 / PCF8563 / MCP79400 / DS1307

 

Gambar 2. [randomnerdtutorials.com]

 

Terdapat beberapa pustaka/library untuk tiap RTC. Dua yang disampaikan dalam “mashup ini adalah pustaka dari Adafruit dan pustaka dari Makuna.

Contoh-contoh kode program sistem Arduino untuk RTC DS3231.

Sistem ini dirancang untuk bekerja dengan menggunakan baterai “khusus”, yaitu baterai yang bisa diisi ulang (rechargeable) seperti LIR2032. Di papan sudah disediakan sistem pengisian ulang untuk baterai tersebut. Karena itu jika kita mempergunakan baterai tipe yang tidak bisa diisi ulang (non-rechargeable) seperti CR2032 maka ada perubahan yang perlu dilakukan. Hal ini untuk mencegah agar baterai primer yang tidak bisa diisi ulang itu tidak berusaha diisi oleh sistem. Caranya adalah dengan melepas/membuang diode seperti pada Gambar 3 berikut.

ds.jpgGambar 3. [sumber: tronixlabs.com]

 

[intense_tabs direction=”right” active_tab_background_color=”#000000″ active_tab_font_color=”#ffff00″ trigger=”click”] [intense_tab title=”Video01″ border=”3px solid #e8e8e8″ link_target=”_self” content_background_color=”#000000″ content_font_color=”#ffffff” icon_size=”1″ icon_position=”left”]

[/intense_tab] [intense_tab title=”Video02″ border=”3px solid #e8e8e8″ link_target=”_self” icon_size=”1″ content_background_color=”#000000″ content_font_color=”#ffffff” icon_position=”left”]

[/intense_tab] [intense_tab title=”Video03″ border=”3px solid #e8e8e8″ link_target=”_self” icon_size=”1″ content_background_color=”#000000″ content_font_color=”#ffffff” icon_position=”left”]

[/intense_tab] [intense_tab title=”Video04″ border=”3px solid #e8e8e8″ link_target=”_self” icon_size=”1″ content_background_color=”#000000″ content_font_color=”#ffffff” icon_position=”left”]

[/intense_tab] [intense_tab title=”Video05″ border=”3px solid #e8e8e8″ link_target=”_self” icon_size=”1″ content_background_color=”#000000″ content_font_color=”#ffffff” icon_position=”left”]

[/intense_tab] [/intense_tabs]

 

Save

RTC DS1307

[ [ images, codes & links ] ]

Gambar 1.

A real-time clock (RTC) is a computer clock (most often in the form of an integrated circuit) that keeps track of the current time.

Although the term often refers to the devices in personal computers, servers and embedded systems, RTCs are present in almost any electronic device which needs to keep accurate time.

<span class="su-quote-cite"><a href="https://en.wikipedia.org/wiki/Real-time_clock" target="_blank">Real-time clock</a></span>

What is an RTC?

A real time clock is basically just like a watch – it runs on a battery and keeps time for you even when there is a power outage! Using an RTC, you can keep track of long timelines, even if you reprogram your microcontroller or disconnect it from USB or a power plug.

Most microcontrollers, including the Arduino, have a built-in timekeeper called millis() and there are also timers built into the chip that can keep track of longer time periods like minutes or days. So why would you want to have a separate RTC chip? Well, the biggest reason is that millis() only keeps track of time since the Arduino was last powered – . That means that when the power is turned on, the millisecond timer is set back to 0. The Arduino doesn’t know that it’s ‘Tuesday’ or ‘March 8th’, all it can tell is ‘It’s been 14,000 milliseconds since I was last turned on’.

OK so what if you wanted to set the time on the Arduino? You’d have to program in the date and time and you could have it count from that point on. But if it lost power, you’d have to reset the time. Much like very cheap alarm clocks: every time they lose power they blink 12:00

While this sort of basic timekeeping is OK for some projects, some projects such as data-loggers, clocks, etc will need to have consistent timekeeping that doesn’t reset when the Arduino battery dies or is reprogrammed. Thus, we include a separate RTC! The RTC chip is a specialized chip that just keeps track of time. It can count leap-years and knows how many days are in a month, but it doesn’t take care of Daylight Savings Time (because it changes from place to place).

~DS1307 Real Time Clock Breakout Board Kit

 

What are Real Time Clocks?

Real time clocks (RTC), as the name recommends are clock modules. The DS1307 real time clock (RTC) IC is an 8 pin device using an I2C interface. The DS1307 is a low-power clock/calendar with 56 bytes of battery backup SRAM. The clock/calendar provides seconds, minutes, hours, day, date, month and year qualified data. The end date of each month is automatically adjusted, especially for months with less than 31 days.

They are available as integrated circuits (ICs) and supervise timing like a clock and also operate date like a calendar. The main advantage of RTC is that they have an arrangement of battery backup which keeps the clock/calendar running even if there is power failure. An exceptionally little current is required for keeping the RTC animated. We can find these RTCs in many applications like embedded systems and computer mother boards, etc.

~www.elprocus.com/rtc-ds1307/

 

 

Arduino Tiny RTC D1307 Tutorial

 

Gambar 2. [henrysbench.capnfatz.com]

Gambar 3. [henrysbench.capnfatz.com]

Gambar 4. [tronixstuff.com]

Connecting your module to an Arduino

Both modules use the I2C bus, which makes connection very easy. If you’re not sure about the I2C bus and Arduino, check out the I2C tutorials (chapters 20 and 21), or review chapter seventeen of my book “Arduino Workshop“.

Moving on – first you will need to identify which pins on your Arduino or compatible boards are used for the I2C bus – these will be knows as SDA (or data) and SCL (or clock).

~ tronixstuff.com

 

 

Tiny RTC

Gambar 5.

Features

  • 5V DC supply
  • Programmable Square-Wave output signal
  • Automatic Power-Fail detect and switch circuitry
  • Consumes less than 500nA in Battery-Backup Mode with Oscillator Running
  • 56-Byte, Battery-Backed, Nonvolatile (NV)RAM for data storage

Gambar 6.

TinyRTC hardware1.jpgGambar 7.

~www.elecrow.com

 

 

RTC DS-1307 with Arduino

DS 1307 RTC Pinout

Gambar 8.

Block Diagram of DS1307

Gambar 9. [Datasheet]

Gambar 10.

~www.theorycircuit.com

 

Terdapat beberapa pustaka/library untuk tiap RTC. Dua yang disampaikan dalam “mashup ini adalah pustaka dari Adafruit dan pustaka dari Makuna.

Contoh-contoh kode program sistem Arduino untuk RTC DS1307.

adafruit: ds1307.ino

[code lang=”C”]// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

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

void setup ()
{
while (!Serial); // for Leonardo/Micro/Zero

// Serial.begin(57600);
Serial.begin(9600);

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

if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date &amp; time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date &amp; time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}

void loop ()
{
DateTime now = rtc.now();

Serial.print(now.year(), DEC);
Serial.print(‘/’);
Serial.print(now.month(), DEC);
Serial.print(‘/’);
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(‘:’);
Serial.print(now.minute(), DEC);
Serial.print(‘:’);
Serial.print(now.second(), DEC);
Serial.println();

Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");

// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));

Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print(‘/’);
Serial.print(future.month(), DEC);
Serial.print(‘/’);
Serial.print(future.day(), DEC);
Serial.print(‘ ‘);
Serial.print(future.hour(), DEC);
Serial.print(‘:’);
Serial.print(future.minute(), DEC);
Serial.print(‘:’);
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();
delay(3000);
}[/code]

[intense_tabs direction=”right” active_tab_background_color=”#000000″ active_tab_font_color=”#ffff00″ trigger=”click”] [intense_tab title=”Video01″ border=”3px solid #e8e8e8″ link_target=”_self” content_background_color=”#000000″ content_font_color=”#ffffff” icon_size=”1″ icon_position=”left”]

[/intense_tab] [intense_tab title=”Video02″ border=”3px solid #e8e8e8″ link_target=”_self” icon_size=”1″ content_background_color=”#000000″ content_font_color=”#ffffff” icon_position=”left”]

[/intense_tab] [intense_tab title=”Video03″ border=”3px solid #e8e8e8″ link_target=”_self” icon_size=”1″ content_background_color=”#000000″ content_font_color=”#ffffff” icon_position=”left”]

[/intense_tab] [intense_tab title=”Video04″ border=”3px solid #e8e8e8″ link_target=”_self” icon_size=”1″ content_background_color=”#000000″ content_font_color=”#ffffff” icon_position=”left”]

[/intense_tab] [/intense_tabs]