gwordal

Lesson 4 of 5 · 20 min

G-code and motion

When you press Print, the slicer's real output is not a 3D model but a plain text file of instructions, thousands of lines long, called G-code. The printer firmware reads it one line at a time and turns each line into stepper pulses and heater changes. Once you can read G-code you can debug a strange print by looking at what was actually commanded, write a test pattern by hand and understand why the printer's step settings matter.

What G-code looks like

Each line is one command: a letter and number, then parameters. Anything after a semicolon is a comment. The commands you will meet most often:

CommandMeaning
G28Home all axes: move to the endstops and set the position to zero
G90 / G91Absolute / relative positioning for X, Y and Z
M82 / M83Absolute / relative extrusion for E
G92 E0Set the current extruder position to 0 without moving
G0 X.. Y.. Z.. F..Rapid move, no extrusion (travel)
G1 X.. Y.. E.. F..Linear move, usually with extrusion
M104 S210Set the nozzle target to 210 C and carry on
M109 S210Set the nozzle target and wait until it is reached
M140 S60 / M190 S60Set the bed target, or set it and wait
M106 S255Fan on at full power (S is 0 to 255)

F is the feed rate in mm per minute, not per second, so F1200 is 20 mm/s and F6000 is 100 mm/s. It stays in effect until you change it. In practice G0 and G1 behave almost identically on modern firmware, and the slicer uses G0 for travel and G1 for printing by convention.

The E axis

Filament is treated as one more axis. In absolute extrusion mode (M82), E is the total length of filament, in millimetres, that has been pushed since the last G92 E0. A move that raises E deposits plastic, and a move that lowers it is a retraction, pulling the filament back to prevent oozing during travel.

How much E does a move need? The extruded line is a thin box, line width times layer height times length, and that volume must equal the filament volume pushed in:

E = (line width x layer height x length) / (pi x (1.75 / 2)^2) = (0.4 x 0.2 x length) / 2.405

That is 0.0333 mm of filament for every 1 mm of line, so a 20 mm line needs 0.665 mm of E. The ratio is called the extrusion multiplier, and the flow setting you will calibrate in Lesson 5 scales it directly.

A hand-written square

This program prints one 20 mm square outline, one layer high, with the same 0.4 mm line and 0.2 mm layer. The E values are cumulative:

G28                     ; home all axes
G90                     ; absolute XYZ
M82                     ; absolute E
M140 S60                ; bed to 60 C
M104 S210               ; nozzle to 210 C
M190 S60                ; wait for the bed
M109 S210               ; wait for the nozzle
G92 E0                  ; zero the extruder
G0 X10 Y10 Z0.2 F6000   ; travel to the start, first layer height
G1 X30 Y10 E0.665 F1200 ; side 1, 20 mm, 20 mm/s
G1 X30 Y30 E1.330       ; side 2
G1 X10 Y30 E1.995       ; side 3
G1 X10 Y10 E2.661       ; side 4, back at start
G1 E1.661               ; retract 1 mm
G0 Z5 F6000             ; lift the nozzle
M104 S0                 ; heater off
M140 S0                 ; bed off

Notice that the first print move comes after the heaters are fully on target, and that the first layer is 0.2 mm high. Nothing in this file is magic, and any printer can run it. If you paste it into a G-code viewer you will see a square.

Steps per millimetre

The firmware does not know about millimetres. It only knows motor steps. For every axis it needs a constant, the steps per millimetre, to convert a G-code position into a step count. For a belt-driven axis with a stepper:

steps/mm = (200 x microsteps) / (pulley teeth x belt pitch)

The 200 is the full steps per revolution of a 1.8 degree motor, microsteps is the driver's subdivision (commonly 16), the pulley teeth count is on the motor shaft, and the pitch is the distance between belt teeth.

Worked example, GT2 belt. GT2 belt has a 2 mm pitch. A common pulley has 20 teeth, so one revolution moves the belt 20 x 2 = 40 mm. With 16 microsteps the motor takes 200 x 16 = 3200 microsteps per revolution:

steps/mm = 3200 / 40 = 80

So the resolution is 1 / 80 = 0.0125 mm per microstep, and a 100 mm move is 8000 steps. At 200 mm/s travel the driver has to receive 16 000 step pulses per second, which is why a slow processor limits speed.

For a lead screw the denominator is the lead, the distance moved per revolution. A T8 screw with 8 mm lead gives 200 x 16 / 8 = 400 steps/mm on the Z axis, which is why Z moves are so precise and slow.

If the steps per mm value in the firmware is wrong, every dimension of the part is wrong by the same ratio, such as a 20 mm cube that measures 19.2 mm. Check it first when sizes are consistently off.

Parse some G-code yourself

The program below reads a G-code string, tracks position and extrusion line by line, and totals the travel distance (moves with no extrusion), the printed distance (moves that extrude), the filament used and a rough move time. It runs the square from above. Change a coordinate and re-run.

gcode_stats.py

The output is 33.09 mm of travel, 80.00 mm of printing (four 20 mm sides), 2.661 mm of filament, which is 6.40 mm3, and 4.33 s of motion. Check the volume against the line model: 80 mm x 0.4 mm x 0.2 mm = 6.4 mm3. The two agree, which confirms the E values were computed correctly. Real slicers also account for acceleration, so actual moves take longer than this ideal estimate.

Check yourself

A GT2 belt axis uses a 16 tooth pulley, 16 microsteps and a 1.8 degree motor. What are the steps per mm?

Check yourself

What does F1200 on a G1 line mean?