<p>I don't know what you would like to achieve.</p>

<p>As @typo mentioned there is similar functionality which triggers at printer startup: <a href="https://github.com/MarlinFirmware/Marlin/wiki/Autostart" rel="nofollow noreferrer">https://github.com/MarlinFirmware/Marlin/wiki/Autostart</a></p>

<p>I found in Marlin's source code that you can enable that autostart feature to be triggered from menu. Just enable <code>MENU_ADDAUTOSTART</code> in <a href="https://github.com/MarlinFirmware/Marlin/blob/1.1.x/Marlin/Configuration_adv.h" rel="nofollow noreferrer">Configuration_adv.h</a>. </p>

<p>If you really need a g-code that start after sd-card insert then you have to adjust the marlin source code on your own. I suggest to alter file <a href="https://github.com/MarlinFirmware/Marlin/blob/1.1.x/Marlin/cardreader.cpp" rel="nofollow noreferrer">cardreader.cpp</a>. There you have to trigger the autostart procedure after a sd-card is inserted.</p>

CC BY-SA 4.0
2018-09-03T12:24:19.913Z
2018-09-03T12:24:19.913Z
amra
<p>I am not <em>entirely sure</em> if this is what you are after, or if it will work, but from  <a href="https://github.com/MarlinFirmware/Marlin/issues/2891#issuecomment-180980733" rel="nofollow noreferrer">this post</a> on <a href="https://github.com/MarlinFirmware/Marlin/issues/2891" rel="nofollow noreferrer">Printing From SD Card when printer is turned On</a></p>
<blockquote>
<p>While an LCD controller is not needed for auto#.g to work, if you have an LCD controller you can use the <code>MENU_ADDAUTOSTART</code> option to add a menu command that will run the auto0.g, auto1.g, auto2.g whenever you want.</p>
</blockquote>
<p>This (final) post ended up closing the thread so it appears to answer the OP's question - however, whether it will answer your question is another matter.</p>
<hr />
<p><a href="https://reprap.org/forum/read.php?415,833394" rel="nofollow noreferrer">This post</a>, on the Marlin forum, replicates your question and asks about purely insertion of an SD card on a printer which is <em>already</em> powered on.</p>
<p><a href="https://reprap.org/forum/read.php?415,833394,833489#msg-833489" rel="nofollow noreferrer">A reply</a> was received and apparently it should be possible by making a code change in <code>ultralcd.ccp</code>:</p>
<blockquote>
<p>It does not seem like an auto print, when inserting a SD-card, is to
be found in the current program code of Marlin.</p>
<p>You might consider making a feature request here: [<a href="https://github.com/MarlinFirmware/Marlin/issues" rel="nofollow noreferrer">github.com</a>]</p>
<p>I do not think that it would require much work to implement a
&quot;card.autoprintfile()&quot; function in Marlin. It seems to me that a call
to such a function could happen at line 5172 right after:</p>
<pre><code>else
LCD_MESSAGEPGM(MSG_SD_INSERTED);
</code></pre>
<p>in the file <code>ultralcd.ccp</code></p>
</blockquote>
<p>A subsequent feature request has been made on Marlin's GitHub, see <a href="https://github.com/MarlinFirmware/Marlin/issues/11815" rel="nofollow noreferrer">[FR] Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?</a></p>
<p>However, from <a href="https://github.com/MarlinFirmware/Marlin/issues/11815#issuecomment-420791259" rel="nofollow noreferrer">this reply</a>, it appears that a feature request is already pending:</p>
<blockquote>
<p>I'm not in favor of this as a general feature. We already have a
feature request to be able to run custom G-code in response to various
events, with SD card insertion being one of those events. Would that
feature serve your needs?</p>
</blockquote>
<p>However, as yet, I do not have a link to this other feature request. Will update as and when.</p>
<hr />
<p>As an aside, in might be worth mentioning that the SDFat related code could be the place to code this in, see <a href="https://forum.arduino.cc/index.php?topic=360754.0" rel="nofollow noreferrer">if SD card removed, how to flag and restart?</a>, although after looking at the Marlin codebase, in particular <a href="https://github.com/MarlinFirmware/Marlin/blob/1.1.x/Marlin/SdFatUtil.cpp" rel="nofollow noreferrer"><code>SdFatUtils.cpp</code></a>, this would appear to be a <a href="https://en.wikipedia.org/wiki/Red_herring" rel="nofollow noreferrer">red herring</a>.</p>

CC BY-SA 4.0
2018-08-31T22:52:14.413Z
2021-09-06T03:25:04.873Z
2021-09-06T03:25:04.873Z
Greenonline
<p>When running <a href="https://github.com/MarlinFirmware/Marlin" rel="nofollow noreferrer">Marlin Firmware</a>, is it possible to run <a href="https://reprap.org/wiki/G-code" rel="nofollow noreferrer">G-code</a> scripts/series of commands automatically when you insert the SD card?</p>

<hr>

<p>I'm running Marlin on a 3D printer board using an ATmega 2560 based board with a reprap discount LCD controller with an SD card slot. I would like to do this without the need to add another computer/board, so native from the board running the Marlin Firmware.</p>

