<p>Another answer that does exactly what you want involves the use of a print server. A print server is an application that runs the instructions to the printer over a USB connection from another device, this can be your computer/laptop, or a dedicated <a href="https://www.raspberrypi.org/" rel="nofollow noreferrer">Raspberry Pi</a> (a small and affordable computer). One such application described here further is <a href="https://octoprint.org/" rel="nofollow noreferrer">OctoPrint</a> (this may very well be done with other applications, but this needs to be checked first!), this print server application allows integration of many third party plug-ins next to the extensive feature set it already has out-of-the-box. One such feature is <code>GCODE scripts</code> (intently spelled this way to match the option in the Octoprint settings menu); this screenshot shows some details:</p>

<p><a href="https://i.sstatic.net/FZIeq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FZIeq.png" alt="GCODE scripts menu options item"></a></p>

<p>As can be seen from the image, there are specific "events" available to process G-code commands at specific event occurences like e.g. just before the print starts. You could use that envent to insert your leveling commands.</p>

<hr>

<p>Please note that in the image you will find strange G-code commands like <code>OCTO100</code> and <code>OCTO110</code> which is a feature of the plugin called "GCODE System Commands" which allows running shell scripts to schedule the fan. I just kill the power to the fan when the printer is idle to get rid of the noise when the printer is just idling, the fan is only needed when the hotend is at elevated temperature.</p>

CC BY-SA 4.0
2018-08-29T15:25:07.353Z
2018-08-29T16:39:13.780Z
2018-08-29T16:39:13.780Z
0scar
<p>With Marlin 1.1.0, you can automatically run a G-Code file when powering on the printer with a SD card already present.</p>

<p>Add a file named <code>auto0.g</code> at the root of your card, containing the following G-Code:</p>

<pre><code>G28 ;Auto-homing
G29 ;Bed leveling
</code></pre>

<p>Normally the bed leveling map should be reused for all subsequent prints, until the printer is turned off.</p>

<p>It is possible to provide up to 10 files, from <code>auto0.g</code> to <code>auto9.g</code>.</p>

