Skip to content
depthue

What Is G-code? The Language 3D Printers, CNC Routers and Lasers Run

Definition

G-code is the plain-text command language that 3D printers, CNC routers and many lasers run line by line: each line tells the machine where to move, how fast, and what to switch on, such as a heater, a spindle or the laser. Slicers, CAM programs and laser software write it for one machine, because each firmware reads its own dialect.

Also called: G code, gcode, RS-274, .gcode file.

G-code is what your machine actually runs. You rarely write it yourself: a slicer writes it for a 3D printer, a CAM program for a CNC router, and laser software such as LightBurn for a laser. Each file is written for one machine. To see what a file will do, drop it into the free G-code viewer and watch every move drawn in 3D.

What a line of G-code says

A line of G-code is a row of words, and each word is a letter followed by a number. NIST's RS274/NGC specification, written for CNC machine tools, defines a word as "a letter other than N followed by a real value". The letters you will meet most:

  • G: the kind of move or mode. G1 is a straight move.
  • M: a machine function. M104 sets the nozzle temperature on a printer, and M3 starts a spindle or a laser.
  • X, Y, Z: where to go.
  • F: the feed rate, in units per minute.
  • S: a value for the command: a temperature, a spindle speed or a laser power.
  • E: filament, on a 3D printer.

A line, which NIST calls a block, "may include commands to a machining center to do several different things". So G1 X20 Y10 F1800 is one line that says: move straight, to X 20 and Y 10, at 1,800 mm per minute. Comments start after a semicolon on 3D printers, as the RepRap wiki describes, and sit inside round brackets in NIST's CNC language.

A real file, line by line

These are the first commands of Depthue's own sample print, a 20 mm test block written for a generic Marlin 2 or Klipper printer, with our notes in place of the file's comments. The Sample print button in the G-code viewer opens it. Our notes follow Marlin's documentation for each command.

M140 S60     ; set the bed to 60 °C and carry on
M104 S160    ; set the nozzle to 160 °C and carry on
G21          ; units are millimetres
G90          ; positions are absolute
M83          ; but filament (E) moves are relative
G28          ; home all axes
M190 S60     ; wait until the bed reaches 60 °C
M109 S210    ; wait until the nozzle reaches 210 °C
M73 P0 R27   ; progress 0 percent, 27 minutes left
G92 E0       ; call the current filament position zero
;TYPE:Custom ; a label for the slicer, ignored by the printer
; purge line ; the file's own comment, also ignored
G1 Z5 F3000  ; move straight up to Z 5 mm at 3,000 mm per minute

The 160 °C on the second line is a preheat: the file's own comment says it warms the nozzle "without oozing" while the bed heats, and M109 brings it to printing temperature once the bed is ready.

The commands you will see most

Motion and modes work on all three machines, with a few differences that catch people out:

CodeWhat it doesWhere it differs
G0Move without workingOn Marlin printers it is the same move as G1: Marlin's docs say that for Cartesian and delta printers G0 "is (and must be) a direct alias for G1". On a CNC it is a rapid move at the machine's set top speed (LinuxCNC). On a GRBL laser, the laser switches off for it
G1Straight move at feed rate FA printer extrudes on the way when E is given
G2, G3Clockwise and counterclockwise arcs, with I and J for the centre or R for the radiusMarlin needs arc support built in, and Klipper needs a gcode_arcs section in its config
G20, G21Inches or millimetres
G90, G91Absolute or relative positionsOn Marlin, G90 makes filament moves absolute too, unless M83 says otherwise
FFeed rate, in units per minute

Machine functions differ by machine:

CodeCNC router or millLaser on GRBL
M3Spindle on, clockwiseLaser on at constant power
M4Spindle on, counterclockwiseLaser on at dynamic power
M5Spindle offLaser off
SSpindle speedLaser power

On a 3D printer, Marlin's codes run the heaters and the fan: M104 sets the nozzle temperature and carries on while M109 waits for it, M140 and M190 do the same for the bed, M106 turns on the part cooling fan, and M82 and M83 make filament moves absolute or relative. M600 runs a filament change, the command behind colour swaps on a single-nozzle printer.

Who writes G-code for each machine

3D printers: the slicer

A slicer such as PrusaSlicer, OrcaSlicer, Bambu Studio or UltiMaker Cura turns an STL or 3MF file into G-code for your printer's profile. The profile includes the firmware flavour: PrusaSlicer offers 13, from RepRap/Sprinter and Marlin 2 to Klipper and Mach3/LinuxCNC, while Bambu Studio offers two, Marlin and Klipper. PrusaSlicer's tooltip asks you to set it to your printer's firmware "to get a compatible output".

CNC routers and mills: CAM and a post-processor

In Tormach's words, "CAM software, with the help of a post-processor, converts the tool paths that you create into a language that the machine can understand." The post-processor is what makes the file fit one controller.

Lasers: LightBurn or LaserGRBL

LightBurn writes G-code for lasers with GRBL, Smoothieware or Marlin controllers: on those, its save button reads Save GCode. LaserGRBL, a free and open-source program for Windows, works with any engraver based on Grbl v0.9 or v1.1. Which program fits which machine is in laser engraving software.

Not every laser runs G-code

