G-Code 101: Modify Your 3D Printing Files

G-Code 101
(Image credit: Tom's Hardware)

What is G-Code?

RS-274, more commonly known as G-Code is a programming language for CNC (Computer Numerical Control) used in computer-aided manufacturing. The basic gist of the language is that it tells a tool / device to “go there, and do this”.

G-Code can be used with many different types of machines, from laser cutters, lathes, mills and the best 3D printers. For 3D printers, G-code is typically created when we “slice” an object for printing. Slicing converts an object into a series of layers, which use G-Code commands to move the tool to mimic the outline and internal structure of the object. Common slicers, such as Cura and PrusaSlicer will do all of the hard work for us, but if we were to write our own G-Code, we could create scripts to tweak and tune our 3D printer for the best possible service.

In this how to we will learn how to write G-Code, specifically using a Creality Ender 2 Pro as our test machine. We will write a script that will be useful when manually leveling the print bed. The code created for this how to is bespoke for the Ender 2 Pro, and will need to be modified for your 3D printer. 

Writing a G-Code Script

G-Code doesn’t require a fancy text editor or IDE, all we need is a simple text editor, such as Notepad, or our preference, Notepad++.

Each line of G-Code is an instruction to a part of the device and commands are written using the command reference, followed by the command parameters (if any). To make a comment we need to prefix the comment with a semicolon.

1. Open your text editor and create a comment line to explain the purpose of the code.

; Ender 2 Pro Example Control G-Code

2. Use command G90 to set absolute positioning. This means that every movement is calculated from the same start point.

G90; Absolute positioning

3. Use command M117 to show a message on the Ender 2 Pro’s LCD screen. The string (message) is directly after issuing the command and requires no parameter.

M117 Tom's Hardware; Prints the string to the LCD screen

4. Use command G4 (dwell) to pause the script for three seconds.

G4 P3000; Wait for 3 seconds

5. Use commands M117 to display a message, and G4 to pause the script long enough for the user to read it.

M117 Heating bed; Prints the string to the LCD screen
G4 P1000; Wait for 1 second

6. Use command M190 to heat the print bed to 60 degrees Celsius (S60). This command will stop the script from running while the bed reaches the required temperature.

M190 S60; Set the bed temperature to 60C and wait until it hits that temperature

7. Home all the printer axes with command G28.

G28; Home all axis

8. Set the movement speed to 1500mm / minute. This will ensure that the bed and hot end move at the same speed. We can go higher, 3000mm / minute is possible, but start slow; that way you have time to react to issues.

G1 F1500; Set Feedrate (tool movement) to 1500 mm/min

9. Lift the Z axis 8mm using command G1 so that the nozzle is clear of the print bed. We don’t want to crash the nozzle into the print bed as that would cause damage to the nozzle and bed.

G1 Z8; Lift Z Axis 8mm

10. Use the G1 command to move the nozzle to a set X and Y position. In this case the position is directly above the front left bed adjustment wheel.

G1 X28 Y35; Move to position 1

11. Print an instruction to the user using M117.

M117 Level the bed #1

12. Wait for user input using M0. Note, this command does not work on every 3D printer. If it fails to work for you, replace it with G4 and use a long wait.

M0; Stop, wait for user input
<<If your 3D printer does not support M), use this for a 20 second wait>>
G4 P20000

13. Repeat the same process of lifting the nozzle, moving to the next position, instruct the user, and wait for input. Remember to swap M0 for G4 if your printer doesn’t support it. This will move the nozzle so that it is above the front right bed adjustment wheel.

G1 Z8 ; Lift Z Axis 8mm
G1 X140 Y35; Move to position 2
M117 Level the bed #2
M0; Stop, wait for user input

14. Another repeat, this time to position 3, the back right bed adjustment wheel.

G1 Z8 ; Lift Z axis
G1 X140 Y130; Move to position 3
M117 Level the bed #3
M0; Stop, wait for user input

15. Another repeat, this time to position 4, the back left bed adjustment wheel.

G1 Z8; Lift Z axis
G1 X28 Y130; Move to position 4
M117 Level the bed #4
M0; Stop, wait for user input

16. Use command G28 to home all the axes, and then display a completion message (M117) on the LCD screen.

G28; Home all axis
M117 Complete; Bed leveling is completed

17. Save the code as Ender2-Pro-Test.gcode to a micro SD card.

18. Remove the card and insert it into the printer.

19. Press the control dial in to open the menu.

20. Scroll down to Attach Card and press the dial. This will ready the card for use.