CC BY-SA 4.0
2018-09-13T03:56:50.273Z
Would you like to post this (or a derivative thereof) as an answer to the question [Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?](https://3dprinting.stackexchange.com/questions/6778/can-g-code-scripts-be-run-automatically-on-inserting-an-sd-card-when-using-marli)
Greenonline
CC BY-SA 4.0
2018-08-25T14:14:47.283Z
2018-09-05T23:04:31.047Z
2018-09-05T23:04:31.047Z
Nikkoura
<p>Rather than placing the <code>G28</code> (Home) and <code>G29</code> (bed level) in the configuration, I would place it in the G-code generating slicer as pre-print code. This will automatically add this to the start of any G-code sliced, enforcing the homing and leveling whenever the G-code is run.</p>

CC BY-SA 4.0
2018-08-26T13:34:45.220Z
2018-08-26T13:34:45.220Z
Trish
<p>Rather than modifying the firmware to handle this, have you considered a pre-processing script on your computer, greping for a <code>G29</code> in the G-code, then adding a <code>G28</code>/<code>G29</code> pair at the start of the file if no <code>G29</code> is found?</p>

CC BY-SA 4.0
2018-08-26T08:48:35.593Z
After re-reading the question, it seems this is maybe the only way to solve the problems in this case, unless Octoprint can do the same as a preprocess.
Sean Houlihane
CC BY-SA 4.0
2018-08-29T15:26:26.340Z
@SeanHoulihane I've added an [answer](https://3dprinting.stackexchange.com/a/6777/5740) that describes how to do that with the use of OctoPrint
0scar
CC BY-SA 4.0
2018-08-25T13:39:12.133Z
2018-08-25T13:54:40.253Z
2018-08-25T13:54:40.253Z
Nikkoura
<p>I'm not that fluent in G-code, but at <a href="https://www.mmsonline.com/columns/understanding-g27-g28-g29-and-g30" rel="nofollow noreferrer">Modern Machine Shop: Understanding G27, G28, G29 and G30</a> I found these descriptions of the <code>G28</code> and <code>G29</code> codes you added:</p>

<ul>
<li><p><code>G28</code>: For any axis letter addresses included in the G28 command, the machine first will move (at rapid) to an intermediate position in those axes. Then, it will rapid to the zero return position in the commanded axes.</p></li>
<li><p><code>G29</code>: G29 is also a two-step command. First, it causes the machine to move (in the axes commanded) to the intermediate position used in the most recent <code>G28</code> command. Second, it causes the machine to move to the position included in the <code>G29</code> command.</p></li>
</ul>

<p>So it sounds to me like you only need one of those two commands. It seems that the code you modified runs separately on startup, rather than for each separate print. I would first try your modification with each G command separately, so you can see exactly what the difference is on your hardware.</p>

<p>I don't know my way around the code involved, but it looks like you'd need to make the modification in another place, such as just before whatever loop sends successive commands to the printer.</p>

<p>Hope that's helpful despite my limited fluency...</p>

CC BY-SA 3.0
2017-10-14T15:45:25.543Z
These descriptions you found relate to the G28/G29 commands on professional CNC machines. In 3D printer firmware, they have been (mis-)appropriated for other uses. G28 performs a homing sequence to the endstops (this kind of - but not really - corresponds to your description), while G29 initiates auto-bed leveling.
Tom van der Zanden
CC BY-SA 4.0
2017-10-14T14:46:05.230Z
2019-06-10T20:14:17.883Z
2019-06-10T20:14:17.883Z
TextGeek
<p>I want to add auto bed leveling before each print. When I enable auto bed level in <code>configuration.h</code>, it only shows <strong>auto bed</strong> in menu. I found this code in <code>cardreader.cpp</code></p>

<pre><code>void CardReader::openAndPrintFile(const char *name) {
    char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
    sprintf_P(cmd, PSTR("M23 %s"), name);
    for (char *c = &amp;cmd[4]; *c; c++) *c = tolower(*c);
    enqueue_and_echo_command(cmd);
    enqueue_and_echo_commands_P(PSTR("M24"));
}
</code></pre>

<p>and changed it to</p>

<pre><code>void CardReader::openAndPrintFile(const char *name) {
    char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
    sprintf_P(cmd, PSTR("M23 %s"), name);
    for (char *c = &amp;cmd[4]; *c; c++) *c = tolower(*c);
    enqueue_and_echo_command("G28");
    enqueue_and_echo_command("G29");
    enqueue_and_echo_command(cmd);
    enqueue_and_echo_commands_P(PSTR("M24"));
}
</code></pre>

<p>Now before each print, the printer does auto bedding <strong>two</strong> times but when print starts the auto bedding is ignored and printer acts like before doing auto bed.</p>

<p>Please help me solve this.</p>

<p>I'm using Marlin Firmware 1.1.0.</p>

CC BY-SA 4.0
2019-06-10T21:02:45.263Z
@HadiBarak you can edit Gcode with any TXT editor, is just needed to add G29 after G28.
Fernando Baltazar
CC BY-SA 3.0
2017-06-18T19:43:27.407Z
Why don't you just add the auto leveling commands to your start G-code (in your slicer)?
Tom van der Zanden
CC BY-SA 3.0
2017-06-18T19:53:03.767Z
I want it to be automatic, some times I only have g-code file and it's not possible to edit all of them
Hadi Barak
CC BY-SA 3.0
2017-11-15T10:03:33.050Z
@HadiBarak If configured correctly, you will have an option to auto level the bed before manually starting a print. This will be under Prepare -> bed leveling (or whatever the option is called). Adding it to the start G-code is the best solution. But make sure you put it after G28 (auto homing) You are not able to automatically run the command when printing something because this is controlled by the starting G-code.
Granny
<marlin>
How can I add auto bed leveling before print?
Hadi Barak
# How can I add auto bed leveling before print?

(Asked by: username_0 on 2017-06-18)

I want to add auto bed leveling before each print. When I enable auto bed level in configuration.h, it only shows auto bed in menu. I found this code in cardreader.cpp
void CardReader::openAndPrintFile(const char *name) {
    char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
    sprintf_P(cmd, PSTR("M23 %s"), name);
    for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
    enqueue_and_echo_command(cmd);
    enqueue_and_echo_commands_P(PSTR("M24"));
}

and changed it to
void CardReader::openAndPrintFile(const char *name) {
    char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
    sprintf_P(cmd, PSTR("M23 %s"), name);
    for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
    enqueue_and_echo_command("G28");
    enqueue_and_echo_command("G29");
    enqueue_and_echo_command(cmd);
    enqueue_and_echo_commands_P(PSTR("M24"));
}

Now before each print, the printer does auto bedding two times but when print starts the auto bedding is ignored and printer acts like before doing auto bed.
Please help me solve this.
I'm using Marlin Firmware 1.1.0.

**Tags:** marlin
> **Comments:**
> **username_2**: Why don't you just add the auto leveling commands to your start G-code (in your slicer)?  
> **username_0**: I want it to be automatic, some times I only have g-code file and it's not possible to edit all of them  
> **username_3**: @HadiBarak If configured correctly, you will have an option to auto level the bed before manually starting a print. This will be under Prepare -> bed leveling (or whatever the option is called). Adding it to the start G-code is the best solution. But make sure you put it after G28 (auto homing) You are not able to automatically run the command when printing something because this is controlled by the starting G-code.  
> **username_1**: @HadiBarak you can edit Gcode with any TXT editor, is just needed to add G29 after G28.  

---

## 5 answers

### Answer by username_5 on 2018-08-25. Score: 6

Rather than modifying the firmware to handle this, have you considered a pre-processing script on your computer, greping for a `G29` in the G-code, then adding a `G28`/`G29` pair at the start of the file if no `G29` is found?

> **Comments:**
> **username_8**: After re-reading the question, it seems this is maybe the only way to solve the problems in this case, unless Octoprint can do the same as a preprocess.  
> **username_4**: @SeanHoulihane I've added an [answer](https://3dprinting.stackexchange.com/a/6777/5740) that describes how to do that with the use of OctoPrint  

---

### Answer by username_4 on 2018-08-29. Score: 5

Another answer that does exactly what you want involves the use of a print server. A print server is an application that runs the instructions to the printer over a USB connection from another device, this can be your computer/laptop, or a dedicated [Raspberry Pi](https://www.raspberrypi.org/) (a small and affordable computer). One such application described here further is [OctoPrint](https://octoprint.org/) (this may very well be done with other applications, but this needs to be checked first!), this print server application allows integration of many third party plug-ins next to the extensive feature set it already has out-of-the-box. One such feature is `GCODE scripts` (intently spelled this way to match the option in the Octoprint settings menu); this screenshot shows some details:

[![GCODE scripts menu options item](https://i.sstatic.net/FZIeq.png)](https://i.sstatic.net/FZIeq.png)

As can be seen from the image, there are specific "events" available to process G-code commands at specific event occurences like e.g. just before the print starts. You could use that envent to insert your leveling commands.

---

Please note that in the image you will find strange G-code commands like `OCTO100` and `OCTO110` which is a feature of the plugin called "GCODE System Commands" which allows running shell scripts to schedule the fan. I just kill the power to the fan when the printer is idle to get rid of the noise when the printer is just idling, the fan is only needed when the hotend is at elevated temperature.


---

### Answer by username_5 on 2018-08-25. Score: 4

With Marlin 1.1.0, you can automatically run a G-Code file when powering on the printer with a SD card already present.
Add a file named auto0.g at the root of your card, containing the following G-Code:
G28 ;Auto-homing
G29 ;Bed leveling

Normally the bed leveling map should be reused for all subsequent prints, until the printer is turned off.
It is possible to provide up to 10 files, from auto0.g to auto9.g.

> **Comments:**
> **username_6**: Would you like to post this (or a derivative thereof) as an answer to the question [Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?](https://3dprinting.stackexchange.com/questions/6778/can-g-code-scripts-be-run-automatically-on-inserting-an-sd-card-when-using-marli)  

---

### Answer by username_7 on 2018-08-26. Score: 4

Rather than placing the `G28` (Home) and `G29` (bed level) in the configuration, I would place it in the G-code generating slicer as pre-print code. This will automatically add this to the start of any G-code sliced, enforcing the homing and leveling whenever the G-code is run.


---

### Answer by username_9 on 2017-10-14. Score: 0

I'm not that fluent in G-code, but at [Modern Machine Shop: Understanding G27, G28, G29 and G30](https://www.mmsonline.com/columns/understanding-g27-g28-g29-and-g30) I found these descriptions of the `G28` and `G29` codes you added:

* `G28`: For any axis letter addresses included in the G28 command, the machine first will move (at rapid) to an intermediate position in those axes. Then, it will rapid to the zero return position in the commanded axes.
* `G29`: G29 is also a two-step command. First, it causes the machine to move (in the axes commanded) to the intermediate position used in the most recent `G28` command. Second, it causes the machine to move to the position included in the `G29` command.

So it sounds to me like you only need one of those two commands. It seems that the code you modified runs separately on startup, rather than for each separate print. I would first try your modification with each G command separately, so you can see exactly what the difference is on your hardware.

I don't know my way around the code involved, but it looks like you'd need to make the modification in another place, such as just before whatever loop sends successive commands to the printer.

Hope that's helpful despite my limited fluency...

> **Comments:**
> **username_2**: These descriptions you found relate to the G28/G29 commands on professional CNC machines. In 3D printer firmware, they have been (mis-)appropriated for other uses. G28 performs a homing sequence to the endstops (this kind of - but not really - corresponds to your description), while G29 initiates auto-bed leveling.  

---

