Consider the following Arduino code that writes a value to EEPROM and then reads it back. What will be printed on the Serial Monitor?
#include <EEPROM.h> void setup() { Serial.begin(9600); EEPROM.write(10, 123); int val = EEPROM.read(10); Serial.println(val); } void loop() {}
EEPROM.write stores a byte at the given address, and EEPROM.read retrieves it.
The code writes the value 123 to EEPROM address 10, then reads it back and prints it. So the output is 123.
This code uses EEPROM.update to write a value. What value will be stored at address 5 after running this code?
#include <EEPROM.h> void setup() { EEPROM.write(5, 50); EEPROM.update(5, 50); EEPROM.update(5, 100); } void loop() {}
EEPROM.update only writes if the new value differs from the stored one.
Initially 50 is written. The first update writes 50 again, so no change. The second update writes 100, which is different, so EEPROM stores 100.
The code below tries to read an int from EEPROM but prints unexpected values. What is the main reason?
#include <EEPROM.h> void setup() { Serial.begin(9600); int val = EEPROM.read(0) + (EEPROM.read(1) << 8); Serial.println(val); } void loop() {}
Think about how to read multi-byte data types from EEPROM safely.
EEPROM.read reads one byte at a time. Combining bytes manually can cause errors. EEPROM.get reads multi-byte types correctly and safely.
Which of these code snippets will cause a compilation error?
EEPROM.write expects a byte value (0-255).
256 is out of byte range and causes a compilation error or warning. Other values are valid bytes.
Given the struct and EEPROM.put code below, how many bytes are written to EEPROM?
#include <EEPROM.h> struct Data { int a; byte b; float c; }; void setup() { Data d = {100, 5, 3.14}; EEPROM.put(20, d); } void loop() {}
Consider the size of each data type on Arduino Uno (int=2 bytes, byte=1 byte, float=4 bytes).
int (2 bytes) + byte (1 byte) + float (4 bytes) = 7 bytes total.