(Image credit: Tom's Hardware)

21. Scroll down to Print from Card and press the dial.

(Image credit: Tom's Hardware)

22. Select Ender2-Pro-Test.gcode and press the dial.

(Image credit: Tom's Hardware)

23. Select Print and press the dial to start. The printer will now go through the steps in the G-Code file.

(Image credit: Tom's Hardware)

The code will go through each step in the process, mimicking a bed-leveling process, but with the nozzle clear of the print bed.

(Image credit: Tom's Hardware)

 Once we have the process perfected, we can lower the nozzle and use it to perform bed-leveling in a semi-autonomous manner.

Complete Code Listing

; Ender 2 Pro Example Control G-Code

G90; Absolute positioning
M117 Tom's Hardware; Prints the string to the LCD screen
G4 P3000; Wait for 3 seconds
M117 Heating bed; Prints the string to the LCD screen
G4 P1000; Wait for 1 second
M190 S60; Set the bed temperature to 60C and wait until it hits that temperature
G28; Home all axis
G1 F1500; Set Feedrate (tool movement) to 1500 mm/min
G1 Z8; Lift Z Axis 8mm
G1 X28 Y35; Move to position 1
M117 Level the bed #1
M0; Stop, wait for user input
G1 Z8 ; Lift Z Axis 8mm
G1 X140 Y35; Move to position 2
M117 Level the bed #2
M0; Stop, wait for user input
G1 Z8 ; Lift Z axis
G1 X140 Y130; Move to position 3
M117 Level the bed #3
M0; Stop, wait for user input
G1 Z8; Lift Z axis
G1 X28 Y130; Move to position 4
M117 Level the bed #4
M0; Stop, wait for user input
G28; Home all axis
M117 Complete; Bed leveling is completed

Adding G-Code via a Slicer

Adding custom G-Code to your slicer provides control over every facet of your print process. We can add an extra nozzle wipe before a print, pre-heat the bed to a custom temperature and tweak many other settings for the perfect print. Here is how to edit the G-Code in two of the most common slicers, Prusa Slicer and Cura.

Adding G-Code Via Prusa Slicer

 1. Click on Printer Settings and then click on Expert. This will reveal every feature of Prusa Slicer so take care.

(Image credit: Tom's Hardware)

2. Click on Custom G-Code.

(Image credit: Tom's Hardware)

3. Custom G-Code can be inserted for the Start and End of a print. This will affect every print that is sliced using Prusa Slicer.

(Image credit: Tom's Hardware)

4. Click on Plater to return to preparing the object for printing.

(Image credit: Tom's Hardware)

Adding G-Code Via Cura Slicer

1. Open Cura and click on Settings >> Printer >> Manage Printers.

(Image credit: Tom's Hardware)

2. Click on Machine Settings.

(Image credit: Tom's Hardware)

3. The printer start and end G-Code can now be directly edited.

(Image credit: Tom's Hardware)

4. Click on Extruder 1 to edit the G-Code for the extruder.

(Image credit: Tom's Hardware)

5. Close the window to save and exit the Machine Settings dialog, then close the Preferences dialog. You will be returned to the Prepare screen ready to slice a new print.

Common G-Code Commands

Swipe to scroll horizontally
CommandDescriptionExample
G1A linear move between two points on the work surface.G1 Z8; Lift the Z axis 8mm
 Set the feedrate (speed) at which the motors move in mm/ minute.G1 F1500; Set speed to 1500mm per minute
 Extrude 25 mm of filament in a 50mm line. (Useful for cleaning the nozzle before a print)G1 X50 E25
G4Dwell. Add a pause, in milliseconds to the sequence.G4 P1000
G20Set unit of measurement to inchesG20
G21Set unit of measurement to millimetersG21
G28Home all axesG28
G90Absolute positioning. All coordinates are interpreted as logical coordinate space. G90
M0Wait for user input, typically a button press. Note: This command does not work for all 3D printers!M0
M18Disable stepper motors.M18; All steppers
  M18 Z E; Disable the Z axis stepper and the extruder stepper.
M117Display a message on the 3D printer display. Messages are strings that can contain letters, numbers and some punctuation.M117 Hello World!
M190Set the bed temperature, in degrees Celsius.M190 S60
M410Quickstop. An emergency stop of all stepper motors. Note: Stepper motors will be out of position after this command and all axes will need to be homed.M410
M500Save all settings to the EEPROM. Use with caution as it will overwrite the current configuration.M500
M701Load filament by turning the extruder stepper motor for a set duration.M701
M702Unload the filament, used when changing filament rolls.M702

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".