Skip to main content

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):

Command Explanation
G0 X100 Move X axis to absolute position 100mm
G1 X0 F100 Move X axis to 0 position at speed 100mm/min

Control GPIO pins

GRBL is designed for CNC machines and IO pins are used to make sense. Pin description is provided here, but for actual functionality source code should be inspected.

Command Explanation
M7 MIST = ON
M8 FLOOD = ON
M9 MIST = OFF, FLOOD = OFF
Command Explanation
M3 Set clockwise spindle rotation
S300 Set spindle control pin PWM duty cycle at 30% (max S = 1000)
M4 Set counter clockwise spindle rotation
M5 Turn spindle off

Motor homing

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

Command Explanation
$HX Initialize X axis

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.

Command Output
? <Idle|MPos:0.000,90.000,0.000,0.000|Bf:35,254|FS:0,0>

Read firmware version string

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

Command Output
$I [VER:1.1f-SCE2.20200405:]
[OPT:VMZHL,35,254]
ok

Probing

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

Command Explanation and Output
G38.2 F100 Z-100

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]

Idle / wait

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

Command Explanation
G4 P60000 Wait 1 minute, reply with ok when done

Read write parameters

Controller has quite a few parameters that can be set in EEPROM. See original description here.

Command Explanation and Output
$$

Read parameters

 

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

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

 

$132=100.00

Set single paramter with the new value

 

Replies with ok

Extra commands

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

Command Explanation and Output
M113 Control GPIO IO3, replies with ok
M114 Control GPIO IO4, replies with ok
M115 Control GPIO IO5, replies with ok
M116 Control GPIO IO6, replies with ok
M117 Control GPIO IO7, replies with ok
M120 Control GPIO LIM_EN, replies with ok