EEPROM lets your Arduino save data even when it is turned off. You can read and write small pieces of information that stay saved.
EEPROM read and write in Arduino
Introduction
Syntax
Arduino
EEPROM.write(address, value); value = EEPROM.read(address);
address is the location in EEPROM memory (0 to 1023 on many boards).
value is a byte (0-255) to write or read.
Examples
Arduino
EEPROM.write(0, 123); // Save number 123 at address 0 byte val = EEPROM.read(0); // Read from address 0
Arduino
for (int i = 0; i < 10; i++) { EEPROM.write(i, i * 10); } for (int i = 0; i < 10; i++) { byte val = EEPROM.read(i); Serial.println(val); }
Sample Program
This program saves the number 42 at EEPROM address 5 and then reads it back to print on the serial monitor.
Arduino
#include <EEPROM.h> void setup() { Serial.begin(9600); // Write value 42 to EEPROM address 5 EEPROM.write(5, 42); // Read value from EEPROM address 5 byte value = EEPROM.read(5); Serial.print("Value at address 5: "); Serial.println(value); } void loop() { // Nothing here }
Important Notes
EEPROM can only store bytes (0-255). For bigger data, you need to split it into bytes.
Writing to EEPROM too often can wear it out. Avoid writing in a fast loop.
Use EEPROM.update() if you want to write only when the value changes to save EEPROM life.
Summary
EEPROM stores data that stays after power off.
Use EEPROM.write(address, value) to save a byte.
Use EEPROM.read(address) to get the saved byte back.
Practice
1. What does
EEPROM.write(address, value) do in Arduino?easy
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]
Hint: Write saves data, read retrieves data from EEPROM [OK]
Common Mistakes:
- Confusing write with read function
- Thinking EEPROM.write clears memory
- Assuming EEPROM.write initializes EEPROM
2. Which of the following is the correct syntax to read a byte from EEPROM at address 10?
easy
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]
Hint: Use EEPROM.read(address) to get stored byte [OK]
Common Mistakes:
- Using EEPROM.write to read data
- Using non-existent EEPROM.readByte function
- Confusing EEPROM.get with EEPROM.read
3. What will be the output of this Arduino code snippet?
EEPROM.write(5, 42); byte val = EEPROM.read(5); Serial.println(val);
medium
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]
Hint: Write then read same address returns stored value [OK]
Common Mistakes:
- Assuming EEPROM.read returns address instead of value
- Forgetting to write before reading
- Expecting default 0 without writing
4. Identify the error in this code snippet:
EEPROM.write(1024, 100); byte val = EEPROM.read(1024); Serial.println(val);
medium
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]
Hint: EEPROM addresses start at 0, max 1023 on typical boards [OK]
Common Mistakes:
- Using address equal to EEPROM size
- Assuming EEPROM size is larger than actual
- Misunderstanding Serial.println capabilities
5. You want to store a user's high score (an integer between 0 and 1000) in EEPROM. Which approach correctly saves and reads this value?
hard
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]
Hint: Use EEPROM.put/get for integers, write/read for bytes [OK]
Common Mistakes:
- Using EEPROM.write for multi-byte integers
- Not reading back with EEPROM.get
- Mixing read and write roles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
