G-Code Programming Examples

G-Code Programming Examples
— · 0 votes

A G-code programming example is a short block of commands that tells a CNC machine where to move, how fast to feed, and when to start the spindle. Reading one example part program line by line teaches more than memorizing a code list, because you see how each word depends on the ones around it. The blocks below show the structure you can adapt to your own parts.

  • Every G-code block does one job: set a mode, move an axis, or switch a machine function on or off.
  • G words set the machine mode and M words switch functions such as spindle and coolant.
  • A program starts with safe setup blocks and ends with a controlled retract and stop.
  • Modal commands stay active until another command in the same group replaces them.
  • Feed and speed values belong to the material and tool, so copy the structure, not the numbers.

What a G-code block contains

A G-code block is one line of a program, made of a letter address and a number, such as G01 or X25.0. The letter tells the control what kind of information follows, and the number gives the value. A block can hold several words when they belong to the same move.

Word order inside a block is flexible on many controls, but a readable order helps the operator check the program. Put the motion mode first, then the axis positions, then the feed rate. Keep one idea per block so a mistake is easy to find.

The M words in a block handle machine functions rather than axis motion. An M code might start the spindle, turn on coolant, or stop the program at the end. G and M code example sets separate the two letters because they control different parts of the machine.

A basic milling program example

A basic CNC programming example for milling usually starts with a program number and a few safety blocks. These blocks cancel cutter compensation, select the work offset, and set the plane before any cutting move. Skipping them lets a leftover mode from the previous job surprise you.

The first cutting block brings the tool to the work at a safe height, then feeds down to depth. A rapid move covers distance with no cutting, and a feed move cuts at the programmed rate. Mixing the two at the wrong moment is how tools break.

The program ends by retracting the tool clear of the part, stopping the spindle, and returning the table to a known position. A clean end block makes the next setup predictable. Here is the skeleton of a simple facing pass, written with generic values.

BlockWhat it does
N10 G21 G90 G94Metric units, absolute moves, feed per minute
N20 G54 G17Select work offset and XY plane
N30 M03 S1200Start spindle clockwise at the set speed
N40 G00 X0 Y0 Z10Rapid to start point above the part
N50 G01 Z-0.5 F100Feed down to depth at the feed rate
N60 G01 X80 F250Cut across the part in a straight line
N70 G00 Z10Rapid clear of the part
N80 M05 M30Stop spindle and end the program

Reading the motion modes

G00 moves the tool at rapid traverse, which is the machine's fast positioning speed and not a cutting move. G01 moves in a straight line at the programmed feed rate. These two modes stay active until you change them, which is what modal means.

G02 and G03 cut clockwise and counterclockwise arcs, and they need an arc center or radius word. An arc block that omits the center or radius stops the control with an alarm. Check the arc direction against the drawing before you run the program.

G90 and G91 choose absolute or incremental positioning and change how every axis word is read. In absolute mode each position is measured from the work zero point. In incremental mode each position is measured from where the tool already is.

How M codes drive the machine

An M code example such as M03 or M08 switches a machine function rather than moving an axis. M03 starts the spindle clockwise and M04 starts it counterclockwise, and both need an S word for speed. M05 stops the spindle.

Coolant codes such as M08 and M09 turn flood coolant on and off. Put the coolant on before the tool reaches the cut and off before the retract, so chips clear and the tool does not drag. Some machines use different coolant codes, so check the machine manual.

M30 ends the program and rewinds it to the start, while M00 pauses the program until the operator presses cycle start. Use M00 when you need to check a feature or clear chips by hand. Never reach into the work area while the spindle can start.

A turning program example

A turning program example looks different because the spindle carries the work and the turret moves in X and Z. On a lathe, X is the diameter or radius across the part and Z is along the spindle axis. Confirm which one your control uses before you program a diameter.

A facing pass on a lathe feeds the tool across the face of the part, then retracts in Z. A turning pass moves along Z at a set depth of cut and leaves a cylinder. Constant surface speed, set with a G96 block, changes the spindle speed as the diameter changes.

The program still ends with a retract, a spindle stop, and an end code, just as a mill program does. Tool changes and offsets sit between the passes, and each offset must match the tool in the turret. A wrong offset number cuts the part at the wrong size.

Setting feeds and speeds in an example

Feed and speed values in a sample G-code program depend on the material, the tool, and the rigidity of the setup. A block that works in aluminum will not suit steel at the same rate. Treat every example as a template for structure, not a recipe for numbers.

Feed rate is the distance the tool travels per minute in milling, and it appears as an F word. Spindle speed appears as an S word and is set in revolutions per minute or by surface speed. The two work together, and changing one without the other changes the chip load.

On a lathe, constant surface speed keeps the cutting speed steady as the tool moves toward the center. The control raises the spindle speed as the diameter shrinks, so set a maximum speed limit to protect the chuck. A machinist with the machine manual and the tool data sets these values for the job.

Checking a program before you run it

A new example part program gets checked before the first cut, and the machine graphics or a simulation run shows the toolpath. Look for the tool going below the part, through a clamp, or past the travel limits. Correct those faults in the program, not at the control with the feed override.

Dry run the program with the tool clear of the work and watch the distance-to-go display. The display shows how far the axis has left to move, so a wrong offset shows up as a large number. Stop the run if any value looks wrong.

Check the work offset, the tool length offset, and the tool number against the setup sheet before cutting. A single-digit error in an offset can drive a tool into the table or the chuck. Where a job involves an unfamiliar control or a heavy setup, have a trained machinist verify the program.

Building your own program from the examples

Start your own program by copying the structure of the milling or turning example and changing the geometry. Keep the setup blocks, the safe height, and the end blocks the same. Replace only the motion and speed words that describe your part.

Write one feature at a time and check it in the graphics before you add the next. A facing pass, then a contour, then a pocket is easier to debug than one long block of moves. Add comments so the next operator can follow your intent.

Save each proven program as a template for parts of the same family. A template that holds the safe start and end blocks lowers the risk on every later job. The value of a programming example lies in that reusable skeleton.

G-Code Programming Questions

What is the difference between G code and M code?

G codes set the machine mode and control axis motion, while M codes switch machine functions on and off. A G word such as G01 selects a cutting move, and an M word such as M03 starts the spindle. Both appear in the same block when a move needs a function.

Can I learn CNC programming from examples alone?

Examples teach the structure of a program well, but you also need the machine manual and the code list for your control. Dialects differ between controls, so a block from one machine may not run on another. Practice with graphics and a dry run builds the rest of the skill.

What does modal mean in a G-code program?

A modal command stays active until another command in the same group replaces it. Once you program G01, every later move cuts at the feed rate until you write G00. Modal behavior saves typing but makes block order important.

How do I set feed and speed for an example program?

Feeds and speeds come from the material, the tool, and the rigidity of the setup, not from the example. Use the tool maker's data and the machine manual, then adjust after the first cut. Copy the structure of an example, never its numbers.

What should the last blocks of a program do?

The last blocks retract the tool clear of the part, stop the spindle, and end the program. An M05 stop and an M30 end code leave the machine in a known state. A clean end makes the next setup safer.

Is it safe to run an unproven program at full speed?

No, an unproven program should be checked in graphics or a dry run before the first cut. Run the first part with the tool clear of the work and watch the distance-to-go display. A trained machinist verifies programs on unfamiliar machines.

Read next

Latest articles