What is G-code? INTRODUCTION

G-code is the language of your 3d printer. It is a series of commands that the machine understands and that determine where the toolhead moves, how fast, how much filament it extrudes and so on. Normally the generation of G-code is left to the slicing softwares but nothing stops you from writing your own G-code files, particularly if you have some very specific ideas in our mind or you would like to achieve effects that would not normally be obtainable with the available solutions.

 

Today we will start a series of tutorials on custom G-code generation that will allow you to understand how G-code works, how to write it correctly, plus giving you some tools that will allow you to create you own software for 3D printing. To do this we will use the programming language called Processing. If you are already familiar with it and with coding in general you have an advantage, but even if you are not, we will try to keep things as simple and understandable as possible. For visualizing our G-code we will use the Voxelizer software and for 3d printing it a ZMorph 2VX.

 

Enough of talking now, let’s have a look at some real G-code.

 

 

How G-code looks like

 

To start, let’s look at the G-code generated for printing this simple low-poly Bulbasaur. If you use Voxelizer you can do this by clicking on File→ Export GCODE… after having it sliced.

 

 

G-code is nothing more than a text file (usually with the extension .g or .gcode) so, in order to visualize it, you can just tell to your operating system to open it with a text editor, for example Notepad. And here we go:

 

G91
G1 Z1.000 F200.000
G90
G28 X0.000 Y0.000
G1 X117.500 Y125.000 F8000.000
G28 Z0.000
T1
G92 E0.00000
T0
G92 E0.00000
; init bottom layer material
; bottom layer
G1 Z0.400 F7200.000
G1 X129.750 Y141.750
G1 X130.750
G1 E0.00000 F600.000
G1 X130.616 Y142.250 E0.06887
G1 X130.250 Y142.616 E0.13773
G1 X129.750 Y142.750 E0.20660
G1 X129.250 Y142.616 E0.27547
G1 X128.884 Y142.250 E0.34433
G1 X128.750 Y141.750 E0.41320
G1 X128.884 Y141.250 E0.48207
G1 X129.250 Y140.884 E0.55094
G1 X129.750 Y140.750 E0.61980
G1 X130.250 Y140.884 E0.68867
G1 X130.616 Y141.250 E0.75754
G1 X130.750 Y141.750 E0.82640
G1 X131.550 F7200.000
G1 E0.82640 F600.000
G1 X131.309 Y142.650 E0.95036
G1 X130.650 Y143.309 E1.07433
G1 X129.750 Y143.550 E1.19829
G1 X128.850 Y143.309 E1.32224
G1 X128.191 Y142.650 E1.44621
G1 X127.950 Y141.750 E1.57017
G1 X128.191 Y140.850 E1.69413
G1 X128.850 Y140.191 E1.81809
G1 X129.750 Y139.950 E1.94205
G1 X130.650 Y140.191 E2.06601
G1 X131.309 Y140.850 E2.18997
G1 X131.550 Y141.750 E2.31393

...

 

Quite scary, isn’t it? Well, not really, let’s start to read it.

Each line constitutes a single command that the machine reads and apply. As you notice, most of these commands start with with the letter ‘G’, hence the name G-code. Zmorph uses Smoothieware to interpret the G-code, so we will always refer to it in these tutorials as a reference for our commands. If you are using a different printer and you are encountering errors during the print, you might need to check which variant of G-code your machine is using and, in case, change some commands accordingly.

The first eight lines of code set up the machine for the print:

G91
G1 Z1.000 F200.000
G90
G28 X0.000 Y0.000
G1 X117.500 Y125.000 F8000.000
G28 Z0.000
T1
G92 E0.00000
T0
G92 E0.00000
  • G91, as we can see from the Smoothieware documentation, sets the machine coordinates system in Relative Mode: this means that when we will state a position in the commands, the machine will interpret it relatively to the last one (so writing G1 X10 Y5 in relative mode means “Move 10mm to the right and 5mm to the top relatively to the last position of the toolhead”).

 

  • G1 is the command that we will use the most: it means “Move the toolhead”, so G1 Z1.000 F200.000, means “Move the toolhead up of 1mm (wherever it is, since we are in relative mode) with a speed of 200 mm/minute (the F parameter)”.

 

  • G90 sets the machine coordinates system in Absolute Mode: as opposed to Relative Mode, this means that when we will state a position in the commands the machine will interpret it in respect to the origin of the axes (normally, the bottom-left of the printing table). So writing G1 X10 Y5 in Absolute mode means “Move the the position (10,5) relatively to the origin of the axes”.

 

  • G28 X0.000 Y0.000 homes the X and Y axes to their origin (bottom left of the table).

 

  • G1 X117.500 Y125.000 F8000.000  means “Move the tool head to X117.5 and Y125 (absolute mode) with a speed of 8000 mm/minute”. This is the center of the table in the Zmorph printer.

 

  • G28 Z0.000 homes the Z axis.

 

  • T allows you to select the toolhead (for example if you are using a dual extruder, T0 is the left one and T1 the right one). If you are using a single extruder this operation is superfluous.

 

  • G92 E0.00000 resets the currently selected extruder position to 0.

 

You already know most of the essential G-code commands!

 

Let’s look at the next lines:

 

