Mach 4 G Code is a set of standardized commands used to control CNC machines running the Mach 4 CNC controller software. This list enables precise movement, spindle control, tool changes, and feed rate adjustments without manual intervention. To use these commands, you must have Mach 4 installed on a compatible PC connected to your CNC machine via a motion controller, with correct motor tuning and limit switch wiring already configured. No additional hardware is required beyond what is needed to run Mach 4 itself.

What You'll Need

  • A computer running Windows 7 or later (64-bit recommended)
  • Mach 4 CNC Controller software (licensed version)
  • A compatible motion controller (e.g., PoKeys, PMDX, or Ethernet-based board)
  • Properly wired CNC machine with steppers or servos, limit switches, and spindle control
  • A G-code file or text editor to write and save your program (.nc or .gcode extension)

Step-by-Step

  1. Power on your CNC machine and ensure all limit switches are properly engaged — if any are tripped, the machine will not home or move.
  2. Launch Mach 4 on your control computer and load your machine configuration file (.xml) that matches your machine’s axis layout, steps per unit, and motor tuning.
  3. Click the Home All button in the Mach 4 interface to move all axes to their reference positions. Wait for the green Home indicator lights to turn on for each axis.
  4. Open your G-code file in a text editor like Notepad++ or within Mach 4 using File > Open Program. Verify the file starts with % and ends with M30 or M02 to indicate proper program termination.
  5. Load the program into Mach 4 by clicking Load Program or pressing Ctrl+L. The program lines will appear in the program window.
  6. Set the work coordinate system (G54–G59) by jogging the tool to your material’s origin point, then click Set Work Zero on the appropriate axis panel.
  7. Ensure the spindle is off and the tool is clear of the material. Select the correct tool number if using an automatic tool changer (ATC), or manually install the tool if not.
  8. Enable the spindle using M3 (clockwise) or M4 (counterclockwise) followed by S### to set RPM (e.g., M3 S8000). Confirm spindle rotation with the spindle speed indicator in Mach 4.
  9. Click Start or press F5 to begin execution. Observe the first few lines of motion for accuracy — if the tool moves incorrectly, stop immediately using the red Feed Hold button.
  10. Monitor the machine during operation. Use the Pause button to interrupt, Reset to cancel the program, or Step to execute one line at a time for debugging.
  11. After completion, verify the part matches dimensions. Use a caliper to check critical features against the G-code’s programmed values.

Common Mistakes

  • Forgetting to set work zero before starting — this causes all cuts to be offset by inches or millimeters.
  • Using G20 (inches) with a machine calibrated in G21 (millimeters), resulting in 25x overtravel and potential collision.
  • Missing or misordered G-code blocks — for example, using G01 without a preceding F (feed rate) command, which may cause erratic movement.
  • Running a program with M06 (tool change) but no tool number specified, like M06 T1 — this halts the machine indefinitely.
  • Not verifying motor tuning values (steps per unit) before running, leading to inaccurate positioning even if the G-code is correct.

Tips

  • Always simulate your G-code in Mach 4’s Verify mode before running on the machine — it shows path preview without motion.
  • Use comments in your G-code (surrounded by parentheses) to document tool changes, coolant states, or material settings — e.g., (TOOL: 1/4" end mill, ALUMINUM).
  • Keep your G-code files organized by material and tool type, using clear filenames like Aluminum_Slot_1.nc.
  • Use G91 (incremental) sparingly; G90 (absolute) is safer for most machining operations.
  • Always include G4 P1 (dwell 1 second) after spindle start to allow full RPM before cutting begins.
  • Save your machine configuration file (.xml) after any change — losing it means retuning all axes from scratch.

Troubleshooting

  • Machine doesn’t move after pressing Start — Check if the E-stop is engaged, verify all limit switches are not stuck, and confirm the motor drivers are powered and not overheating.
  • Position drift during long runs — This usually means motor current is too low or belts are slipping. Increase motor current in the motion controller settings and inspect belt tension.
  • Spindle won’t turn on — Confirm the spindle control output is assigned correctly in Mach 4’s Config > Ports & Pins > Spindle section. Test with the manual spindle on/off buttons.
  • Program stops at line 10 with "Unknown Code" error — Check for typos: G001 should be G00, M001 should be M00. Remove any extra spaces or special characters.
  • Tool path is mirrored or inverted — Reverse the direction of the affected axis in Config > Ports & Pins > Motor Tuning by checking the "Invert" box for that axis.

Frequently Asked Questions

What is the difference between G00 and G01?

G00 is rapid positioning — the tool moves as fast as the motors allow without cutting. G01 is linear interpolation at the programmed feed rate — used for actual cutting.

Can I use M05 to stop the spindle during a program?

Yes, M05 stops spindle rotation. Use it after finishing a cut and before retracting the tool. Always use M05 before M30 to end the program cleanly.

Do I need to use G28 to return to home?

Not always. G28 moves to the machine zero point, but if you’ve homed already with Home All, G53 G00 X0 Y0 Z0 will move directly to machine zero without going through intermediate points.

What does G4 do in Mach 4?

G4 is a dwell command. G4 P1 pauses for 1 second. Use it after spindle start, tool change, or before a precision cut to allow mechanical settling.

Can I run Mach 4 G-code on a different CNC controller?

Not reliably. G-code is standardized, but Mach 4 uses specific macro calls and custom M-codes (like M64 for digital output) that other controllers may not recognize.

Why does Mach 4 show "Invalid File Format" when I load my G-code?

The file may contain non-standard characters, UTF-16 encoding, or Windows line endings. Save it as plain ASCII or ANSI with Unix line endings (LF only) in Notepad++.