-
Notifications
You must be signed in to change notification settings - Fork 1
GCode Tutorial
G-Code ("Geometric Code") is the the programming language for CNC machines, controlling how and where the machine moves. G-Codes varies based on the machine type, make and model, but the basic principles are shared among current commercial models.
Most G-Codes are modal, meaning that once the G-Code is in effect it will remain so until you change it.
Let's take a look at this example line command:
G1 X-9.2 Y-5.42 Z0.5 F3000.0 E0.0377
G1: The command. In this case, G1 means a move. What follows are the instructions for the movement. X-9.2 Y-5.42 Z0.5: The arguments for the command. In this case, it is saying ‘Move the X 9.2 mm to the left, the Y 5.42 mm forward, and the Z .5 mm downward. F3000.0: An F code, referring to the motor frequency. In this case, the motor is working at 3000 mm/minute. This frequency, or Feed Rate, for the stepper motor may vary considerably over the course of the print. E0.0377: An E code, controlling the amount of extrusion. In this case no feed rate is given and the speed rate is taken at default.
The (preamble) start.gcode
The start.gcode file describes the initial conditions of every print and defines such necessities as homing behavior and coordinate systems. Some of the work done in the preamble may include:
- Setting units
- G20 for inches, G21 for mm
- Specifying the active plane
- G17 for XY plane (mill), G18 for XZ plane (lathe)
- Turning off problematic tool dimensions, etc.
- G40 turns off tool diameter compensation
- G49 turns off tool length compensation
- G80 turns off canned cycles
- Setting the coordinate system (usually the G54 coordinate system)
- Using Distance Mode to define how axis words are interpreted
- G90 specifies distance from the axis 0
- G91 specifies distance from current point
The following is an example of a typical start.gcode: G21; Sets units to mm G91 G1 X200 F3000 G1 Y200 F3000 G1 X-82 Y-76 F3000 G1 Z-200 F500 G1 Z5 F500 G1 Z-10 F250 G90; Set absolute coordinates G92 X0 Y0 Z0 E0; Reset extruder
Here, the command G21 sets units to mm while G91 switches the printer to relative positioning mode. These are necessary for the next G1 commands to work. G1 X200 F3000 G1 Y200 F3000
Here, the commands set in motion the homing sequence. The following commands does the ‘real work of homing’. They relate the relative distance from the end stops to the center position. They also send the Z axis home to its end stop. The final line backs the Z axis 10mm away from the end stop for a smooth ‘final flourish’. G1 X-82 Y-76 F3000 G1 Z-200 F500 G1 Z5 F500 G1 Z-10 F250
In the final two lines, G90 resets the printer to absolute positioning. G92 sets the printer back to relative positioning while setting all coordinates to 0.
; starts a comment
( surrounds a comment )
M101 - Extruder on, forward (see M227, M228, M229 for extruder/filament retraction)
M102 - Extruder on, reverse
M103 - Extruder off
M107 - Fan off (use M106 S0 instead)
A list of useful codes: A - "A axis" of machine B - "B axis" of machine C - "C axis" of machine D - Tool radius compensation number F - Sets feed rate H - Tool length offset index I - X axis offset for arc, or X offset in G87 canned cycle J - Y axis offset for arcs, or Y offset in G87 canned cycle K - Z axis offset for arcs, or Z offset in G87 canned cycle L - Number or repetitions in canned cycles/subroutines - L1/L2: tool offset settings/fixture offset (with G10) N - Line number O - Subroutine label number P - Dwell time in a canned cycle - Dwell time with G4 - Tool/fixture number (with G10) - Tool radius (with G41/G42) Q - Feed increment in G83 canned cycle - Repetitions of subroutine call R - Radius of arc - Canned cycle retract level S - Spindle speed T - Tool selection X - X axis of machine Y - Y axis of machine Z - Z axis of machine
G00 - Move at max speed (rapid move)
G01 - Move at feed rate (determined by F)
G0 -> G1
G02 - Makes machine move in circular clockwise motion
G03 - Makes machine move in circular, counter-clockwise motion
G1 - Coordinated Movement X Y Z E
G2 - CW ARC
G3 - CCW ARC
G4 - Dwell S or P
G7 - Sets lath diameter (preamble)
G8 - radius measurements for the X axis
G10 - retract filament according to settings of M207
G11 - retract recover filament according to settings of M208
G20 - Set units to inches
G21 - Set units to mm
G28 - Home all Axis
G90 - Use Absolute Coordinates
G91 - Use Relative Coordinates
G92 - Set current position to coordinates given
M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
M1 - Same as M0
M17 - Enable/Power all stepper motors
M18 - Disable all stepper motors; same as M84
M20 - List SD card
M21 - Init SD card
M22 - Release SD card
M23 - Select SD file (M23 filename.g)
M24 - Start/resume SD print
M25 - Pause SD print
M26 - Set SD position in bytes (M26 S12345)
M27 - Report SD print status
M28 - Start SD write (M28 filename.g)
M29 - Stop SD write
M30 - Delete file from SD (M30 filename.g)
M31 - Output time since last M109 or SD card start to serial
M32 - Select file and start SD print (Can be used when printing from SD card)
M42 - Change pin status via G-code Use M42 Px Sy to set pin x to value y,
when omitting Px the onboard led will be used.
M80 - Turn on Power Supply
M81 - Turn off Power Supply
M82 - Set E codes absolute (default)
M83 - Set E codes relative while in Absolute Coordinates (G90) mode
M84 - Disable steppers until next move, or use S to specify an inactivity timeout,
after which the steppers will be disabled. S0 to disable the timeout.
M85 - Set inactivity shutdown timer with parameter S. To disable set zero (default)
M92 - Set axis_steps_per_unit - same syntax as G92
M104 - Set extruder target temp
M105 - Read current temp
M106 - Fan on
M107 - Fan off
M109 - Sxxx Wait for extruder current temp to reach target temp.
Waits only when heating
Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
M114 - Output current position to serial port
M115 - Capabilities string
M117 - display message
M119 - Output Endstop status to serial port
M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
M140 - Set bed target temp
M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
M200 - Set filament diameter
M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) in
mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
M205 - advanced settings: minimum travel speed S=while printing T=travel only,
B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
M206 - set additional homeing offset
M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
M209 - S enable automatic retract detect if the slicer did not support G10/11: every normal
extrude-only move will be classified as retract depending on the direction.
M218 - set hotend offset (in mm): T X Y
M220 S- set speed factor override percentage
M221 S- set extrude factor override percentage
M240 - Trigger a camera to take a photograph
M280 - Position an RC Servo P S, ommit S to report back current angle
M300 - Play beepsound S P
M301 - Set PID parameters P I and D
M302 - Allow cold extrudes
M303 - PID relay autotune S sets the target temperature. (default target temperature = 150C)
M304 - Set bed PID parameters P I and D
M400 - Finish all moves
M500 - stores paramters in EEPROM
M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
M503 - print the current settings (from memory not from eeprom)
M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires
ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract]
L[later retract distance for removal]
M907 - Set digital trimpot motor current using axis codes.
M908 - Control digital trimpot directly.
M350 - Set microstepping mode.
M351 - Toggle MS1 MS2 pins directly.
M928 - Start SD logging (M928 filename.g) - ended by M29
M999 - Restart after being stopped by error
Resources: