eeprom-log.ino

LilyPad Arduino · File : eeprom-log.ino · Language : Arduino · Use case : Conserver des relevés dans l'EEPROM après une coupure de courant

The microcontroller EEPROM keeps data without power (100,000 write cycles minimum). EEPROM.put only writes changed bytes, sparing the memory; the resume index is re-read at boot.

Hardware required

A LilyPad Arduino (ATmega168V/328V, 8 MHz, 3.3 V) with its sew tabs, programmed via an FTDI cable (breakout) and powered by 3.7 V (LiPo) or 5 V on V-REG. None (any voltage source on A0 for testing: potentiometer or LDR).

Wiring

Nothing special; wire A0 to a divider or analog sensor.

How it works

Each record is 16 theoretical bytes (2 used here); the header at address 0 stores the sample count so logging resumes after a reboot. In the Arduino IDE, select « LilyPad Arduino » and the FTDI port, then Upload. Conductive thread adds a few ohms of resistance: keep runs short and insulate crossings.

Going further

Tag the entries (count, date) and dump the journal over the serial link on demand.

eeprom-log.ino

// Journalise une valeur analogique dans l'EEPROM (4 Ko ou 1 Ko).
#include <EEPROM.h>

const int ENTREE = 16;      // taille d'un enregistrement (octets)
const int MAX = EEPROM.length() / ENTREE;
int compteur = 0;

void setup() {
  Serial.begin(9600);
  // Relit le dernier indice ecrit (0xFFFF = vierge)
  EEPROM.get(0, compteur);
  if (compteur == 0xFFFF || compteur > MAX) compteur = 0;
  Serial.print(F("Reprise a l'entree "));
  Serial.println(compteur);
}

void loop() {
  int valeur = analogRead(A0);
  int idx = compteur % MAX;
  EEPROM.put(2 + idx * ENTREE, valeur); // 2 octets par entree
  EEPROM.put(0, ++compteur);            // memoire du rang
  Serial.print(idx); Serial.print(F(" -> ")); Serial.println(valeur);
  delay(2000);
}

← Back to the board