⚙️⚡⚡ Basic Intro to CNC programming
4 Video Views·Oct 7, 2024
This is a basic introduction to G-code programming for CNC machines. This tutorial will give you the basics for learning to program all kinds of machines, from hobby-grade sheet cutters up to professional-grade mills and lathes. Please ask questions in the comments!
Hey guys, if you're new to CNC programming and would like some tips to get you started,
that's what this video is for, so let's jump right in.
First of all, CNC equipment runs on G-code.
This ranges from your hobbyist-grade 3D printer to conventional manufacturing equipment like
mills and lathes to non-traditional stuff like EDM machines or lasers.
Assuming you know this much, the next question becomes,
okay, so what is G-code?
It's the language of CNC machines, which is written using two different types of commands,
those that start with the letter G and those that start with the letter M.
There are exceptions, but the general way that it works is that commands starting with G
control the movement of the machine.
For example, a G01 will let you move and access some amount of distance and at a certain speed.
Another example would be a G04, which will let you tell the program to dwell or pause
for a certain amount of time before continuing on.
Commands that start with M, on the other hand, control machine-specific functions,
like turning on the spindle or the coolant in a milling machine,
or in the case of one of the lasers I use, an M54, for example,
will fire a burst of laser pulses to drill a hole.
So, is G-code standardized?
Well, mostly.
It has a long history, and different machine manufacturers sometimes do things differently
than others, so the best advice I can give you up front is to check the manual for whatever
specific machine you're trying to use.
I would expect G commands to be pretty common across the board, but M commands will vary.
So, let's look at an example program to get a feel for it.
So, I wrote this program using code specific to one of the CNC lasers I'm familiar with,
so just keep in mind that this may look slightly different from whatever machine you're using,
but the concept is still the same.
Before reading through the codes themselves, let's first talk about how comments work.
If you're writing a program and want to leave yourself a note for future reference,
or for the next person so they know what's going on, you need to mark it so the machine
doesn't try to read it and get confused.
You can do that by putting a comment character at the beginning of your note.
In this case, if I type a semicolon, the machine controller will know to ignore everything
that comes after it on that particular line.
All comments are shown in gray in this example.
Be aware that the comment character may be different on one machine to the next.
They might be a semicolon in this case, but they could also be double forward slashes
or an apostrophe, etc.
Okay, so let's walk through the actual codes.
Take a look at the first four commands.
It's good practice at the start of any program to run a group of commands that set the machine
in the proper mode you want it to be in before you run the rest of your program.
As we talk about these, you'll see what I mean.
The first command is the G99.
It sets a number of things, but primarily it will set the machine's coordinate system
to report access positions relative to a known reference spot like the center of your
rotary table if you have one, for example.
I don't want to get into more detail than that right now, but it just makes sure that
you know where you're starting from so you don't inadvertently crash into something.
Next is the G70.
G70 tells the machine you want to use English units versus metric, which would be a G71
instead.
So if you tell the machine to move 5, it will know to move 5 inches instead of 5 millimeters.
I don't have to tell you why not setting this up front could get you into trouble.
Next is the G91.
The machine will always be in either G90, absolute mode, or G91, incremental mode.
In absolute mode, a command to move the x-axis positive 5 will move it to a position 5 inches
away in the plus direction from whatever zero point is set in the machine, regardless of
where the x-axis is currently.
This means that the x-axis will move in the plus direction or the minus direction, whatever
it needs to, in order to get to that spot.
Incremental mode, which we're using in this example, is different.
It is relative to wherever you are right now.
In this case, if you say to move the x-axis positive 5, it will move 5 inches in the positive
direction from whatever position it's already at.
If this is a little hard to picture right now, don't worry, but it's definitely important
to make sure the machine moves in the way that you actually want it to.
The M100 is a command that likely won't be the same in other machines, but in this case
what it's doing is opening a shutter that basically enables the laser, so it will shoot
what we ask it to later.
So that's everything in the safety block in this example, so now we can actually get to
the commands that do stuff.
The next line begins with the G1, or G01, which would mean the same thing, meaning let's
move an axis, or group of axes, in a straight line.
And the X2 means that we're going to be moving the x-axis 2 inches in the positive direction.
The number following the letter F determines how fast you're going to move, and in this
case it will be at 10 inches per minute.
The M0 that follows is a stop command.
It will stop the program indefinitely until the user presses the start button on the machine
control panel.
These can be handy for a number of reasons, but assuming you press the start button, the
program would move on to the next line, which is a bunch of M codes all stacked together
on one line.
You can see an M51, an M52, an M53, and a PK in there.
These codes are what allow you to set the laser parameters on this particular machine,
like how much power we want to use and so forth.
The G04 that follows, as I said before, is a dwell or a pause.
The 0.5 means it's going to pause for half a second before continuing.
The X, if you're wondering, doesn't have anything to do with the x-axis.
It just has to be there for the pause to work for whatever reason.
After the pause is over, the M54 command will execute, which in this case will fire a group
of 5 laser pulses because of the X5.
Again, the X just has to be there.
And no, it's not a weaponized laser, so that would be awesome.
After we've shot our 5 laser pulses, we see an X-2.0, and that's it.
In this case, a G01 is assumed, as is whatever feed rate we were using last, which would
still be 10 inches per minute.
This is just a shorthand form of our initial move command, but this time we're moving 2
inches in the opposite direction, back to where we started.
After the X has finished its second move, the M101 command will close the laser shutter,
and the M02 command will tell the machine controller that the program is complete, thus
resetting it back to the beginning.
This doesn't run it again, it just resets it, and that's the end.
Feel free to rewatch it a few times if you need to, but my hope is that walking through
this example helps give you a feel for how to understand how programs are structured
as you read and write them.
A few tips that I want to leave you with are this.
One, ask an experienced person.
You can't ask for more than advice from someone who already knows what they're doing.
Two, find the table in the manual that lists all the GNM commands and what they do for
your machine and make a copy.
Tape it next to the machine control panel if you need to, and use it for reference until
you're familiar with what they do.
Three, find example programs and read them, and try to understand how they work.
Lastly, try writing your own programs, starting with short, simple ones at first.
Maybe that only move a single axis back and forth a little bit.
That's how you start.
And that's really all I've got for this introduction.
This is my first video on CNC programming, so if you want me to do another video, or
if you have specific questions about something in this video, please let me know in the comments.
Just FYI, as I make other videos, I'll put links to them below, and thank you very much
for watching.

