Validate a small stored payload with a checksum.

Arduino / C: Store a checksum with the payload and reject a changed byte.

C++ snippet.ino
#include <EEPROM.h>
void setup() {
  Serial.begin(9600);
  byte data[]={10,20,30};byte checksum=0;
  for(byte i=0;i<3;i++){EEPROM.update(i,data[i]);checksum^=data[i];}
  EEPROM.update(3,checksum);
  EEPROM.update(1,21);
  byte observed=0;for(byte i=0;i<3;i++)observed^=EEPROM.read(i);
  Serial.println(observed==EEPROM.read(3) ? "valid":"corrupt");
}

void loop() {
}

Expected output EXAMPLE

corrupt

Details

Targets Arduino Uno R3 (ATmega328P, 16 MHz) with the Arduino AVR core. Open as a sketch in the Arduino IDE and select Arduino Uno. Serial output uses 9600 baud. Tested with simavr; electrical characteristics and physical wiring are outside this simulation. Store a checksum with the payload and reject a changed byte.

Keep in mind

This simple checksum detects the demonstrated corruption; it is not a robust transactional storage scheme.

✓ Verified

Tested on Ubuntu 24.04.5 LTS · · Uno R3 / ATmega328P at 16 MHz; simavr 1.6+dfsg-3build2 · bash 5.2.21-2ubuntu4; coreutils 9.4-3ubuntu6.3; arduino-cli Version: 1.5.1 Commit: 01f3d4f2b Date: 2026-06-05T10:22:17Z; Arduino AVR core 1.8.6

Test inputs, results and environment

The exact published code ran in a dedicated Linux test container with prepared inputs. Snippet process uid 65534 with a read-only root filesystem and temporary working files. Networking disabled. The exact sketch was compiled for Uno R3 and simulated at 16 MHz. UART output is shown below; compiler logs and measured GPIO traces are retained in the test evidence. Fixtures and output records are isolated from the application database. Testing covers this example and its stated checks, not every input, operating system, or runtime version. The Linux container shares its host's kernel; this is not a native Windows test.

Architecture / host kernel
x86_64 / 6.18.54.1-microsoft-standard-WSL2
Snippet identity / locale
uid=65534, gid=65534 (unprivileged process) / C.UTF-8
Interpreter invocation
arduino-cli compile --fqbn arduino:avr:uno; uno-sim sketch.ino.elf (capture UART and D13 transitions)
Container image ID
sha256:ec91410a8f6ec3e985830b5e3a0560306d36f8c1c5ccd91a6699abe707123fa3
Code SHA-256
032d102a249c3a8b2b13462fb1dca7745326299d9a48e57c4c2d6d197be6152a
Dependencies
bash 5.2.21-2ubuntu4; coreutils 9.4-3ubuntu6.3; arduino-cli Version: 1.5.1 Commit: 01f3d4f2b Date: 2026-06-05T10:22:17Z; Arduino AVR core 1.8.6

Test input setup

These are the prepared inputs used by the test harness. See Details above for the prerequisites and how to run this example.

Simulator inputs: {"sim_ms": 500, "serial_input": "", "pin_input": "none"}

Observed standard output

corrupt

Exit code: 0. Standard error: empty. Validation: Exact stdout comparison. System-dependent values above are one recorded run.