# Running code off a SD Card

**URL:** <https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109>\
**Category:** General Guidance\
**Created:** [July 7, 2022, 5:57am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109 "2022-07-07T05:57:12Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 7, 2022, 7:06am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/21 "2022-07-07T07:06:31Z")

</div>

Can the Atmega328 run code from RAM ?

---

<div class="post-metadata">

**Author:** ![madmark2150](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@madmark2150](https://forum.arduino.cc/u/madmark2150)\
**Post date:** [July 7, 2022, 7:07am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/22 "2022-07-07T07:07:17Z")

</div>

Dunno. Is why I'm asking. The memory model seems flexible enough but I've not done a deep dive. But this is how programs load and go, no? You can jump to a known address, no? So if you load an array and jump to it. Or does the Harvard architecture prevent that? I know not all Arduinos are Harvard, so it should work on the non-Harvard machines.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [July 7, 2022, 7:15am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/23 "2022-07-07T07:15:14Z")

</div>

> [@outbackhut](#):
>
> I am planning on having quite a few "apps" running on it (one at a time of course), but each "app" might be HUGE.

This sounds clearly to me like a need for an operating system. Load many large apps into memory from SD card and execute. Pi zero has an operating system, Pico does not. I'm sure it could be done in Pico, but @outbackhut would have to "reinvent the wheel".

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [July 7, 2022, 9:49am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/24 "2022-07-07T09:49:59Z")

</div>

Split this topic on July 7, 2022, 9:49am UTC

7 posts were split to a new topic: [The price of a Pi](https://forum.arduino.cc/t/the-price-of-a-pi/1010174)

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [July 7, 2022, 7:17am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/25 "2022-07-07T07:17:43Z")

</div>

> [@madmark2150](#):
>
> But this is how programs load and go, no?

No.

> [@madmark2150](#):
>
> Or does the Harvard architecture prevent that?

Yes.

> [@madmark2150](#):
>
> I know not all Arduinos are Harvard, so it should work on the non-Harvard machines.

No, because the boot loader would need to be modified to do this.

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [July 7, 2022, 7:18am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/26 "2022-07-07T07:18:00Z")

</div>

lot of confusion in this tread. so some summary

the code on Mega runs from the flash memory. you report 300% of dynamic memory, which is RAM, not flash. so you have to move some data (not code) to flash (or to SD card).

Raspberry Pico is an MCU, not a usual Raspberry Pi mini computer .

the tread should focus on Mega, not on Pi Zero price

---

<div class="post-metadata">

**Author:** ![SteveThackery](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@SteveThackery](https://forum.arduino.cc/u/SteveThackery)\
**Post date:** [July 7, 2022, 8:17am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/33 "2022-07-07T08:17:26Z")

</div>

> [@madmark2150](#):
>
> I know not all Arduinos are Harvard

Hey, I didn't know that. Which ones are not Harvard?

---

<div class="post-metadata">

**Author:** ![madmark2150](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@madmark2150](https://forum.arduino.cc/u/madmark2150)\
**Post date:** [July 7, 2022, 8:48am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/34 "2022-07-07T08:48:46Z")

</div>

Ok. thanks for the explanation. Boot loader makes sense.

---

<div class="post-metadata">

**Author:** ![madmark2150](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@madmark2150](https://forum.arduino.cc/u/madmark2150)\
**Post date:** [July 7, 2022, 8:51am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/35 "2022-07-07T08:51:29Z")

</div>

Read it somewhere, but can't recall. I seem to recall a memory model diagram with two cols, one Harvard and one Von Neumann with different part numbers under each, but I'm not up to speed on all the variants as, for now, I'm strictly MEGA 2560 based. I've got all the power and screw terminal shields and mounts and crap all worked out for that platform.

---

<div class="post-metadata">

**Author:** ![outbackhut](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/outbackhut/32/1099892_2.png) [@outbackhut](https://forum.arduino.cc/u/outbackhut)\
**Post date:** [July 7, 2022, 9:18am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/36 "2022-07-07T09:18:52Z")

</div>

> [@PaulRB](#):
>
> reinvent the wheel

No thanks  
I'm trying to make a Arduino OS

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [July 7, 2022, 9:24am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/37 "2022-07-07T09:24:51Z")

</div>

> [@madmark2150](#):
>
> I know not all Arduinos are Harvard

The ARM and ESP -based Arduinos are generally capable of running code from RAM. But most of the SAMD-based boards don't really have enough RAM to make it interesting.

An interpreter (Bitlash, Forth, micro- or circuit- Python) might work if your "applications" are quite short (You'd want to copy them into RAM for performance reasons, probably.)

An RPi Pico, or one of the other boards based on it, has substantial flash (2MB on a Pico to 8MB on an Adafruit Feather rp2040 ($12) or 16MB on a Sparkfun ThingPlus rp2040 ($20 - with an SD socket.)) and enough RAM to make this possible. So do many ESP32 boards.

Note that there would be substantial "software engineering" effort to get individual small "apps" from an SD card working. You'd have to divide up the functions that would be part of the flash-based code, and what functions would be usable from the "apps", and how they would interact. Essentially an "OS", as some have said.

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [July 7, 2022, 9:55am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/38 "2022-07-07T09:55:29Z")

</div>

I've moved the stuff about expensive components at the request of one of the contibutors because it wasn't relevant to the OP's question.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 7, 2022, 10:09am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/39 "2022-07-07T10:09:54Z")

</div>

[I have one of these.](https://www.rugged-circuits.com/new-products/quadram)

The 8K internal RAM becomes dedicated stack space, occupies the 1st 8K address space.

The 8 banks of 64K have their 1st 8K unusable, leaving 8K to 64K heap in 8 banks.

Will you be writing an interpreter that reads source from SD?

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [July 7, 2022, 11:30am UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/41 "2022-07-07T11:30:33Z")

</div>

> [@outbackhut](#):
>
> I'm trying to make a Arduino OS

== re-inventing the wheel

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [July 7, 2022, 12:03pm UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/42 "2022-07-07T12:03:12Z")

</div>

Generally in this situation, you can just buy your way out of trouble with better hardware (Due, Teensy, Feather for example) and use the Mega for another project.

As noted earlier though, posting your code will likely get you hints about how to reduce your RAM usage.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 7, 2022, 12:05pm UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/43 "2022-07-07T12:05:38Z")

</div>

> [@outbackhut](#):
>
> I'm trying to make a Arduino OS

School's out.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 7, 2022, 12:32pm UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/44 "2022-07-07T12:32:17Z")

</div>

> [@anon73444976](#):
>
> School's out.

for summer ?

It's winter here 😉

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [July 7, 2022, 12:36pm UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/45 "2022-07-07T12:36:42Z")

</div>

> [@sterretje](#):
>
> for summer ?

No second line:-  
for ever.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 7, 2022, 12:40pm UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/46 "2022-07-07T12:40:41Z")

</div>

🤣 🤣

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 7, 2022, 7:40pm UTC](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109/47 "2022-07-07T19:40:08Z")

</div>

> [@madmark2150](#):
>
> Why not? Load it as a byte array and call the array pointer address. As long as it's self contained and ends in a return it SHOULD work. Or am I missing something?

What you miss is that the AVR Instruction Pointer points into FLASH MEMORY.

Code in flash can read data in RAM (SD has a 512 byte buffer by default) and cause code in flash to execute, which is why I asked if what's wanted is an Interpreter.

Whatever you're planning is going to teach you a lot of what not to do.

[Previous page](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109.md?page=1)

[Next page](https://forum.arduino.cc/t/running-code-off-a-sd-card/1010109.md?page=3)
