Direct CNC Control With The Raspberry Pi

If you’re building a CNC router, laser cutter, or even 3D printer, you’ll usually be looking at a dedicated controller. This board takes commands from a computer, often in the form of G-Code, and interprets that into movement commands to the connected stepper motors. Historically this has been something of a necessary evil, as there was really no way to directly control stepper motors with a computer fast enough to be useful. That may not be the case anymore.

A stepstick driver

Thanks to the Raspberry Pi (and similar boards), we now have Linux computers with plenty of GPIO pins. The only thing missing is the software to interpret the G-Code and command the steppers over GPIO, which thanks to [pantadeusz], we now have. Called raspigcd, this software interprets a subset of G-Code to provide real-time control over connected steppers fast enough to drive a small CNC router.

Of course, you can’t directly control a beefy stepper motor to the GPIO pins of a Pi. You’ll let out all the magic smoke. But you can wire it up directly to a stepper driver board. These little modules connect up to a dedicated power supply and handle the considerable current draw of the steppers, all you need to do is provide them the number of steps and direction of travel.

This method of direct control offers some very interesting possibilities for small, low-cost, CNC projects. Not only can you skip the control board, you could conceivably handle the machine’s user interface (either directly via a touch screen or over the network) on the same Pi.

We’ve seen attempts at creating all-in-one Linux stepper controllers in the past, but the fact that anyone with a Raspberry Pi 2 or 3 (the boards this software has currently been tested on) can get in on the action should really help spur along development. Has anyone used this?

