Where is the correct Marlin firmware file and location to add code that I want to shop up in the LCD menu of my printer, and then execute the function I write when the button is pressed?

For example I want to add a menu item that says "Preheat Custom" that is in the same menu as "preheat PLA" and "preheat ABS" and then runs code to heat to values I specify.

I'm running Marlin Firmware version 1.1.9 on a Creality Ender 3.


The answer to your question is the file ultralcd.cpp. Nowadays, you can also enable extra option through the Configuration_adv.h file, just enable:


and edit the options beneath it to your needs (otherwise it will use the preset values from the Configuration.h file).

Add custom items using ultralcd.cpp

This is how I used to do it if you want to add items to the menu in Marlin Firmware through the ultralcd.cpp. It is best to first look at the current implementation of the menu items. As you already mention Preheat PLA, that would be the first to search for. Searching in files is easy when you go to the github website with the Marlin firmware sources, functionality is available for searching in the files. Alternatively, download a copy of the firmware and use a free "grep" utility to search in files.

Searching for Preheat PLA will show you a bunch of language translation files. These point to the use of a constant MSG_PREHEAT_1 which finds its presence in ultralcd.cpp. This hints to function lcd_preheat_m1_menu that is called by MENU_ITEM which adds menu items to LCD. You could start there to add your own option.


As a quick demonstration, I've added a CUSTOM PREHEAT item by copying the lcd_preheat_m2_menu function in ultralcd.cpp and renamed this lcd_preheat_m3_menu (a full functional item needs changes within the lcd_preheat_m3_menu as it now uses the constants from the ABS preheat option).

You then add the item to the menu by changing this part of the code:

      // Preheat for Material 1 and 2
      #if TEMP_SENSOR_1 != 0 || TEMP_SENSOR_2 != 0 || TEMP_SENSOR_3 != 0 || TEMP_SENSOR_4 != 0 || HAS_HEATED_BED
        MENU_ITEM(submenu, MSG_PREHEAT_1, lcd_preheat_m1_menu);
        MENU_ITEM(submenu, MSG_PREHEAT_2, lcd_preheat_m2_menu);
        // ADD THIS LINE:
        MENU_ITEM(submenu, "CUSTOM PREHEAT", lcd_preheat_m3_menu);
        MENU_ITEM(function, MSG_PREHEAT_1, lcd_preheat_m1_e0_only);
        MENU_ITEM(function, MSG_PREHEAT_2, lcd_preheat_m2_e0_only);

After compiling and uploading to the printer board, enter the Prepare menu and scroll down to see:

LCD showing custom preheat option


As Mark said, Marlin supports a number of menu items in it's advanced configuration. These items are meant to run custom G-code, which in this case (adding a preheat action with custom target extruder and bed temp) is enough to fill your needs. So, let's see that advanced configuration file: Configuration_adv.h. The section we are looking for is almost at the end of the file, you can ctrl+f for "CUSTOM_USER_MENUS" to find it.

  #define USER_SCRIPT_DONE "M117 User Script Done"
  //#define USER_SCRIPT_RETURN  // Return to status screen after a script

  #define USER_DESC_1 "Do the trick"
  #define USER_GCODE_1 "G91\nG0 z5\nG0 y10"

  #define USER_DESC_2 "Park"
  #define USER_GCODE_2 "G27 P2"

  //#define USER_DESC_2 "Preheat for PLA"

  //#define USER_DESC_3 "Preheat for ABS"

  //#define USER_DESC_4 "Heat Bed/Home/Level"
  //#define USER_GCODE_4 "M140 S" STRINGIFY(PREHEAT_2_TEMP_BED) "\nG28\nG29"

  //#define USER_DESC_5 "Home & Info"
  //#define USER_GCODE_5 "G28\nM503"

The source code downloaded from the official repo actually comes with some examples (the ones commented) and I added two more just for fun. To get your custom preheat menu item working you'll make use of the following Gcodes:


The supplement on how to proceed with custom menus in practice:

Enable (uncomment) this line in configuration_adv.h:


Below there are 5 sample entries, later you can change them or remove/comment out. You can add new, for example:

  #define  USER_DESC_6 "Home Z (0.2)"
  #define USER_GCODE_6 "G28 Z\nG0 Z0.2"

  #define  USER_DESC_7 "Fan on"
  #define USER_GCODE_7 "M106 S255"

  #define  USER_DESC_8 "Fan off"
  #define USER_GCODE_8 "M107"

Marlin currently (v2.0.7.2) supports up to 25 menu items (see menu_custom.cpp). There may be gaps in these numbers, so you can comment out some unnecesary options without fixing numbering of others, which could be really convenient.

Custom commands can be executed in runtime (when already printing). I could use the above "home Z" after manually tuning Z endstop to correct first layer's height quickly, without restarting the print. So you can actually make harm to ongoing print as well.


There might be a better solution.

Marlin supports custom user menus, in configuration_adv.h, you probably should try to keep your changes limited to the configuration*.h files.

The menu code is kind of ugly and you can easily break things.

  • 1
    $\begingroup$ Hi Mark and welcome to SE.3DP! That sounds like a good point to be made. Could you provide a snippet of the code? I haven't looked myself, and maybe it is too large to summarise, but if you could edit your answer and show a part of the code and illustrate how/where to insert, that would be a great addition to the answer. $\endgroup$ – Greenonline Oct 22 '19 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.