CC BY-SA 4.0
2018-08-30T00:08:19.547Z
Not quite the answer, but close: https://github.com/MarlinFirmware/Marlin/wiki/Autostart.
typo
CC BY-SA 4.0
2018-09-11T10:20:36.183Z
Adding link to [Nikkoura's answer](https://3dprinting.stackexchange.com/questions/4256/how-can-i-add-auto-bed-leveling-before-print/6740#6740) and while that deals with power-on only, [this post](https://reprap.org/forum/read.php?415,833394) on the Marlin forum asks about purely insertion on a printer which is *already* powered on. [A reply](https://reprap.org/forum/read.php?415,833394,833489#msg-833489) was received and apparently it should be possible by making a code change in `ultralcd.ccp`
Greenonline
CC BY-SA 4.0
2018-09-12T20:10:16.627Z
A feature request has been made on Marlin's GitHub, see [Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?](https://github.com/MarlinFirmware/Marlin/issues/11815)
Greenonline
<marlin><g-code><firmware><scripts>
Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?
0scar
# Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?

(Asked by: username_0 on 2018-08-29)

When running [Marlin Firmware](https://github.com/MarlinFirmware/Marlin), is it possible to run [G-code](https://reprap.org/wiki/G-code) scripts/series of commands automatically when you insert the SD card?

---

I'm running Marlin on a 3D printer board using an ATmega 2560 based board with a reprap discount LCD controller with an SD card slot. I would like to do this without the need to add another computer/board, so native from the board running the Marlin Firmware.

**Tags:** marlin, g-code, firmware, scripts

> **Comments:**
> **username_1**: Not quite the answer, but close: https://github.com/MarlinFirmware/Marlin/wiki/Autostart.  
> **username_2**: Adding link to [Nikkoura's answer](https://3dprinting.stackexchange.com/questions/4256/how-can-i-add-auto-bed-leveling-before-print/6740#6740) and while that deals with power-on only, [this post](https://reprap.org/forum/read.php?415,833394) on the Marlin forum asks about purely insertion on a printer which is *already* powered on. [A reply](https://reprap.org/forum/read.php?415,833394,833489#msg-833489) was received and apparently it should be possible by making a code change in `ultralcd.ccp`  
> **username_2**: A feature request has been made on Marlin's GitHub, see [Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?](https://github.com/MarlinFirmware/Marlin/issues/11815)  

---

## 2 answers

### Answer by username_2 on 2018-08-31. Score: 5 | Accepted answer

I am not *entirely sure* if this is what you are after, or if it will work, but from [this post](https://github.com/MarlinFirmware/Marlin/issues/2891#issuecomment-180980733) on [Printing From SD Card when printer is turned On](https://github.com/MarlinFirmware/Marlin/issues/2891)

> While an LCD controller is not needed for auto#.g to work, if you have an LCD controller you can use the `MENU_ADDAUTOSTART` option to add a menu command that will run the auto0.g, auto1.g, auto2.g whenever you want.

This (final) post ended up closing the thread so it appears to answer the OP's question - however, whether it will answer your question is another matter.

---

[This post](https://reprap.org/forum/read.php?415,833394), on the Marlin forum, replicates your question and asks about purely insertion of an SD card on a printer which is *already* powered on.

[A reply](https://reprap.org/forum/read.php?415,833394,833489#msg-833489) was received and apparently it should be possible by making a code change in `ultralcd.ccp`:

> It does not seem like an auto print, when inserting a SD-card, is to
> be found in the current program code of Marlin.
>
> You might consider making a feature request here: [[github.com](https://github.com/MarlinFirmware/Marlin/issues)]
>
> I do not think that it would require much work to implement a
> "card.autoprintfile()" function in Marlin. It seems to me that a call
> to such a function could happen at line 5172 right after:
>
> ```
> else
> LCD_MESSAGEPGM(MSG_SD_INSERTED);
>
> ```
>
> in the file `ultralcd.ccp`

A subsequent feature request has been made on Marlin's GitHub, see [[FR] Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?](https://github.com/MarlinFirmware/Marlin/issues/11815)

However, from [this reply](https://github.com/MarlinFirmware/Marlin/issues/11815#issuecomment-420791259), it appears that a feature request is already pending:

> I'm not in favor of this as a general feature. We already have a
> feature request to be able to run custom G-code in response to various
> events, with SD card insertion being one of those events. Would that
> feature serve your needs?

However, as yet, I do not have a link to this other feature request. Will update as and when.

---

As an aside, in might be worth mentioning that the SDFat related code could be the place to code this in, see [if SD card removed, how to flag and restart?](https://forum.arduino.cc/index.php?topic=360754.0), although after looking at the Marlin codebase, in particular [`SdFatUtils.cpp`](https://github.com/MarlinFirmware/Marlin/blob/1.1.x/Marlin/SdFatUtil.cpp), this would appear to be a [red herring](https://en.wikipedia.org/wiki/Red_herring).


---

### Answer by username_3 on 2018-09-03. Score: 1

I don't know what you would like to achieve.

As @username_1 mentioned there is similar functionality which triggers at printer startup: <https://github.com/MarlinFirmware/Marlin/wiki/Autostart>

I found in Marlin's source code that you can enable that autostart feature to be triggered from menu. Just enable `MENU_ADDAUTOSTART` in [Configuration\_adv.h](https://github.com/MarlinFirmware/Marlin/blob/1.1.x/Marlin/Configuration_adv.h).

If you really need a g-code that start after sd-card insert then you have to adjust the marlin source code on your own. I suggest to alter file [cardreader.cpp](https://github.com/MarlinFirmware/Marlin/blob/1.1.x/Marlin/cardreader.cpp). There you have to trigger the autostart procedure after a sd-card is inserted.


---

