Marlin M500 M501 M502 M503 M504 G-Codes – Explanation & Use

Understanding how Marlin firmware makes use of the RAM and EEPROM of your 3D printer to store the configuration can be confusing at first, which, as a result, makes it challenging to ensure that your 3D printer will start the next print with the correct configuration.

In this guide, we will explain the functionality of the M500, M501, M502, M503, and M504 G-code commands in Marlin firmware and demonstrate how you can use them to have complete control over the settings of your 3D printer.

M500 (Save Settings)

The M500 (Save Settings) G-code saves the currently active configuration stored in the RAM to the EEPROM.

marlin m500


Whenever you make a change to the configuration of your 3D printer, these changes are written to the RAM by default, and whenever you start a print, the configuration stored in the RAM is used.

That being said, since the RAM of your 3D printer is volatile, it can only retain data as long as it’s powered on, and as a result, any changes you make to the configuration will be lost once the 3D printer is powered off unless it’s written to somewhere else that isn’t the RAM.

This is where the EEPROM, which is a form of non-volatile memory (can retain data when powered off) comes in, and fulfills the duty of permanently storing your 3D printer’s configuration whenever necessary.

So, whenever you run the M500 G-code and save the configuration stored in the RAM to the EEPROM, you’re practically making the new configuration permanent and completely removing the old configuration from your 3D printer’s memory.

For instance, when you use the Store Settings menu option on an Ender 3, the M500 G-code is what actually runs behind the scenes, and if you power cycle your Ender 3 without clicking on Store Settings after a modification, you’ll notice that your modification is lost.

Running the M500 G-code requires the EEPROM_SETTINGS parameter to be defined in the Marlin firmware configuration.

M501 (Restore Settings)

The M501 (Restore Settings) G-code loads the currently stored configuration from the EEPROM to the RAM. In a way, you can think of M501 as the exact opposite of M500.

marlin m501


Since the firmware uses the configuration in the RAM during operation and not the EEPROM, the data in the EEPROM must first be loaded into the RAM for the settings you have saved with M500 to take effect, which is where M501 comes in.

That being said, as Marlin firmware automatically loads the data in the EEPROM to the RAM on boot by internally calling the same function that running the M501 G-code does, it’s not necessary to manually run the M501 command whenever you turn your 3D printer on.

With this in mind, we can say that the primary use case for M501 is to undo the temporary changes that you haven’t saved to the EEPROM, such as when you would like to see how your 3D printer performs with a particular set of modifications to the configuration, and go back to what’s already working afterward.

For instance, clicking on the Load Settings menu option on an Ender 3 is the same as running the M501 G-code, and if you click this option after making a change to any parameter, such as the Z offset, you’ll notice that it goes back to the value before you modified it.

Running the M501 G-code requires the EEPROM_SETTINGS parameter to be defined in the Marlin firmware configuration.

M502 (Factory Reset)

The M502 (Factory Reset) G-code loads the firmware’s default configuration to the RAM.

marlin m502


Aside from what’s currently stored in the RAM and the EEPROM, there is one more set of configurations the firmware can access at any given time, which are the initial (default) values that come hard-coded with the firmware itself.

These values are stored in the Configuration.h and the Configuration_adv.h files of Marlin firmware and are meant to be configured on a case-by-case basis, which makes it possible to create forks of Marlin firmware that are suitable for each different 3D printer.

For instance, the NOZZLE_TO_PROBE_OFFSET value is arbitrarily set to { 10, 10, 0 } in the default version of Marlin, but as the correct nozzle offset values will naturally be different for every 3D printer, this is something you can modify with the M851 G-Code and write to EEPROM for permanent storage.

In such a scenario, as the default value will always be hard-coded to the firmware and never be completely lost, even when you have saved your custom offset value to both the RAM and the EEPROM, you can revert to it whenever you wish by using the M502 G-code.

With this in mind, the M502 G-code will only be useful if the Marlin fork you’re using comes with configuration files that are correctly modified for the exact 3D printer you’re using, as loading the “factory defaults” won’t mean much if they weren’t suitable for your printer to start.

For instance, if you’ve flashed a fork of Marlin specifically made for your 3D printer, such as Professional Firmware (also known as the MRiscoC fork) for Ender 3 V2, you will find that almost all the default values work perfectly, which makes resetting to the factory defaults a reliable way to obtain a working set of configurations.

Remember that you will need to run M500 after M502 to make the factory defaults permanent, as Marlin will automatically load the old configuration in the EEPROM the next time you power on your printer if you don’t save the factory defaults to the EEPROM.

M503 (Report Settings)

The M503 (Report Settings) G-code prints the configuration currently stored in the RAM to the console.

marlin m503


This makes it possible to see all the configurable parameters of the firmware, along with their current values, in one place, which often comes in handy for backing up your current settings before making modifications.

While you can technically do the same thing by browsing the menus through your 3D printer’s panel and taking note of each value individually, M503 makes the process much less time-consuming.

Additionally, with the C flag, getting the M503 G-code to save the current configuration as a ZIP file to the SD card is possible, which offers another convenient way of keeping your 3D printer’s configuration in external storage (requires CONFIGURATION_EMBEDDING).

Running the M503 G-code requires the DISABLE_M503 parameter not to be defined in the Marlin firmware configuration.

M504 (Validate EEPROM)

The M504 (Validate EEPROM) G-code validates the correctness of the data stored in the EEPROM.

marlin m504


With this command, you can quickly find out if your 3D printer can correctly process the data in the EEPROM and whether any problems you’re having with your prints are related to the corruption of the configuration.

Specifically, in a scenario where you flash a new firmware to your 3D printer without erasing the EEPROM, using the M504 G-code will come in extremely handy to verify whether the data in the EEPROM is compatible with the new firmware and potentially save you from reconfiguring all the settings again.

Running the M504 G-code requires the EEPROM_SETTINGS parameter to be defined in the Marlin firmware configuration.

Conclusion

Now that you have a better understanding of the G-code commands that allow you to interact with how your 3D printer’s settings are saved, tasks such as trying different configurations without losing your original one or verifying that your printer is configured correctly should become a lot easier.

Since configuring your 3D printer as optimally as possible is one of the essential aspects of having successful prints, having good command over how the firmware stores the configuration is definitely a skill worth adding to your toolkit.