Add new functionality to affordable CNC machines using CNC.js and balena

Here’s a simple way to add premium CNC machine features to low-cost options using CNC.js and balenaOS.

Here’s a simple way to add premium CNC machine features to low-cost options using CNC.js and balenaOS. Now you can outfit your shop with incredible maker tools without breaking the bank.

Here's a balena logo scribed by one of my shop machines

Fundamentally, I’m a maker at heart. I’ve been making things for years, but it’s only recently that I have had the time, space (and, yes, cashflow) to start fitting out my workshop with the machines I’ve always wanted. I’ve had my eyes on machines like the Shapeoko from Carbide 3D for quite some time, but it’s only over the last few years (and thanks largely to the 3D printing revolution) that we’ve started to see smaller, more affordable hobby versions of these machines.

Unfortunately many of these machines operate with very limited controllers and often with no way to control the machine without a PC connected. Well, until now.

What’s a CNC machine?

Before we dig in, let’s go over a few fundamentals. If you’re wondering what is a CNC machine is, CNC stands for Computer Numerical Control. Here are some examples that you’ve probably come across on some cool video on the internet or in a friend’s garage or workshop.

If you’re up to speed on all that and want to get into the action, click here.

3D Printer

The most common form of 3D printing is FDM or Fused Deposition Modeling which moves a tool (the extruder hot end) along three axes (X, Y and Z) to place melted plastic in layer after layer to produce a 3D object. There are other 3D printing technologies available too but they all basically do the same thing with different methods and materials.

Here's a 3D printer doing it's thing

Above is a timelapse video of my 3D printer printing a part for one of my robotics projects.

Laser Cutter/Engraver

A laser cutter uses a high power laser beam as its tool and moves it in the X and Y axes. Traditionally, these have been big expensive machines with large fixed CO2 laser tubes which shoot through a series of mirrors mounted on the X and Y axes directing the beam.

Here's my own laser cutter scribing the balena logo

Recently, with the advent of smaller and cheaper (and much less powerful) diode lasers, these machines have become a lot more accessible. They can be used to cut parts out of a flat material or engrave a design on a surface.

Router/Milling Machine

A router uses a specially designed bit mounted in a high-powered spindle to remove material from a workpiece in precise motions (called toolpaths). Think of it like a drill mounted on a gantry or arm that moves it along the X, Y and Z axes (sometimes even more axes!). Instead of only drilling holes, it uses special bits that can cut sideways (route), engrave or any number of other cutting operations.

Above is a short timelapse of my CNC machine cutting robot parts out of acrylic.

Using G-code to interface with a CNC machine

Most of these machines use a language called G-code to control the machine, usually via a serial connection. This requirement often leads people to expose their laptops to the potentially harsh environment of a workshop or have a sacrificial PC. All my machines provide a USB serial connection.

Integrating CNC.js

CNC.js is an open source project which provides a server and nice web interface for managing a CNC machine running a number of common open source controller firmwares. It’s designed to run on a Raspberry Pi or other Linux-based computers. This is a much lower cost or risk than to use a larger, more expensive device.

Let’s build this use case

For the purposes of this demo, we’re going to use a Raspberry Pi to control our CNC machine and monitor its progress. The Raspberry Pi will be configured with balena-cncjs with the following services:

  • cncjs: the actual CNC.js server connecting over serial to our machine and serving its web interface on port 8000
  • mjpg-streamer: allows us to stream video from an attached camera, if present
  • proxy: a NGINX reverse proxy which allows us to connect to the CNC.js interface and the camera stream over port 80
  • mdns: a service which advertises the balenaCloud device name on the local network to make connecting to it nice and easy

Hardware required

  • A balenaOS-supported device with an available USB port, eg. Raspberry Pi
  • A SD card rated for your device
  • A power supply
  • A CNC machine with a controller running Grbl, Smoothieware, TinyG or Marlin firmware.

Software required

Setting up

