G-code is the language the 3D printer uses to receive movement, temperature and extrusion instructions. Normally it is the slicer that generates it automatically, but knowing the basic commands is essential for troubleshooting, customising start/end scripts and understanding what the machine is doing.
What is the G-code
The G-code is a standard text format created for CNC machines and adopted by 3D printing. Each line contains a command with parameters. The printer executes the commands one at a time, in the order in which they are written. Commands begin with the letter G (movements) or M (machine functions), followed by a number and optional parameters.
Movement Commands
G28 is the homing command: it returns all axes (or those specified) to the zero position. It is always the first command of every print. G28 X Y homing only X and Y. G1 is the linear movement command: G1 X100 Y50 Z0.3 F3000 E10 moves the head at coordinates X=100, Y=50, Z=0.3 at a speed of 3000 mm/min extruding 10 mm of filament. G0 is identical to G1 but by convention indicates rapid movements without extrusion (travel). G90 activates absolute positioning (co-ordinates are referenced to the origin), G91 activates relative positioning (co-ordinates are increments).
Temperature Commands
M104 S220 sets the nozzle temperature to 220 °C and immediately continues with the next command (does not wait). M109 S220 sets the temperature and waits for it to be reached before continuing. M140 S60 sets the plate temperature to 60 °C (does not wait). M190 S60 sets and waits for plate temperature. M106 S255 switches fan on to full (S0-S255). M107 switches the fan off.
Extrusion commands
M82 sets the extruder to absolute mode. M83 sets relative mode (most common). G92 E0 resets the extruder counter (frequently used in print start scripts).
Useful commands for debugging
M503 shows all current settings saved in EEPROM (steps/mm, PIDs, limits). M92 X80 Y80 Z400 E93 sets the steps per millimetre for each axis. M500 saves the current settings in EEPROM. M501 loads settings from EEPROM. M502 restores the factory settings.
The Print Start Script
The print start script (start G-code) in the slicer is the sequence of commands executed before each print. A typical script includes: M140 S[bed_temp] to start heating the platter, then G28 for homing, then G29 for auto-leveling (if available), then M109 S[nozzle_temp] to wait for the nozzle temperature, a purge line along the edge of the platter to prepare for extrusion, and finally the actual print starts.
Practical tips
Open the .gcode files with a text editor to understand what the slicer does. Edit the start G-code in the slicer to customise the start sequence. Use M104/M140 commands (no waiting) before homing to save time: the platter heats up while the machine is homing. In case of problems, send manual G-code commands from the firmware console (OctoPrint, Mainsail, Fluidd, Bambu Studio) to diagnose the problem.
Guide to G-code commands for 3D printing
1. What is G-code and why is it important to know?
G-code is the universal language of computer numerical control (CNC) machines and 3D printers. Although the slicer generates the code automatically, knowing the basic commands allows you to customise the print start script, diagnose mechanical problems via the console and optimise heating times. Each line of code represents a precise instruction that the printer's motherboard executes in sequence.
2. What is the difference between G1, G28 and G29 commands?
These are the pillars of motion:
G28: Performs Homing, bringing the axes into contact with the end stops to establish physical zero.
G29: Initiates the Auto Bed Leveling procedure (if the printer has a sensor such as the BL-Touch), creating a map of the platen surface.
G1: This is the controlled linear motion command; it tells the head where to go, at what speed (F) and how much filament to extrude (E).
3. M104 vs M109: How to manage temperatures correctly?
Heat management is crucial to avoid damaging components:
M104 (Set): Sends the heating order and immediately moves on to the next command. It is often used in the start G-code to start heating the platter while the head is homing.
M109 (Wait): Sets the temperature and stops all other operations until the nozzle has reached the set value. This is essential before starting to extrude the purge line to avoid forcing the extruder motor cold.
4. What are the M500, M501 and M503 commands used for?
These commands are used to manage the printer's non-volatile memory (EEPROM):
M503: Displays all current parameters (such as motor steps/mm or PID values).
M500: Saves the changes made (e.g. after an extruder calibration) so that they remain active even after being switched off.
M501: Reloads saved values, useful if you have made temporary changes that you wish to undo.
5. How can I use G-code to solve printing problems?
Via the slicer console or interfaces such as OctoPrint or Fluidd, you can send manual commands to test individual components. For example, if the extruder seems to be stuck, you can send M83 (relative mode) followed by G1 E10 F100 to see if the motor is pushing the wire correctly. Knowing G-code transforms the user from a mere spectator to a technician capable of intervening directly on the 'brain' of the machine. On DHM-online you will find motherboards and control interfaces compatible with all major firmware (Marlin, Klipper, RepRapFirmware) for total management of your code.





