I keep seeing RAM, ROM, and Flash memory mentioned in Arduino specs, but I’m not entirely sure what each one does. Like, where does my code get stored? Where does the Arduino keep variables while running? And what actually happens when power is lost? A simple explanation would really help clear things up!
Hey there! Yeah, those memory terms can be confusing at first. Let's break it down simply, Arduino-style.
You forgot to mention an important memory type- the EEPROM.
Think of it like this:
Flash Memory (like a hard drive): This is where your actual code lives. When you upload your sketch, it gets stored here permanently. It's like the hard drive on your computer – it keeps the code even when you turn the power off. So, when you power your Arduino back on, it knows what to do because the code is safe and sound in the Flash.
SRAM (like RAM on your computer): This is your Arduino's working memory. When your code runs, it uses SRAM to store variables, temporary values, and all the stuff it needs to keep track of while it's running. Think of it like your computer's RAM – it's super fast, but it's volatile. That means when you turn the power off, everything in SRAM is gone. Poof! That's why your Arduino starts fresh every time you power it on.
EEPROM (like a tiny, special hard drive): This is a small amount of memory that's also persistent, like flash. But it's not for your main code. It's for storing small bits of data that you want to keep even when the power is off. Think of it like saving your high score in a game. You might use it to store settings, calibration values, or other little things your program needs to remember. It has limited writes though, so don't write to it constantly.
Now let's come to the tricky part, ROM:
ROM (Read-Only Memory) on a typical Arduino (like an Uno) is a bit different than the other memories. In most common Arduino boards, the bootloader (a small piece of code that helps the main program start) is stored in a specific section of the Flash memory, and sometimes people refer to this region as ROM, though technically, it is still flash memory. It's "read-only" in the sense that you don't usually modify it. It's pre-programmed and essential for getting your Arduino up and running.
So, while it's not a separate physical chip like SRAM or a distinct area like EEPROM, it's a designated part of the Flash memory that serves a specific, read-only (from your perspective) function. It's crucial for the Arduino to even know how to load your program from Flash.
Think of it this way: Flash is like a bookshelf. Your code is one book. The bootloader (in the "ROM" area) is like the librarian who knows where to find your book and how to open it. You put your code-book on the shelf (flash), and the bootloader-librarian (in the "ROM" area) helps the Arduino read and run it.
So, to summarize:
- Flash: Where your main code lives.
- SRAM: Where your program's variables live while running.
- EEPROM: For small bits of data you want to persist across power cycles.
- ROM (in Arduino's case, a special part of Flash): Contains the bootloader, essential code to start your program.
Think of these memory types in this way:
const int myConstant = 42; // Stored in Flash (permanent) int myVariable = 10; // Stored in SRAM (temporary) EEPROM.write(0, 25); // Stored in EEPROM (permanent small data)
- Flash: Where the program itself is stored (const values stay here too).
- SRAM: Where runtime variables live (int myvariable).
- EEPROM: Stores values permanently (but with limited writes).
- ROM (bootloader in Flash): Loads your program when the board starts.
If you turn off the power, only Flash and EEPROM keep their data, while SRAM is wiped clean.