eeprom-contre.ino

Arduino Nano (Classic, ATmega328P) · File : eeprom-contre.ino · Language : Arduino · Use case : Compter les redémarrages dans l'EEPROM

The ATmega328P 1 KB EEPROM keeps data without power: this boot counter survives power cuts. The magic word avoids reading noise on first run — the pattern of any Nano persistence.

Hardware

An Arduino Nano classic (ATmega328P at 16 MHz, 30-pin DIL breadboard footprint, mini-B USB, 5 V logic, RESET pin at the lower-left corner). Bare Arduino Nano; the EEPROM is inside the chip, no external part.

Wiring

No wiring; to reset the counter, flash any sketch calling EEPROM.put(0, 0).

How it works

EEPROM.get/put read/write arbitrary types (here uint32_t over 4 bytes). Each put only writes bytes that changed, preserving cell lifetime.

Running it

In the Arduino IDE, select the Arduino Nano board and ATmega328P processor, pick the serial port, then upload. The board's 5 V rail suits 5 V sensors; otherwise use 3.3 V on compatible pins.

Variants

Store a circular buffer of readings for a fallback logger, or a setpoint table editable over serial without reflashing.

eeprom-contre.ino

// L'EEPROM 1 Ko du 328P survit a la coupure : compteur de demarrages.
// Attention : ecriture use la cellule (~100 000 cycles) : journalisez.
#include <EEPROM.h>

const int ADRESSE = 0;
const uint32_t MAGIC = 0xA5A51234; // verifie que l'EEPROM est initialisee

void setup() {
  Serial.begin(9600);
  uint32_t magic = 0;
  EEPROM.get(ADRESSE + 4, magic);
  uint32_t compteur;
  if (magic != MAGIC) {
    compteur = 0;
    EEPROM.put(ADRESSE + 4, MAGIC);
    Serial.println(F("EEPROM initialisee"));
  } else {
    EEPROM.get(ADRESSE, compteur);
  }
  compteur++;
  EEPROM.put(ADRESSE, compteur);
  Serial.print(F("Demarrage n°"));
  Serial.println(compteur);

  // Lecture de controle apres ecriture (bonne habitude AVR)
  uint32_t verif;
  EEPROM.get(ADRESSE, verif);
  Serial.println(verif == compteur ? F("verif OK") : F("verif KO"));
}

void loop() {}

← Back to the board