Upload my main code to SD Card?

So I was wondering if there is any way that I could upload my main code to the address card and have my arduino read it from there? This would be helpful so if the power goes out or something I wouldn’t lose all of my data and have to reupload it to all of my different things. Thanks in advance.

It's not clear what you mean by "my main code". Your Arduino sketch is in FLASH and will run again when the power comes back. Your sketch can tore data in EEPROM or in files on an SD card if it needs that data to continue where it left off.

The code? When you write a program to Arduino, The sketch is saved on the board itself It will execute always upon being powered up. btw if you mean to save some kind of log details like the temperature being saved to the sd card, that is possible.
And yes, for the sd card, u would need an sd card module.
Download the library for sd card(mostly its built-in)
Look for the example that creates a file if the file does not exist and then u can change the code to log temperature details at specific interval
And yes, welcome to the arduino community!

Whenever I have lost power and plugged it back in, it acts like I factory reset it. And what I mean by my main code is the code that I made in arduino IDE. For example, one of my projects is an alarm clock, and when I regain power I want it to continue to display the time and whatnot.

You need a way to keep track of the time while the Arduino is turned off. Do you have a Real-Time Clock module connected to your Arduino?

Yes, I do

Please post the sketch that you are having the problem wit, using code tags when you do

Does said Real Time Clock have a working battery?

It is practically brand new, so I assume so. How might I test it to see for sure? And would that affect how the code is actually stored?

Which RTC did you buy (link please)? Did it come with a battery and did you insert it.?

You were asked to post your sketch, please do so. Don't forget to post using code tags as described in How to get the best out of this forum.

The reason to ask for your sketch is that e.g. a number of example sketches contain a line that sets the initial date and time of the RTC to the date and time that the sketch was compiled. So if you power cycle your board, that will be executed again.

It came in this set:
ELEGOO Mega R3 Project The Most Complete Ultimate Starter Kit w/ TUTORIAL Compatible with Arduino IDE https://a.co/d/gfmGGBa

The battery came pre-inserted. I’m not able to get my computer rn to post the code, but I will asap

That's a DS3231 RTC.

@rickface we can keep suggesting possible causes of your problem but I suspect that it is caused by your sketch which you still have not posted

How might a sketch sort of delete itself when it loses power?

I don't believe that is what is happening

Try uploading the Blink sketch and once you see that is working disconnect the power to the Arduino. Does the Blink sketch run when you connect the power again ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.