What if your Arduino could remember things forever, even when turned off?
Why EEPROM read and write in Arduino? - Purpose & Use Cases
Imagine you want your Arduino to remember a setting, like a high score in a game, even after it loses power. Without EEPROM, you'd have to enter the score again every time you turn it on.
Manually saving data without EEPROM means you lose it when power goes off. Trying to keep data in variables only works while the device is on. Writing to external storage manually is slow and complicated.
EEPROM lets your Arduino save small pieces of data permanently inside its memory. You can easily write and read values, so your device remembers important info even after power loss.
int score = 0; // loses value when powered off#include <EEPROM.h> int score = 0; EEPROM.write(0, score); // saves score permanently score = EEPROM.read(0); // reads saved score
EEPROM read and write lets your Arduino keep important data safe across power cycles, making your projects smarter and more reliable.
A thermostat saving the last temperature setting so it can start with the same setting after a power outage.
EEPROM stores data permanently on Arduino.
It keeps data even when power is off.
Reading and writing EEPROM is simple and useful for many projects.
Practice
EEPROM.write(address, value) do in Arduino?Solution
Step 1: Understand EEPROM.write function
The functionEEPROM.write(address, value)saves a byte value at the given address in EEPROM memory.Step 2: Differentiate from EEPROM.read
EEPROM.read(address)is used to read data, not write it.Final Answer:
Stores a byte value at the specified EEPROM address -> Option AQuick Check:
EEPROM.write saves data [OK]
- Confusing write with read function
- Thinking EEPROM.write clears memory
- Assuming EEPROM.write initializes EEPROM
Solution
Step 1: Identify correct EEPROM read syntax
The correct function to read a byte from EEPROM isEEPROM.read(address).Step 2: Check options for exact match
OnlyEEPROM.read(10);matches the correct syntax.Final Answer:
EEPROM.read(10); -> Option DQuick Check:
EEPROM.read(address) reads byte [OK]
- Using EEPROM.write to read data
- Using non-existent EEPROM.readByte function
- Confusing EEPROM.get with EEPROM.read
EEPROM.write(5, 42); byte val = EEPROM.read(5); Serial.println(val);
Solution
Step 1: Write value 42 at EEPROM address 5
The code stores the byte 42 at address 5 usingEEPROM.write(5, 42);.Step 2: Read the value back from address 5
EEPROM.read(5)reads the stored value, which is 42.Final Answer:
42 -> Option BQuick Check:
Write 42, read 42 [OK]
- Assuming EEPROM.read returns address instead of value
- Forgetting to write before reading
- Expecting default 0 without writing
EEPROM.write(1024, 100); byte val = EEPROM.read(1024); Serial.println(val);
Solution
Step 1: Check EEPROM address range
Typical Arduino EEPROM size is 1024 bytes, so address 1024 is out of range (valid addresses are 0-1023).Step 2: Validate other code parts
EEPROM.write has correct arguments, EEPROM.read returns byte, and Serial.println can print bytes.Final Answer:
Address 1024 is out of EEPROM range -> Option CQuick Check:
EEPROM address must be within 0-1023 [OK]
- Using address equal to EEPROM size
- Assuming EEPROM size is larger than actual
- Misunderstanding Serial.println capabilities
Solution
Step 1: Understand data size and EEPROM functions
High score is an integer (2 bytes), soEEPROM.write(1 byte) is insufficient to store full value.Step 2: Use EEPROM.put and EEPROM.get for multi-byte data
EEPROM.put(address, highScore);saves the entire integer, andEEPROM.get(address, highScore);reads it back correctly.Final Answer:
Use EEPROM.put(address, highScore); and EEPROM.get(address, highScore); -> Option AQuick Check:
Use put/get for multi-byte data [OK]
- Using EEPROM.write for multi-byte integers
- Not reading back with EEPROM.get
- Mixing read and write roles
Ready to prove it?
Pass the test and this pattern moves unknown
