# Some g-code examples and recipes

G-code while being universal machine control language has various flavors and capabilities. Below section is dedicated to speed up manual machine control with simple commands.

#### Move motors

There are two basic linear motion commands G0 and G1. Both are generally the same just G0 uses default max speed constant from configuration. Examples (assume linear actuator is connected):

<table border="1" id="bkmrk-command-explanation-" style="border-collapse: collapse; width: 100%;"><tbody><tr><td class="align-center" style="width: 15.5556%;">**Command**</td><td class="align-center" style="width: 84.4444%;">**Explanation**</td></tr><tr><td style="width: 15.5556%;">`G0 X100`</td><td style="width: 84.4444%;">Move X axis to absolute position 100mm</td></tr><tr><td style="width: 15.5556%;">`G1 X0 F100`</td><td style="width: 84.4444%;">Move X axis to 0 position at speed 100mm/min</td></tr></tbody></table>

#### Control GPIO pins

GRBL is designed for CNC machines and IO pins are used to make sense. Pin description is provided [here](https://wiki.kurokesu.com/books/sce2/page/pinout), but for actual functionality source code should be inspected.

<table border="1" id="bkmrk-command-explanation--0" style="border-collapse: collapse; width: 100%;"><tbody><tr><td class="align-center" style="width: 15.5556%;">**Command**</td><td class="align-center" style="width: 84.4444%;">**Explanation**</td></tr><tr><td style="width: 15.5556%;">`M7`</td><td style="width: 84.4444%;">MIST = ON</td></tr><tr><td style="width: 15.5556%;">`M8`</td><td style="width: 84.4444%;">FLOOD = ON</td></tr><tr><td style="width: 15.5556%;">`M9`</td><td style="width: 84.4444%;">MIST = OFF, FLOOD = OFF</td></tr></tbody></table>

<table border="1" id="bkmrk-command-explanation--1" style="border-collapse: collapse; width: 100%; height: 145px;"><tbody><tr style="height: 29px;"><td class="align-center" style="width: 15.5556%; height: 29px;">**Command**</td><td class="align-center" style="width: 84.4444%; height: 29px;">**Explanation**</td></tr><tr style="height: 29px;"><td style="width: 15.5556%; height: 29px;">`M3`</td><td style="width: 84.4444%; height: 29px;">Set clockwise spindle rotation</td></tr><tr style="height: 29px;"><td style="width: 15.5556%; height: 29px;">`S300`</td><td style="width: 84.4444%; height: 29px;">Set spindle control pin PWM duty cycle at 30% (max S = 1000)</td></tr><tr style="height: 29px;"><td style="width: 15.5556%; height: 29px;">`M4`</td><td style="width: 84.4444%; height: 29px;">Set counter clockwise spindle rotation</td></tr><tr style="height: 29px;"><td style="width: 15.5556%; height: 29px;">`M5`</td><td style="width: 84.4444%; height: 29px;">Turn spindle off</td></tr></tbody></table>

#### Motor homing

After powering controller on it does not know motor position, thus it should be driven to home reference position

<table border="1" id="bkmrk-command-explanation--2" style="border-collapse: collapse; width: 100%; height: 145px;"><tbody><tr style="height: 29px;"><td class="align-center" style="width: 15.5556%; height: 29px;">**Command**</td><td class="align-center" style="width: 84.4444%; height: 29px;">**Explanation**</td></tr><tr style="height: 29px;"><td style="width: 15.5556%; height: 29px;">`$HX`</td><td style="width: 84.4444%; height: 29px;">Initialize X axis</td></tr></tbody></table>

#### Read controller status

Almost any time controller status can be read with command `?`. It will report motor status (Idle/Run), actual positions and some other info.

<table border="1" id="bkmrk-command-output-%3F-%3Cid" style="border-collapse: collapse; width: 100%; height: 145px;"><tbody><tr style="height: 29px;"><td class="align-center" style="width: 15.5556%; height: 29px;">**Command**</td><td class="align-center" style="width: 84.4444%; height: 29px;">**Output**</td></tr><tr style="height: 29px;"><td style="width: 15.5556%; height: 29px;">`?`</td><td style="width: 84.4444%; height: 29px;">`<Idle|MPos:0.000,90.000,0.000,0.000|Bf:35,254|FS:0,0>`</td></tr></tbody></table>

#### Read firmware version string

Functionality between versions will vary, some firmware versions or branches can have special functionality.

<table border="1" id="bkmrk-command-output-%24i-%5Bv" style="border-collapse: collapse; width: 100%; height: 145px;"><tbody><tr style="height: 29px;"><td class="align-center" style="width: 15.5556%; height: 29px;">**Command**</td><td class="align-center" style="width: 84.4444%; height: 29px;">**Output**</td></tr><tr style="height: 29px;"><td style="width: 15.5556%; height: 29px;">`$I`</td><td style="width: 84.4444%; height: 29px;">`[VER:1.1f-SCE2.20200405:]`  
`[OPT:VMZHL,35,254]`  
`ok`</td></tr></tbody></table>

#### Probing

Firmware allows use of touch trigger probes. Moves down Z axis and stops on when probe pin is triggered, then reports collision point.

<table border="1" id="bkmrk-command-explanation--3" style="border-collapse: collapse; width: 100%;"><tbody><tr><td class="align-center" style="width: 19.6296%;">**Command**</td><td class="align-center" style="width: 80.3704%;">**Explanation and Output**</td></tr><tr><td style="width: 19.6296%;">`G38.2 F100 Z-100`</td><td style="width: 80.3704%;">Move down Z axis until PROBE pin is triggered

- F - Speed
- Z - Target depth

Replies with `[PRB:1.503,0.000,-22.860,0.000:1]`

</td></tr></tbody></table>

#### Idle / wait

Idle command is useful for certain operations where controller needs to wait and do nothing (for example wait till spindle speeds up).

<table border="1" id="bkmrk-command-explanation--4" style="border-collapse: collapse; width: 100%; height: 145px;"><tbody><tr style="height: 29px;"><td class="align-center" style="width: 15.5556%; height: 29px;">**Command**</td><td class="align-center" style="width: 84.4444%; height: 29px;">**Explanation**</td></tr><tr style="height: 29px;"><td style="width: 15.5556%; height: 29px;">`G4 P1.5`</td><td style="width: 84.4444%; height: 29px;">Wait 1.5 seconds, reply with `ok` when done</td></tr></tbody></table>

#### Read write parameters

Controller has quite a few parameters that can be set in EEPROM. See original description [here](https://github.com/gnea/grbl/blob/master/doc/markdown/settings.md).

<table border="1" id="bkmrk-command-explanation--5" style="border-collapse: collapse; width: 100%;"><tbody><tr><td class="align-center" style="width: 19.6296%;">**Command**</td><td class="align-center" style="width: 80.3704%;">**Explanation and Output**</td></tr><tr><td style="width: 19.6296%;">`$$`</td><td style="width: 80.3704%;">Read parameters

`$0=6`  
`$1=255`  
`$2=0`  
`$3=31`

`...`  
`$132=360.000`  
`$133=360.000`  
`ok`

</td></tr><tr><td style="width: 19.6296%;">`$132=100.00`</td><td style="width: 80.3704%;">Set single paramter with the new value

Replies with `ok`

</td></tr></tbody></table>

#### Extra commands

Extra GPIO control commands used in motorized zoom lenses (L084, L0117, ...).

<table border="1" id="bkmrk-command-explanation--6" style="border-collapse: collapse; width: 100%;"><tbody><tr><td class="align-center" style="width: 19.6296%;">**Command**</td><td class="align-center" style="width: 80.3704%;">**Explanation and Output**</td></tr><tr><td style="width: 19.6296%;">`M113 Px`</td><td style="width: 80.3704%;">Control GPIO IO3, replies with `ok`</td></tr><tr><td style="width: 19.6296%;">`M114 Px`</td><td style="width: 80.3704%;">Control GPIO IO4, replies with `ok`</td></tr><tr><td style="width: 19.6296%;">`M115 Px`</td><td style="width: 80.3704%;">Control GPIO IO5, replies with `ok`</td></tr><tr><td style="width: 19.6296%;">`M116 Px`</td><td style="width: 80.3704%;">Control GPIO IO6, replies with `ok`</td></tr><tr><td style="width: 19.6296%;">`M117 Px`</td><td style="width: 80.3704%;">Control GPIO IO7, replies with `ok`</td></tr><tr><td style="width: 19.6296%;">`M120 Px`</td><td style="width: 80.3704%;">Control GPIO LIM\_EN, replies with `ok`</td></tr></tbody></table>

Where X is 0 or 1