3dSynth

Tutorials / Custom G-code

Open in the interactive tutorials reader

---
title: Tutorial 1 What is G-code
description: Introduction to the language of machine motion.
---

Tutorial 1 What is G-code

1. Lesson Header

2. Concept Introduction (Why It Matters)

Purpose: Understand that G-code is just a text file with coordinates.

Most people think 3D printing is magic. You press "Print," and an object appears. But under the hood, there is no magic—only coordinates.

G-code is the language of motion. It tells a machine exactly where to go, how fast to get there, and what to do along the way (like squirting out plastic).

Think of G-code like a text message to your printer.
- Slicers (like Cura or PrusaSlicer) are just translators. They take a 3D model (shapes) and turn it into G-code (paths).
- Your Printer is a dumb robot. It doesn't know what a "cube" is. It only knows "Move to X:10, Y:10."

When you learn G-code, you stop relying on the translator and start speaking directly to the robot. This gives you control that no slicer can offer.

3. Machine State Explanation

Before we write a single line, we must agree on the "State" of the machine.

A 3D printer is a state machine. If you tell it "Heat to 200°C," it stays at 200°C until you tell it otherwise. If you tell it "Move in Millimeters," it stays in millimeters forever (or until you restart it).

For this lesson, we assume:
- Units: Millimeters (standard for 3D printing).
- Positioning: Absolute (we will explain this shortly).

4. Command Breakdown

G-code commands usually start with a letter (G or M) followed by a number.

The Comment (;)

The most important character for humans. The machine ignores everything after a semicolon. - Syntax: ; This is a comment - Use: Write notes to yourself.

The Move Command (G1 or G0)

Tells the machine to move to a specific coordinate. - Syntax: G1 X[value] Y[value] - Example: G1 X10 Y20 (Move to X=10mm, Y=20mm) - Modal: Yes. If you write G1 X10 and then on the next line write X20, the machine remembers you are still using G1. - Travel vs. Extrusion: If you omit the E value, the move is a travel move—the nozzle moves but no plastic is extruded. To extrude, you add E[value] (covered in Lesson 4).

5. Minimal Working Example

Here is the smallest valid G-code program that moves the nozzle.

; My First G-code
G21        ; Set units to Millimeters
G90        ; Set positioning to Absolute
G1 X0 Y0   ; Move to the origin (travel)
G1 X50 Y50 ; Move diagonally to 50,50 (travel)

Line-by-Line:
1. ; My First G-code: A note for us. Machine ignores it.
2. G21: "Robot, use millimeters."
3. G90: "Robot, use the graph paper's grid (Absolute coordinates)."
4. G1 X0 Y0: "Go to the starting corner." (Travel—no E value, so no extrusion.)
5. G1 X50 Y50: "Go to the point where X is 50 and Y is 50." (Travel—no extrusion.)

What you will see:
- In 3DSynth preview: The toolpath shows a diagonal line from (0,0) to (50,50). It appears as a travel move (typically grey or dashed).
- On a real printer: The nozzle moves from origin to (50,50), but no plastic is extruded. You see motion, not a printed line. Extrusion requires an E value—we cover that in Lesson 4.

Running in 3DSynth

When you paste this into the Custom G-code module, 3DSynth wraps it with your printer's start G-code (heat, home, purge line) and end G-code (cooldown, park). Your code runs in the middle. The nozzle may already be at a different position when your first G1 X0 Y0 runs—that's fine. Your commands still move it to the coordinates you specify.

6. Visual Representation

Imagine a piece of graph paper. The bottom-left corner is 0,0.

Interactive preview is available in the interactive reader.

The machine moves in a straight line from start to finish. This is a travel path—no extrusion.

7. Build Exercise

Task: Write a G-code file that moves the nozzle in a triangle shape (travel only).

  1. Start at 0,0.
  2. Move to X0 Y50.
  3. Move to X50 Y0.
  4. Return to 0,0.

Don't scroll down yet! Try writing it out on paper or in a text editor.

...

Solution:

G21 ; Millimeters
G90 ; Absolute positioning
G1 X0 Y0   ; Start
G1 X0 Y50  ; Move up (travel)
G1 X50 Y0  ; Move right/down (travel)
G1 X0 Y0   ; Close the triangle (travel)

8. Deep Insight Section

Why Absolute (G90) vs. Relative (G91)?

In the example above, we used G90 (Absolute). This means X50 refers to the specific grid line "50" on the graph paper.

If we used G91 (Relative), X50 would mean "Move 50mm to the right from where you are now."

Slicers mostly use Absolute for movement because if one command fails or is skipped, the next command still sends the printer to the correct location. In Relative mode, one error ruins the rest of the print because the error accumulates.

9. Common Failure Modes

  1. Missing G21 (Units): Some machines default to Inches (G20). If you write X50 expecting 50mm (2 inches) but get 50 inches, your printer will crash into the wall. Always set safety defaults.
  2. No Feedrate (Speed): We didn't set speed (F) in this lesson yet. Some machines won't move if they don't know how fast to go. (We cover this in Lesson 3).
  3. Expecting extrusion without E: If you expect a printed line but only see the nozzle move, you're doing travel moves. Add an E value to extrude—see Lesson 4.
  4. Decimal Points: X10 and X10.0 are usually the same, but some very old industrial CNC machines require the decimal. Modern 3D printers are forgiving.

10. Real-World Application

Open any .gcode file you have ever sliced. Open it in a text editor (Notepad, TextEdit, VS Code).
Look at the first 20 lines. You will see:
- M104 / M140 (Heaters - Lesson 8)
- G28 (Home - Lesson 2)
- G1 commands (Movement—with or without E for extrusion)

You can now read the "Matrix." You verify that the file actually tells the printer to do what you want.

11. Final Clean Version

Save this as lesson1.gcode or paste it into 3DSynth's Custom G-code module.

; Lesson 1 - The Triangle (travel only)
; Setup
G21        ; Millimeters
G90        ; Absolute Positioning

; The Path (no E = travel, no extrusion)
G1 X0 Y0   ; Home
G1 X0 Y50  ; Point A
G1 X50 Y0  ; Point B
G1 X0 Y0   ; Return to Start

12. Stretch Challenge

Challenge: Can you draw a square using Relative positioning (G91)?

Hint: You start at 0,0. To go up, you add Y. To go right, you add X. To go down... you subtract Y.

Answer Key:

G21
G91        ; Relative Mode!
G1 X0 Y50  ; Move UP 50
G1 X50 Y0  ; Move RIGHT 50
G1 X0 Y-50 ; Move DOWN 50 (Negative!)
G1 X-50 Y0 ; Move LEFT 50 (Negative!)