Lessons and the learning game

Data logging: SD, EEPROM and flash

Keep data through resets and power cuts on an Uno: EEPROM wear, a 24LC256, SD cards and CSV logs with timestamps, and a balloon flight recorder.

Take the course at /learn/path/data-logging-and-storage. It is an intermediate course: it assumes the Uno course.

The lessons

EEPROM. A boot counter and settings in the Uno's 1024-byte EEPROM that come back after a reset, put() and get(), and the sums for a cell rated at 100,000 writes. Then a 24LC256: page writes, ACK polling through the write cycle, and a page size bug that wraps a write around its page.

Files on an SD card. Mounting a FAT16 card, listing it, and the 8.3 name rule. CSV rows with a header, read back on demand, and a semicolon for European spreadsheets. Rows stamped by a DS3231: Unix time, a year that rolls over by itself, and ISO 8601.

Surviving power loss. A file kept open and flushed every ten rows: cut the power and count what reached the card, then flush every row and lose none.

Boss. A weather balloon flight recorder: a file per day named from the RTC's date, timestamp, temperature and altitude rows closed after every write, the flight number in EEPROM, and a resume after a power cut.

The benches

The EEPROM lessons open the gallery's clock and memory board: a 24LC256 at 0x50 and a DS1307 at 0x68 on A4 and A5. The other lessons open the temperature data logger: a microSD card on CS 10, MOSI 11, MISO 12 and SCK 13, a DS18B20 on pin 2, a DS3231 at 0x68 and a LED on pin 7. The card starts with one file, README.TXT.

What each sketch does

  • dlogeeprom: a struct of settings and a boot count at address 0, b and t commands to change them, and the wear sums for SAVE_EVERY_MIN.
  • dlog24lc: an I2C scan, 100 bytes written from address 40 in chunks split at PAGE_SIZE, ACK polling, and a read-back check.
  • dlogsd: mounts the card, lists the root, and creates FILE_NAME.
  • dlogcsv: LOG.CSV with a SEP separator, a row a second, d to dump it.
  • dlogrtc: STAMPED.CSV with RTClib timestamps and Unix time.
  • dlogsafe: SAFE.CSV kept open and flushed every FLUSH_EVERY rows; x stops the sketch dead, as a power cut would, and RESET counts the rows.
  • dlogboss: daily YYYYMMDD.CSV files, LOG_SECONDS between rows with a SPEEDUP for the demo, a flight number in EEPROM, and a resume.

Honest limits

  • The EEPROM and the SD card keep their contents through resets within one Run, and start fresh at the next Run. Real ones keep them for years.
  • The SD library makes FAT16 short names only: no FAT32, no long names.
  • Pressing RESET in the middle of a block write leaves the simulated card mid-transaction, as a real card would be until its power is cycled. The power-loss lesson uses x to stop the sketch between writes instead.
  • The DS3231 reports lost power at the start, as a fresh module does until rtc.adjust() sets it.

Clocks, RTCs and time for time zones and RTC alarms, and Low power products for sleeping between log rows.