0Pricing
Arduino & IoT Academy · 课时

将数值存入 EEPROM

写入并读取掉电不丢失的设置

将数值存入 EEPROM 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Memory That Forgets

Normal variables live in RAM, which wipes clean the moment power drops. To keep a setting around, you need memory that remembers.

Meet EEPROM

EEPROM is a small block of storage built into the chip that survives power loss. Think of it as a tiny notepad that stays written.

How Much Space

An Arduino Uno gives you about 1024 bytes of EEPROM. It is small, so save settings and counters, not big logs.

Addresses, Not Names

EEPROM is organized by numbered slots called addresses, starting at 0. You choose which address holds each value.

Include the Library

To use it, pull in the built-in EEPROM library at the top of your sketch. No download needed; it ships with the IDE.

#include <EEPROM.h>

Write a Byte

EEPROM.write stores one byte at an address. Here we save the number 42 into slot 0.

EEPROM.write(0, 42);

Read It Back

EEPROM.read fetches the byte you stored. Read slot 0 and you get your value back, even after a reset.

byte value = EEPROM.read(0);

One Byte Holds 0 to 255

A single byte stores values from 0 to 255. To save a larger number, you split it across several addresses.

Save Bigger Values Easily

For ints, floats, or structs, use EEPROM.put and EEPROM.get. They handle the byte splitting for you automatically.

int score = 1500;
EEPROM.put(0, score);

Writes Wear Out

EEPROM survives roughly 100,000 writes per cell. Don't write every loop, or you'll burn it out fast.

Write Only on Change

Use EEPROM.update instead of write. It skips the write when the value is unchanged, sparing the memory.

EEPROM.update(0, 42);

Quick Check

Let's check why we reach for EEPROM at all.

Recap: Persistent Storage

You saw EEPROM as non-volatile memory, wrote and read bytes by address, and learned to limit writes. Your settings can now persist! 💾

常见问题解答

「将数值存入 EEPROM」课时是免费的吗?

是的 — 「将数值存入 EEPROM」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。

「将数值存入 EEPROM」这节课中我会学到什么?

写入并读取掉电不丢失的设置 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Arduino & IoT Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「将数值存入 EEPROM」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?

能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 按钮为什么会“抖动”
  2. 使用 millis() 消抖
  3. 将数值存入 EEPROM
  4. 复位后保留计数器数值
← 返回 Arduino & IoT Academy