<ol start="0">
<li>Choose a code in the &gt;10,000 in case new codes are added. But in this example I will choose 13</li>
<li>Navigate to 'src' folder of Marlin</li>
<li>Edit the file <code>gcode.cpp</code> around line 223 to have a new unused number. For example, this will create a  new G code function for the label <code>G13</code>.</li>
</ol>
<pre><code>  ...
  // Handle a known G, M, or T
  switch (parser.command_letter) {
    case 'G': switch (parser.codenum) {
      case 13: G13(); break;
      case 0: case 1: G0_G1(  
  ...
</code></pre>
<ol start="3">
<li><p>On line 375 of <code>gcode.h</code> add:  <code> static void G13();</code> to declare it.</p>
</li>
<li><p>In my case i was reading values from an analog system. So I went to <code>src/temperatures</code> and copied <code>M105.cpp</code> to be <code>G13.cpp</code>. Then inside the file I replaced <code>GcodeSuite::M105</code> to be <code>GcodeSuite::G13</code>. I am using this to take in the weight of something using a [scale][1] but for now I just want to test functionality so here is my test function:</p>
</li>
</ol>
<pre><code>
void GcodeSuite::G13() {

  SERIAL_ECHOPGM(MSG_OK);
  SERIAL_ECHOLNPGM(&quot;here is where weights are broadcast&quot;);

}
</code></pre>
<p>And again this is the only part I changed in my new copy of M105.cpp (a new file named G13.cpp). There is still more stuff in the file than just these few lines.</p>
<ol start="5">
<li><p>Upload to board</p>
</li>
<li><p>When going to octoprint and typing in <code>G13</code> I get:</p>
</li>
</ol>
<pre><code>
Send: G13
Recv: okhere is where weights are broadcast
</code></pre>
<p>A bit more work can be done to make it look nice, but this was the hard part.
[1]: <a href="https://www.instructables.com/id/Arduino-Bathroom-Scale-With-50-Kg-Load-Cells-and-H/" rel="nofollow noreferrer">https://www.instructables.com/id/Arduino-Bathroom-Scale-With-50-Kg-Load-Cells-and-H/</a></p>

CC BY-SA 4.0
2020-05-20T17:09:33.417Z
2020-11-26T23:23:23.953Z
2020-11-26T23:23:23.953Z
K Mmmm
<p>The code in 2.0.x is similar to the old branch 1.1.x, G-code is parsed in <a href="https://github.com/MarlinFirmware/Marlin/blob/2.0.x/Marlin/src/gcode/gcode.cpp" rel="nofollow noreferrer"><code>gcode.cpp</code></a>, specifically in <code>process_parsed_command</code>:</p>

<pre><code>void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/)
</code></pre>

<p>In the case statement the codes read from the G-code files are parsed (interpreted) and the appropriate method is called (e.g. <code>G28()</code> calls <a href="https://github.com/MarlinFirmware/Marlin/blob/2.0.x/Marlin/src/gcode/calibrate/G28.cpp" rel="nofollow noreferrer"><code>void GcodeSuite::G28()</code></a>)</p>

<p>If you want to create your own codes, it could be an idea to start there. Also think of using a different letter and/or codes in the 10,000 range so that it will not collide with new implemented G-codes.</p>

CC BY-SA 4.0
2020-05-19T18:42:46.460Z
2020-05-19T18:42:46.460Z
0scar
<p><a href="https://3dprinting.stackexchange.com/questions/3586/adding-custom-m-codes-to-marlin">Adding custom M Codes to Marlin</a> doesn't work for Marlin 2.0</p>

<p>How would one go about adding custom G codes or M Codes to Marlin 2.0? The Marlin_main.cpp file does not exist. </p>

<p>In general for Marlin 2.0, things are organized better, but split into more files. </p>

<marlin>
Adding custom M or G codes to Marlin 2.0
K Mmmm
# Adding custom M or G codes to Marlin 2.0

(Asked by: username_0 on 2020-05-19)

[Adding custom M Codes to Marlin](https://3dprinting.stackexchange.com/questions/3586/adding-custom-m-codes-to-marlin) doesn't work for Marlin 2.0

How would one go about adding custom G codes or M Codes to Marlin 2.0? The Marlin\_main.cpp file does not exist.

In general for Marlin 2.0, things are organized better, but split into more files.

**Tags:** marlin


---

## 2 answers

### Answer by username_0 on 2020-05-20. Score: 1 | Accepted answer

0. Choose a code in the >10,000 in case new codes are added. But in this example I will choose 13
1. Navigate to 'src' folder of Marlin
2. Edit the file `gcode.cpp` around line 223 to have a new unused number. For example, this will create a new G code function for the label `G13`.

```
  ...
  // Handle a known G, M, or T
  switch (parser.command_letter) {
    case 'G': switch (parser.codenum) {
      case 13: G13(); break;
      case 0: case 1: G0_G1(  
  ...

```

3. On line 375 of `gcode.h` add:  `static void G13();` to declare it.
4. In my case i was reading values from an analog system. So I went to `src/temperatures` and copied `M105.cpp` to be `G13.cpp`. Then inside the file I replaced `GcodeSuite::M105` to be `GcodeSuite::G13`. I am using this to take in the weight of something using a [scale][1] but for now I just want to test functionality so here is my test function:

```

void GcodeSuite::G13() {

  SERIAL_ECHOPGM(MSG_OK);
  SERIAL_ECHOLNPGM("here is where weights are broadcast");

}

```

And again this is the only part I changed in my new copy of M105.cpp (a new file named G13.cpp). There is still more stuff in the file than just these few lines.

5. Upload to board
6. When going to octoprint and typing in `G13` I get:

```

Send: G13
Recv: okhere is where weights are broadcast

```

A bit more work can be done to make it look nice, but this was the hard part.
[1]: <https://www.instructables.com/id/Arduino-Bathroom-Scale-With-50-Kg-Load-Cells-and-H/>


---

### Answer by username_1 on 2020-05-19. Score: 2

The code in 2.0.x is similar to the old branch 1.1.x, G-code is parsed in [`gcode.cpp`](https://github.com/MarlinFirmware/Marlin/blob/2.0.x/Marlin/src/gcode/gcode.cpp), specifically in `process_parsed_command`:

```
void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/)

```

In the case statement the codes read from the G-code files are parsed (interpreted) and the appropriate method is called (e.g. `G28()` calls [`void GcodeSuite::G28()`](https://github.com/MarlinFirmware/Marlin/blob/2.0.x/Marlin/src/gcode/calibrate/G28.cpp))

If you want to create your own codes, it could be an idea to start there. Also think of using a different letter and/or codes in the 10,000 range so that it will not collide with new implemented G-codes.


---