; init bottom layer material
; bottom layer
G1 Z0.400 F7200.000
G1 X129.750 Y141.750
G1 X130.750
G1 E0.00000 F600.000
G1 X130.616 Y142.250 E0.06887
G1 X130.250 Y142.616 E0.13773
G1 X129.750 Y142.750 E0.20660
G1 X129.250 Y142.616 E0.27547
  • A semicolon ; is the way to comment your G-code. The machine will ignore whatever is written after it, until the next line.

 

  • G1 Z0.400 F7200.000 We are moving on the Z axis with a speed of 7200 mm/minute. Let’s skip few lines (which you can already ready).

 

  • G1 E0.00000 F600.000 with this command we are setting the extruding speed: 600 mm/minute. The machine will remember this and you will not have to specify the speed again in the following G1 commands.

 

  • G1 X130.616 Y142.250 E0.06887 from here, the printing is really starting. This means “Move to the coordinate X30.616 Y142.250 and, meanwhile, extrude 0.06887 mm of filament (at the speed of 600 mm/minute).

 

  • As you can already guess, the next command G1 X130.250 Y142.616 E0.13773 reads “Move to the coordinate X130.250 Y142.616 and, meanwhile, extrude 0.0688 mm of filament (0.13773 – 0.06887).

This means that, if we put an E value smaller than the one in the previous command, we will have a retraction of the filament. This will come very handy later on.

 

That’s basically all of the G-code that you need to know! There are a few other useful commands, we will look at them later. In any case, we encourage you to read the Smoothieware page about G-code, it’s not long and it’s very useful.

 

Ok, how about writing our own little G-code file before closing this introductory tutorial? We will create a small cube, let’s see how.

 

 

Writing your own first G-code file

 

Open a new Notepad file (or other text editor).

 

Let’s first write the commands for homing the printer and setting up the extruder:

 

; relative mode
G91
G1 Z1; up one millimeter
G28 X0 Y0; home X and Y axes
; absolute mode
G90
G1 X117.5 Y125. F8000; go to the center (modify according to your printer)
G28 Z0; home Z axis
G1 Z0; go to height 0
T0; select extruder 1
G92 E0; reset extruder position to 0

 

Now let’s move to our starting position. Since we are writing our G-code “by hand”, we will use Relative mode, it will make our writing much easier.  

 

; relative mode
G91
G1 Z0.1; first layer height
G1 X5.0 Y5.0; starting position
G1 E0.00000 F400.000; set speed of extrusion (slow for first layer)

 

Now, let’s decide few parameters, we will use them to calculate the amount of filament extrusion that we need :

  • Path width: 0.4 mm
  • Layer height: 0.2 mm
  • Filament: PLA 1.75 (diameter in mm)

Filament_extrusion_scheme

We want to print a cube with a side of 1 cm.

Now we can calculate how much extrusion we need for each side of the squares making the cube:

  • Volume of the filament to be extruded = Path width * Layer height * path length  → 0.4 mm 0.2 mm 10 mm = 0.8 mm3
  • Length of filament extruded = volume of the filament to be extruded / area of the filament cross section → 0.8 mm3 ( (1.75 mm 2)2) = 0.3326 mm

 

Now we have all the information that we need to know in order to write the commands to print our cube.

 

Here are the first five layers:

 

; in the first two layers we will extrude double amount of filament
G1 X-10 Y0 E0.6652
G1 X0 Y-10 E0.6652
G1 X10 Y0 E0.6652
G1 X0 Y10 E0.6652
G1 X0 Y0 Z0.2; up one layer
G1 X-10 Y0 E0.6652
G1 X0 Y-10 E0.6652
G1 X10 Y0 E0.6652
G1 X0 Y10 E0.6652
G1 X0 Y0 Z0.2
; from now on we will extrude the normal amount of filament
G1 X-10 Y0 E0.3326
G1 X0 Y-10 E0.3326
G1 X10 Y0 E0.3326
G1 X0 Y10 E0.3326
G1 X0 Y0 Z0.2
G1 F1200.000; now we can increase the printing speed
M106; turn fans on
G1 X-10 Y0 E0.3326
G1 X0 Y-10 E0.3326
G1 X10 Y0 E0.3326
G1 X0 Y10 E0.3326
G1 X0 Y0 Z0.2
G1 X-10 Y0 E0.3326
G1 X0 Y-10 E0.3326
G1 X10 Y0 E0.3326
G1 X0 Y10 E0.3326
G1 X0 Y0 Z0.2

 

The height of our cube is 1 cm, so we need 10mm/0.2mm(layer height) → 50 layers. Just copy-paste the last layer other 45 times:

 

G1 X-10 Y0 E0.3326
G1 X0 Y-10 E0.3326
G1 X10 Y0 E0.3326
G1 X0 Y10 E0.3326
G1 X0 Y0 Z0.2

 

And, at the end, the last commands to finish our print:

 

G1 E-4 F3000; retract filament to avoid filament drop on last layer
G1 X0 Y100 Z20; facilitate object removal
G1 E4; restore filament position
M107; turn fans off

 

Now save your text file, close it, and modify the extension to .g or .gcode.

 

You are done! Send it to you printer and there you have it, your little cube made from the G-code that you wrote by yourself. Congratulations!

You probably feel very proud now for your little creation, however it was quite a lot of work for a simple cube. Wouldn’t it be better if we could automate all of this? In the next tutorials we will see how we can achieve this goal using Processing. Then there will be no limits to what you can imagine and do with your printer. Stay tuned!

 


wall panel cnc file

Leave a Reply