First let’s set up the balena device:

  • Sign up for a free balenaCloud account
  • Create a new balenaCloud application
  • Add a device and download the balenaOS disk image from the dashboard (don’t forget to enter your WiFi details if needed!)
  • Use Etcher to Flash your SD card with the balenaOS disk image and boot the device
    After a few seconds it should connect to the internet and show up on the balenaCloud dashboard.

Once your device is provisioned and booted, it should show up on your balenaCloud dashboard.

Note: you may want to use this opportunity to rename the device to something more memorable using the small pencil icon next to the device name, as I’ve done here.

Deploy the project code

Once the device is connected and showing in the dashboard, it’s time to deploy the application that includes all the services.
Download the balena-cncjc project and unzip the file. If you are familiar with git, you can also use git clone.

Clone or download the balena-cncjs repo however you choose.

Using the above-mentioned balenaCLI, run the command balena push <appName> where <appName> is the name of the application you created with the balenaCloud dashboard earlier.

If all went well, you’ll see the balena unicorn mascot (named Charlie, for the record) and the code you’ve just pushed will automatically be distributed to the devices in your application. You should see the services running in the dashboard:

You're on the right track if you see these services running on your device (via the balenaCloud dashboard)

Connect your CNC machine

Connect your CNC machine to the Raspberry Pi using the appropriate USB cable and power it on.

Raspberry Pi connected to controller

Load the CNC.js interface in your browser

In your web browser, open the URL https://<deviceName>.local/ where <deviceName> is the name of the device in the balenaCloud dashboard. In my case that’s https://laser-cutter.local/.

Here's an example of drawing the balena logo.

Select the serial port in the connection dropdown (top left widget) then click connect. CNC.js should detect the device’s firmware, however in some cases (particularly with older firmwares) you may have to specify it.

Once connected you’ll be able to use the Axes widget on the right to jog your tool around the work area and zero the location. You can drop your G-code file on the large visualisation area in the center of the screen or click the “Upload G-code” button. Use the play button to start your job.

Generating G-code

The process of generating G-code is called CAM (Computer Aided Manufacturing) processing and this largely depends on the sort of machine you’re using. Here are some of my recommendations:

  • If you’re doing 3D printing then you need a Slicer to generate G-Code (I use Cura)
  • For laser cutters I use Lightburn, although I’m thinking about trying out LaserWeb – maybe a future improvement to this project
  • For routing and milling I use the built-in machining tools in Fusion 360, but that’s largely because I use Fusion 360 for my design workflow

One more thing: access your devices remotely

Since we’re using balenaCloud with this project, you can use balena’s public URL feature to check device status remotely. This is a big advantage of using balenaCloud for your CNC machines instead of just a Raspbian with a manual setup.

Machines used in this article

Whilst firmly “hobby grade” I’ve been having great luck with the machines I have here at home, especially after making them remotely manageable with the Balena platform. For those interested in the more technical details.

Router

CNC 3018 Pro
My CNC router is a Genmitsu 3018 CNC Pro which I have modified by way of 3D printed upgrades to add limit switches, a drag chain and a Raspberry Pi camera mount. All the upgrades pictured are available on Thingiverse.

Laser cutter

CNC 3018 Pro
My laser cutter is very similar to this one from Banggood with a 10 watt diode laser.

With great power comes great responsibility…

Now you’re equipped with the ability to take an affordable CNC machine, outfit it with balenaOS, CNC.js and some open source software and turn it into a serious maker machine. If you get this going in your shop, we’d love to hear more about it and take a look at your projects. Similarly, if you get stuck or have any questions, let us know in our forums, on Twitter, on Instagram or on Facebook.

Recommended reading

In this post we set up a Raspberry Pi to drive our CNC machine using freely available open source software. If you’re interested in 3D printing, laser cutting or routing here are some resources you may find helpful:

  • Getting Started with CNC: a great intro to engraving, routing and 3D carving with desktop CNC machines
  • All3DP: – a site dedicated to 3D printing and other CNC information
  • Thingiverse: a maker’s social network that encourages the sharing of designs and projects for 3D objects

Thanks for reading. Good luck and have fun, maker!


Posted

in

Join the discussion at forums.balena.io