I want to make my own CNC machine from scratch because it's cheaper to make than buy a premade one. I noticed that some machines go for tens of thousands of dollars and I cannot understand how they can cost that much to build. I am not afraid to learn what I need to learn to make one.

I can do basic Arduino programming and stuff. I understand the concept of G-code, its not that hard. The thing I cannot understand is how you go from G-code to moving the stepper motors.

How does a controller translate a move $x$ 20 units to moving the stepper $x$ amount of steps and keep dimensional accuracy? If the CAD G-code says move 200 mm in the $x$ direction, how to you translate the G-code to tell the stepper motor that 200 mm?

Any help is greatly appreciated. If you need clarification, please ask in the comments.

  • $\begingroup$ Look up machinekit,gbrl,smoothieware. Infact there are already dozens of opensource controller boards to purchase for little money, don‘t reinvent the wheel ;) $\endgroup$ Commented Sep 5, 2020 at 11:29
  • $\begingroup$ Even if I don’t make my own, I would like to know how they work. $\endgroup$ Commented Sep 6, 2020 at 6:14
  • $\begingroup$ all the above mentioned are on github, you can go read how all machine motion is done with actual code. $\endgroup$ Commented Sep 6, 2020 at 6:16
  • $\begingroup$ Ok, thanks for the help. $\endgroup$ Commented Sep 7, 2020 at 7:06

1 Answer 1


How does a controller translate a move x 20 units to moving the stepper x amount of steps and keep dimensional accuracy? If the CAD G-code says move 200 mm in the x direction, how to you translate the G-code to tell the stepper motor that 200 mm?

The G-Code interpreter interprets the motion command. It applies some basic transformations on it to make sure that all coordinates in the motion buffer are in the same coordinate system. Assuming it is G01 (linear interpolation) X 200, The interpreter will place the appropriate command to the motion buffer. Please note that there is a difference between currently executed command and interpreted command. The difference is a lookahead, or a motion command buffer with several hundreds of commands (in case of industrial high-end CNC machines).

The trajectories are planned by evaluating machine dynamics (the trajectory generator is aware of the maximum velocities and acceleration of each axis). So if the max velocity of your CNC is 5000 m/min and you have set the F paramter for your G01 to 8000 your machine will accelerate to 5000 and keep that velocity until possible or set otherwise.

There are some advanced transformation functions (optionally) applied, like tool radius compensations, etc.

At this point there is a trajectory in form of a spline curve with attached velocity/acceleration profiles planned for the entire lookahead buffer. This spline is then discretized to achieve the next reference point for the closed loop controls (position, velocity and torque control loops on an industrial CNC machine) or just a stepper motor on a hobby CNC.So if you have a linear motion on the x axis, the spline will be only a line segment from the previous value in the motion buffer to X200. If the motion buffer is empty, i.e. this is the first command the current position will be used to start the line.

There is another transformation here. The industrial system measure current position in encoder ticks, the hobby systems in stepper motor steps. There has to be a transformation function available to convert from ticks or steps to mm and vice-versa. This comes from the geometrical model of the machine and includes transmission ratios of gearboxes, ballscrew drives or belt drives. This is a design parameter of the machine and is included as parameter for the software. More advanced functions, like compensating for belt elasticity and/or backlash are also common. So 200 mm might translate to 2 000 000 encoder ticks or steps.

For a stepper motor quite simply the missing number of steps are calculated and executed. So if you are at x100 mm you will need to execute 1 000 000 steps. For a stepper motor without an encoder you have to count the steps travelled and use this count instead of an encoder position reading. If you loos count (e.g turn off machine) there is a reference position with a mechanical switch at one end of the axis. You drive to that end and reset count to 0 when you hit the switch.

For a closed loop system and error signal is created between current and target position for the first control loop, and cascaded error signals including feed forward reference signals for the velocity and torque control loops. Also here there might be a reference position, or you might have absolute encoders, which do not need a reference position.

  • $\begingroup$ Thank you very much, this was very helpful $\endgroup$ Commented Sep 10, 2020 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.