46 thoughts on “Direct CNC Control With The Raspberry Pi

    1. It is possible (or it used to be) to build a PREEMPT_RT version of the RasPi kernel, though not trivial. This will then let you run a real-time thread.

      You should note that *all* software has random latency issues, just sometimes they are relatively small.

      1. Agreed. I run a couple LinuxCNC machines, and even though the kernel is compiled with real-time support, it is still recommended to run a latency test to determine the fastest safe/reliable step rate your computer can achieve.

    2. This probably isn’t an issue if the steps are timed enough cycles apart. This way the random latency cancels itself out to some degree that might be enough. Steppers aren’t that fast, after all.

    3. Even better, you can exclude everything else from running on one core (via kernel command line) and then so long as you don’t go making blocking system calls you can basically treat that core as your own. Hit the gpio pins by memory mapped I/O and you’re good to go.

      1. It’s still non-deterministic though. You don’t know about the number of cycles spent waiting to access shared memory, the precise time it will take any code to execute (the joys of out-of-order execution) etc. There’s a reason that ARM designed the A series and M series cores. The A’s are faster, but you know exactly how long something is going to take on an M, plus it’s been optimised for IO.
        A cheap $1 STM32 is going to be far better at the job than any Core A without dedicated hardware.

        1. Last time I looked at the Machinekit the pru driver was written as some “one program does everything”. It ran the stepper algorithm, did some plc like stuf and maybe more (can’t remember well).
          Maximum step frequency was some 20 to 30kHz. plenty for “real” steps, but not adequate for high microstepping to get the motors quiter.
          Only a single PRU was used, the otther was twidling it’s thumbs.
          It was a real bummer when I discovered that.
          Another project used the PRU as stepper driver and could get upto 1.5MHz step rate. Plenty and with headroom to spare for high microstepping (On github, can’t rember the project’s name).

          If I had known this in advance I would have simply bought a mesa FPGA card and be done with it.
          It’s not like EUR100 is a serious difference in a decent CNC machine.

          GRBL on Cortex M3 might be an option for me.
          Probably fast enough, and software on a lever that I can comprehend and fit into my head.

      1. No. Arduino has no particular operating system to speak of; your code runs on the chip and that’s (just about) it. It’s slightly less advanced than MS-DOS.

        Linux running on a Raspberry Pi has a modern operating system which is better than Arduino in almost every respect – except that it doesn’t do deterministic timing without substantial modification.

        1. ‘better’- 95% of us just want an easy way to wire up our diy cncs. I’d like to just order the stepper drivers, hook them up to the pi zero and the CD drive steppers, change whatever variables are required (all accessible easily) and fire away with your new baby cnc

    4. It spawns thread (https://github.com/pantadeusz/raspigcd/blob/743d26df3961349a16cee3913f472dba26f59822/src/m_hwmoves.cpp#L169) that is of highest priority (https://github.com/pantadeusz/raspigcd/blob/743d26df3961349a16cee3913f472dba26f59822/src/m_hwmoves.cpp#L173). It needs to be run on root. The real-time kernel is not required. It works on Debian provided by Raspberry Pi Foundation. It does not yet support full set of G-Code, but enough to execute simple programs (see how it works on https://www.youtube.com/watch?v=KTBCnEE6s4s). The example program is at tests directory: https://github.com/pantadeusz/raspigcd/blob/master/tests/unit_gcd_engine_gcdtest.gcd – it is similar to the one on the video.

  1. I have an ANET A8 3D printer that is making a great paperweight on my shop desk. I’ve never been able to figure out why it freezes up during a print. The heated nozzle and block always seem to leak which sucks. So why am I bringing this up here on this discussion? Well, if I can’t get the 3D printer to print, I’m wondering if there is a way to convert it to a CNC instead? Or if someone wants to contact me with how to fix this thing, I’d really like that. Either way, it’s good to hear someone has overcome the CNC issue presented here. Awesome. Now if I had plans to implement this whole thing, it would be even more useful to me. CNC’s are expensive. DIY would be great.

    1. Even the beefiest 3D printer will make a lousy CNC router/mill. FDM doesn’t put a ton of stress on a frame so there is no need to built it super robust. Random freezes are often an issue of your bootloader or arduino distro. I’d reflash the bootloader (you’ll need an ISP) and then find the appropriately configured Marlin or reprapfirmware and flash that. There was a Ardunio Mega/Taurino bug in the bootloader that would freeze the printer and return a single character serial stream that plagued the 3D printer community for a little bit.

    2. In general, a RAMPS controller with Marlin firmware is the solution for goofy control electronics.
      The firmware will check the heating control feedback for anomaly, and shutdown th heaters if thermometers are too slow/disconnected. RepRap setups are very reliable, but to use them for CNC is ridiculous for anything outside engraving (GRBL is the same, but offers more options).

      The other option is a Prusa i3 mkII if you want to avoid the pain of setting up your own system.

    3. LOL, yeah.. good luck converting an A8 in to anything close to a subtractive machine. You would be hard pressed to get it to do simple engraving in butter. It is not even close to a rigid machine, and just barely rigid enough for FDM.

      Like Jake said , even the most heavy duty, rigid 3D printers are just barely capable of being used as even a rotary engraver. Will be a complete waste of effort.

      1. I can confirm that, I needed to print about two spools of ABS worth of braces and some threaded rod supports to make my A8 even print right. ABS just for the higher glass transition temperature, to keep it from sagging next to the hot end and heated bed. Also makes welding parts together with acetone a possibility if you want braces that are larger than the print bed with no hardware.

        It’s a fun project if you know what you’re getting into, but if you wanted to convert it to a subtractive machine I’d go out and get a welder and some scrap metal and begin from scratch instead. For the record, I wanted to get an A8 and improve it instead of simply buying a good printer from the get-go because it seemed like a good way to practice my CAD and mechanical design skills. It was. I definitely learned enough to justify the hours of labor spent.

        I’m currently working on putting in a RAMPS board to replace the cornballer of a control board that comes with the A8. No thermal runaway protection? I think my toaster is more advanced than that. Hopefully that’ll be the last safety upgrade it needs. Might even add dual extrusion as well! Who knows.

        So far my A8 works as a passably decent filament printer with PLA, ABS, TPU, and PETG, and I printed out a bracket to hold a sharpie so I can use it as a small plotter. Just for funsies. But a plotter or a laser is about as much as you’ll get out of that frame; any kind of rotary tool is going to exceed its structural integrity and lead to rapid unplanned disassembly. This project begs to differ: https://www.thingiverse.com/thing:2508924 but notice just how heavily reinforced it is and how much of the machine is new growth. I don’t know if that’s the route I would take personally.

        1. The extra parts are not needed,you have to take your time when assembling and ensure everything is nice and SQUARE, also when the Z is adjusted for zero it is best to use digital calipers and insure the Z is perfectly level to the build plate also the z motors must be in sync step wise so both motors are on the same step not one on step 4 and the other on step one,also take time and build a nice wire harness when you lay in wiring.

    4. I DIY’d my own machine with the Shapeoko for about $1100. Since it’s 2’x4′ and already that much money I set it up with a refurb desktop. I feel silly calling it a CNC because next thing I know I’m being asked to make a crankshaft or something, but I find there’s a lot of market segments you can jump in on with a larger format machine.

  2. I’m set up through work to take an introductory course in CNC/mill/lathe programming. Any tips for a noob? What should I pay particular attention to, besides everything?

  3. “Historically this has been something of a necessary evil, as there was really no way to directly control stepper motors with a computer fast enough to be useful”. Ahum… that’s not entirely true, because I’ve been using “Mach3” for many years and it does exactly that.

    But what a great development to read about this working on a raspberry pi! Really, very very cool, I’ve been looking for this for a long time, although a few years too late for me perhaps. But in the future it might be of great use I’m sure!

    1. In response to this blarney:

      “Historically this has been something of a necessary evil, as there was really no way to directly control stepper motors with a computer fast enough to be useful”

      I’d like to add..

      Except for every single piece of equipment that uses steppers, from floppy drives to receipt printers and of course LinuxCNC and Mach3, which you mention, that also directly also provide a pulse train to the steppers.

      A less than PC sized full G-Code interpreting hackable controller is the holy grail, and there are many contenders. There is GRBL for Arduino, there was an attempt to put a port of LinuxCNC on the Beaglebone and on the the Raspberry Pi. People have made a CNC control for the Propellor chip and I would be amazed if there was not a Pic version or several out there.

      There are some interesting standalone China made CNC controls on eBay, some with even built in stepper drivers, but I seem to have a thing for the price point of old off-lease Dells and ebay stepper drivers, combined with the flexibility of LinuxCNC.

      1. I use a kflop on my cnc mill, it can handle all sorts of fun stuff like dual encoders, rigid tapping, pretty much anything you can write in C. i think there is only windows support but you could tie that to a microatx pc or an intel NUC and get a very small system.

      2. So maybe the term “Historically” depends on your perspective here, but running a CNC with something like Mach3 is definitely not common anymore. You need a 32bit Windows OS running on a machine with a parallel port…it’s fair to say there haven’t been many NEW Mach3 installs/setups recently.

        For the last 15 – 20 years, you’ve definitely needed a motion controller.

    1. As someone who is interested in doing just that – using a Pi to drive an Arduino Uno to build a ghetto CNC machine, do you have any tips/tricks/links/guides that would be a good place to start?

  4. This is new? I thought that Linux CNC was already ported to the Pi and already could use the GPIOs.

    Besides, what great expense does this save? An Arduino clone is cheap. Plus you will still want some sort of motherboard to place those step sticks on. I haven’t seen one with ready positioned connectors to plug into a Raspi GPIO header yet. Though… I wouldn’t be that surprised if it’s out there. Comparing the time required to wire up all those StepSticks to their GPIOs, power source and motors… And then.. if there is any chance of latency problems… I’d rather just buy the RAMPS/Arduino combo and not worry about all that.

    Also, does anyone have experience with the RTL kernel extensions? If the stepper control is guaranteed real time does the UI suffer? Does it trade motor jitters for UI responsiveness issues?

  5. This type of use of a RaspyPi is a engineering non sense:
    RT kernel is not enough to ensure proper fail safe RT timing, some DMA access must be implemented for GPIO.
    Much more elegant solution is to drive with a RaspyPi a standard microcontrollor like with klippers FW.

    1. Engineering is not about building hard-realtime failsafe systems for small hobby CNC mills that are made out of plywood. Using just a raspberry is cheaper and is on the level of cheapness of the rest of the machine.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.