On a Ruida controller, LightBurn's save button reads Save RD File; on TopWisdom it is Save OUT File, and on Trocen Save UD5 File. Those controllers take their own job files. Galvo lasers get no saved machine file at all: LightBurn's docs say that option "is not available for Galvo lasers".

Why a G-code file only fits one machine

NIST said it in 2000: "Implementations of the language differ from machine to machine, however, and a program that runs on one machine probably will not run on one from a different maker." That is still true:

  • Dialects. PrusaSlicer's flavour setting warns that "Some G/M-code commands, including temperature control and others, are not universal." Klipper says outright: "It is not a goal to support every possible G-Code command", and it adds its own readable commands.
  • Same code, different meaning. G0 is a rapid move on a CNC and an ordinary move on a Marlin printer. Arcs work only where the firmware allows them.
  • One setup baked in. A printer's file also carries its bed size, its temperatures and the filament it was sliced for.

One r/3Dprinting user who downloaded G-code made for a Cartesian bed printer, and owned a CoreXY Flashforge, got the right answer in the thread: G-code "rarely works for more than one machine unless it's the same model, same conditions and same material." The fix is to get the STL, 3MF or STEP file and slice it for your own printer.

G-code on a laser

The Grbl v1.1 Laser Mode wiki sets out how a GRBL laser reads G-code, with laser mode switched on in setting $32:

  • M3, constant power: it "keeps the laser power as programmed, regardless if the machine is moving, accelerating, or stopped."
  • M4, dynamic power: the power follows the speed, and "When not in motion, M4 dynamic mode turns off the laser."
  • S0 turns the laser off.
  • G0 switches the laser off by design: "G0 enforces the laser to be disabled automatically." So G0 M3 S1000 does not fire the laser; it waits for the next G1.

S is not a percentage everywhere. On GRBL, full power is the S value set in $30, and the wiki's sample settings use 1000. Check your controller before you copy S values from someone else's file. For power and speed by material, start from the laser and CNC settings chart.

How to read or check a G-code file

G-code is plain text, so any editor opens it, but a column of coordinates says little until you see it drawn. Slicers help by adding comments: Prusa's knowledge base notes that "The G-code itself does not contain all the information", so it is "usually augmented with slicer specific comments" for layers, features and time.

The free G-code viewer below reads those comments from Cura, PrusaSlicer, Bambu Studio and OrcaSlicer files, and it reads CNC and laser jobs too:

  • Files: .gcode, .nc, .ngc, .tap and .gc, or a Bambu Studio or OrcaSlicer plate exported as .gcode.3mf. PrusaSlicer's binary .bgcode files need converting to plain G-code in PrusaSlicer first.
  • Machine: it works out whether the file is for a 3D printer, a CNC or a laser, and you can change it.
  • Moves: step through the layers, show one layer at a time, and see the G-code line behind each move.
  • Numbers: print time, filament, travel and retractions for a print; rapids, passes and the highest S value for a CNC or laser job. Print time is the slicer's estimate when the file carries one, and otherwise a lower bound worked out from the feed rates.

The file is read in your browser and never uploaded.

To price a print, put the viewer's time and grams into the 3D print cost calculator.

G-code is a path, not a model

G-code stores the path of the nozzle or the cutter, not the surface of the model, so any mesh rebuilt from it is a rough guess. Keep the original STL, 3MF or STEP file. If the model itself is wrong, fix it before you slice, for example in the free STL repair tool, and slice again.

Questions

What does G-code do?

It tells a machine, one line at a time, where to move, how fast, and what to switch on: the heaters and the fan on a 3D printer, the spindle on a CNC router, the laser on an engraver.

How do I convert a file to G-code?

Open the model in software made for your machine: a slicer such as PrusaSlicer, OrcaSlicer, Bambu Studio or Cura for a 3D printer, CAM with a post-processor for a CNC, or LightBurn or LaserGRBL for a GRBL laser. Pick your machine's profile so the file uses its dialect.

What are G and M codes?

G codes set motion and modes, such as G0 and G1 for straight moves, G2 and G3 for arcs and G90 for absolute positions. M codes switch machine functions, such as M3 for spindle or laser on, M5 for off and M104 for the nozzle temperature.

Can you give me an example of a G-code?

G1 X20 Y10 F1800 moves in a straight line to X 20, Y 10 at 1,800 mm per minute. On a 3D printer, adding E0.5 pushes 0.5 mm of filament on the way.

Do CNC machines and lasers run on G-code?

NIST notes that most NC machine tools can run RS274 programs. CAM software and a post-processor usually write them, as Tormach explains. Lasers on GRBL, Smoothieware or Marlin controllers run G-code too, while Ruida, TopWisdom and Trocen controllers take their own job files, and galvo lasers run from LightBurn without a saved G-code file.

Can I use G-code made for a different printer?

Usually not. The file carries that printer's dialect, bed size and temperatures. Get the STL or 3MF and slice it for your own printer.

How do I open a G-code file?

Any text editor shows the commands. To see the toolpath, drop it into the free Depthue G-code viewer, which runs in your browser and never uploads the file, or use your slicer's preview.

Can I convert G-code back to STL?

Not reliably. G-code stores the path of the nozzle, not the surface of the model, so any mesh rebuilt from it is a rough guess. Keep the original STL or 3MF.

Related terms