📖 Scenario: You are building a small device that remembers a user's preferred brightness setting even after it is turned off. To do this, you will store the brightness value in the Arduino's EEPROM memory.
🎯 Goal: Create a program that saves a brightness setting to EEPROM and reads it back when the device starts.
📋 What You'll Learn
Use the Arduino EEPROM library
Store a brightness value (0-255) in EEPROM address 0
Read the brightness value from EEPROM on startup
Print the stored brightness value to the Serial Monitor
💡 Why This Matters
🌍 Real World
Many devices need to remember user settings like brightness or volume even after being turned off. EEPROM allows saving these settings permanently.
💼 Career
Understanding EEPROM is important for embedded systems programming and working with microcontrollers in IoT and hardware projects.
Progress0 / 4 steps
1
Setup EEPROM and initial brightness value
Include the EEPROM.h library and create a variable called brightness with the value 128.
Arduino
Hint
Use #include <EEPROM.h> to include the EEPROM library.
Create an int variable named brightness and set it to 128.
2
Write brightness to EEPROM
In the setup() function, write the brightness value to EEPROM address 0 using EEPROM.write().
Arduino
Hint
Use EEPROM.write(address, value) to save the brightness value at address 0.
3
Read brightness from EEPROM
In the setup() function, after writing, read the brightness value back from EEPROM address 0 using EEPROM.read() and store it in the brightness variable.
Arduino
Hint
Use brightness = EEPROM.read(0); to get the stored value back.
4
Print brightness value to Serial Monitor
Initialize the Serial communication at 9600 baud in setup() and print the brightness value using Serial.println().
Arduino
Hint
Use Serial.begin(9600); to start serial communication.
Use Serial.println(brightness); to print the brightness value.
Practice
(1/5)
1. What is the main purpose of using EEPROM in an Arduino program?
easy
A. To increase the speed of the program
B. To store data permanently even when the power is off
C. To connect to the internet
D. To control the brightness of an LED
Solution
Step 1: Understand EEPROM functionality
EEPROM memory keeps data saved even if the Arduino loses power.
Step 2: Compare options with EEPROM purpose
Only To store data permanently even when the power is off describes permanent data storage, which matches EEPROM use.
Final Answer:
To store data permanently even when the power is off -> Option B
Quick Check:
EEPROM stores permanent data = A [OK]
Hint: EEPROM saves data without power loss [OK]
Common Mistakes:
Confusing EEPROM with RAM (temporary memory)
Thinking EEPROM speeds up the program
Mixing EEPROM with internet or LED control
2. Which of the following is the correct syntax to write the value 42 to EEPROM address 10?
easy
A. EEPROM.save(42, 10);
B. EEPROM.read(10, 42);
C. EEPROM.write(10, 42);
D. EEPROM.store(10, 42);
Solution
Step 1: Recall EEPROM write syntax
The correct function to save data is EEPROM.write(address, value);.
Step 2: Match syntax with options
Only EEPROM.write(10, 42); uses EEPROM.write with correct parameter order.
Final Answer:
EEPROM.write(10, 42); -> Option C
Quick Check:
Write uses EEPROM.write(address, value) = D [OK]
Hint: Use EEPROM.write(address, value) to save data [OK]
Common Mistakes:
Using EEPROM.read() to write data
Using non-existent functions like EEPROM.store()
Swapping address and value parameters
3. What will be the output of this Arduino code snippet?
EEPROM.write(5, 100);
int val = EEPROM.read(5);
Serial.println(val);
medium
A. Error
B. 0
C. 5
D. 100
Solution
Step 1: Understand EEPROM write and read
The code writes 100 to address 5, then reads from address 5.
Step 2: Predict Serial output
Reading address 5 returns 100, so Serial.println prints 100.
Final Answer:
100 -> Option D
Quick Check:
Write 100, read 100 = A [OK]
Hint: Read returns last written value at address [OK]
Common Mistakes:
Expecting read to return address instead of value
Confusing EEPROM.read() with EEPROM.write()
Assuming default value 0 without write
4. Identify the error in this code that tries to save a setting to EEPROM:
int setting = 25;
EEPROM.read(0, setting);
medium
A. Using EEPROM.read() instead of EEPROM.write() to save data