Let's Make Robots!

Converting an STL file to Gcode to drive a CNC mill or lathe

As mentioned in my blog, "Multipurpose Mini Machine" DAGU wants to develop a low cost CNC machine kit. The models shown here will not be the final product, they are simply what I used for testing the quality of the components.

Part of the kit will be an Arduino compatible controller. Ok, I know, nothing new so far.

What I hope will be new is that I want to be able to make an object just by inserting an SD card with an STL file on it. I want it to be self contained without needing a PC to drive it. The advantages are:

  • Simplicity - no cables or wireless links to your PC.
  • If your computer crashes it won't affect production of the parts.
  • Your computer is not slowed down because it is trying to drive 1 or more machines.
  • Your laptop is not tied to the machines, you can take your laptop with you while the machines keep on working.

I know there is software availble now that converts STL to gcode, perhaps the most well known is Skeinforge. The problem is I think the process is unecessarily complex. To begin with you need to install Python. Then it is recommended to install Psyco to speed it up. Then you can install / run Skeinforge. Then you need to configure the program Then you can generate your gcode. Admittedly, once this is done you can put your gcode onto an SD card.

I just want to put the STL file straight into the controller and have the machine do the rest. Maybe I am just dreaming but from what I have read of the STL file format I think it is possible. It is all simple trigonometry and floating point math. Although the Arduino is not a fast processor, it is still much faster than the actual machine. I think it should be possible for the Arduino to convert the STL file on the fly.

At this point I want to know:

  • Am I re-inventing the wheel? Is there an Arduino Library or easy to use software already?
  • Am I missing some important factor that makes this project impossible?
  • Is this project impractical in some way?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Ohh, this is not as simple as it sounds. There are 2 different machines: a mill and a lathe. A regular CNC is a mill, but there are industrial CNC lathes too. I have built a home made CNC and TinHead has done it too. Also CtC can give you more input on this matter, as he was the latest that fought with the double system: CNC and 3D print. From my experience working with the CNC I built: I used an old version of AutoCAD to design my pieces and my stupid way of designing the piece hurt me in the end. I have exported the file and load it in CAM Bam where I had to generate the toolpaths do cut my piece. There I run into trouble, because the lines were made from segments that were cloned and trimmed, so the tool was cutting one segment from left to right the the next segment from right to left and the tool had a lot of running around above the piece I needed to cut. I had to re-draw the whole piece in a continuous line to get nice, optimized tool paths. I also had to separate the holes from the lines and change tools because when drilling in acrylic with a milling tool I got melted plastic ruining my holes. Then CAM Bam generated the gcode, I loaded it on a stick and plugged it on the linux computer running the EMC2 that controlled the CNC. The nice part of the EMC2 is that you can do a dry run without a tool to see if the machine works properly and you can see the path on the screen. 

I would advise to try it out first, then find a way to simplify and integrate the process. Design a chassis tobe milled from a piece of wood or acrylic, export it, load it into a CAM processor, see wat kind of info you need to set up, generate the toolpath and the gcode then try a dry run to see how that works. After that you'll have a better understanding of the system.

For the lathe, a similar process can be done, but I think you need the CAM processor before you can give the file to the machine to process it.

When you desing the electronics that will run the machine, please include a LCD and buttons for the user interface.

At the moment there are only two machines, mill and lathe. When the kit is finished it may also be a 3D printer and a laser cutter as well. One reason I wanted to write my own code is so that I could include different options for different machines and materials being cut / printed.

The issue you had with line segments is another reason I wanted to avoid other peoples code. An STL file represents the object's surface as a series of triangles with each corner of the triangle represented by 3 floating point values (X,Y,Z). It does not matter how you draw it.

STL files also have an 80 byte header which is often ignored but can store useful information such as what type of machine and material should be used. Although this information might be blank when coming from programs like Sketchup. I had planned to have the Arduino update the header with configuration information so that you only need to configure a drawing once. next time you want to make that part the configuration is included in the header.

An LCD display and keypad is important. I definitely plan to include one so you can specify what type of machine you built and basic configuration data / homing opperations.

I think this is a good idea. I know that most professional machines don’t do this though (at least the ones I’ve worked with…which are pretty old). The ones I’ve used you have to generate the gcode offline (and on a PC).

When I read "convert the STL file on the fly" I was thinking you're crazy, but then I realized that if you are "slicing the stl" I guess that would be possible since you're only worried about one slice at a time (2.5 D instead of 3D). Although, if you’re planning on running the tool while you’re calculating the next path, that might require two threads (or two different processors). Even if you did all the path generation before starting the cutting, do it an arduino is going to be quite a project. But STL is definitely the file format to use for this. DXF and SVG are 2D files formats…I’m not sure what TinHead was getting at there.  

I have heard of CNCs that use the slicing technique, and played with some free programs once, but I've never seen the results from one. My impression is that it couldn't perform as nicely as the CAM systems that do parametric analysis on the parts and create complex 3D tool paths (circles, curves, etc). But I guess if your resolution is fine enough, you could get good enough results, but the tooling marks won’t look as nice. 

As you have realised, the Arduino would only be doing the calculations 1 slice at a time with a timer interrupt being used to keep the stepper motors stepping at an even speed.

Resolution is not a problem, these slides are metric with 1 turn = 1mm. Using a 200 step per revolution motor would give a resolution of 5μm with full steps. The stepper motor driver can produce 1/16th steps if required. I will be be happy with a resolution of 10μm.

Most people have recommended that I stick with generating the gcode on a PC and I am not 100% certain my trigonometry is good enough to write the Arduino code. If I can find something less horrendous than skeinforge that is free then I might just use that but ultimately, I would love to just feed an STL directly to the Arduino and walk away while it did the rest.

I do not know why you say STL is not a good choice.

  • It only supplies data on the outer shape (we don't want internal structure, colour or texture)
  • Most CAD software can generate STL
  • It's simplicity makes it easy to work with.

Considering the speed and power of something like a Pi or Atom board, they would spend 99% of their processing power twiddling their thumbs (remember Marvin).


700Mhz is on the low side for controlling a software powered machine with EMC. That is due to the fact they use software to generate the clock signals that cause the steppers to step, along with all of the other work that all has to happen in realtime and not just whenever the processor gets to it. The faster the processor, the faster the machine can be run.

Stepper motors and the machineing process is very slow, even by Arduino standards. If you try to run a stepper motor too fast it starts to miss steps and finally stalls and vibrates.

"The faster the processor, the faster the machine can run" is an old wives programmers tale!

It's not about how fast the software can spit out stepper commands, but about how even it can do it. Timing is important here.

If the stepper is rotating at a certain speed, and the suddenly your bloated OS decides that now is a good time to scan for updates over the Internet you could experience as small hick up. And when the stepper is rotating fast, a 1ms unexpected delay could make it loose count and your machine will be somewhere else than your code think it is.

I haven't followed the realtime linux development for a long time, but if you are going to generate steps directly on your main CPU you should do it in a realtime thread of the kernel.

I think I agree with most other people here in this thread, generate at much as possible on the PC, but let an uC clock out data to the steppers.

How much voltage have you pushed to your steppers that were loosing steps? Stepper based CNC machines usually get 5 to 10 times their stated voltage fed to them.

Yes you can use high voltage to help them rotate quicker but they are still much slower than a standard DC motor. The controller IC I will be using can work with voltages up to 35V. I have heard of industrial stepper motors being driven at 100V or more.

The point is, stepper motors are very slow compared to common DC motors and their maximum speed can easily be achieved with an Arduino.