GRBL with Arduino CNC Shield – Complete Guide

Learn how to use GRBL with an Arduino CNC Shield. In this post we’ll go over the Arduino CNC Shield, each of the pins, how to interact with it, installing GRBL to your Arduino, sending commands from your computer and running gcode from your computer.

What is GRBL?

GRBL (pronounced “gerbil”) is a free open-source software that’s used for motion control with Arduino boards that use the ATmega328. This enables us to use an Arduino UNO to operate CNC machines (laser cutters, 3D printers, etc). Basically, we can use GRBL and an Arduino UNO to operate any machine that has 3-axis.

Arduino CNC Shield Basics

The Arduino CNC Shield basically allows you to easily connect up to 4 stepper motor drivers and motors to your Arduino project while connecting them to very specific pins in the Arduino UNO. The GRBL software, which you have to deploy into your Arduino, knows which pins are used to control each stepper motor, so the Arduino CNC Shield and GRBL work very well together.

What if you don’t want to use GRBL?

Technically, you could use the Arduino CNC Shield without having to use GRBL. Say, you simply have a project that needs several stepper motors and want to connect them to your Arduino with ease, then you can just use the CNC shield and reference the appropriate pins to control each stepper motor accordingly.

As you can see, the Arduino CNC shield has very specific locations for each axis for your CNC machine. There is also an A-axis which can duplicate X, Y, or Z by using the appropriate pins (more on that below) or you could also operate 4 independent axis by using non GRBL software through pins D12 and D13.

Arduino GRBL CNC Shield
Arduino CNC Shield Axis

Arduino CNC Shield Pins

If you look at the images in this section, you will see that each of the axis (X, Y, Z, and A) have their own spot to connect their stepper motor driver (i.e. A4988) and stepper motor wires. On each of these spots, you also see pins to define the stepper motor micro stepping settings. For details on the A4988 stepper motor driver microstepping settings, please see my earlier post.

On the left side you can also see the pins to control the steps and directions of the stepper motors on X, Y, Z. These are connected to the Arduino pins and are there if needed, but you shouldn’t need to use these if the pins are being driven by outputs coming from the Arduino. Near the middle left you also see pins that can be used to enable the A-axis to mirror the X, Y, or Z axis.

Note that when using GRBL, you can only use the A axis as mirror and cannot independently control 4 axis. Without GRBL you could simply control all axes through their Arduino pins (shown on the images). On the right we have the pins for limit switches (allows for pins on both +end and -end if needed) and also have pins for control of externally driven spindle. Note that if using GRBL 0.9, the Z-limit is on pin D12 and Spindle PWM control on D11.

Arduino CNC Shield GRBL 0.9 and GRBL 1.1

How to use the Arduino CNC Shield – Basic Info

There are 2 basic ways to use the Arduino CNC Shield: running GRBL on the Arduino or running non-GRBL software on the Arduino (including regular Arduino code). If running regular Arduino code, you are simply using the Arduino Shield to benefit from a very simple setup that enables having up to 4 stepper motors being controlled by one Arduino in a very condensed format. Code for this would follow similar examples as shown on my earlier post. Just know that you have to make sure to use the pin numbers called out on the Arduino CNC Shield pinout.

If running GRBL software on the Arduino UNO, you have to send commands externally from another device to the Arduino UNO, which tell the Arduino UNO what to do. The simplest and most common way of doing this is by connecting a USB cable to the Arduino UNO and sending the commands from a computer running a controller software such as UGS (Universal G-Code Sender).

Arduino CNC Shield USB Input

How to Install GRBL on Arduino

To download GRBL, go to the following link to Github and download as a .ZIP file.

GRBL download

Once downloaded, extract the contents of the Zip File and go to Arduino IDE and proceed to add the .Zip Library by going to Sketch, Include Library, then Add Zip Library (see image below). Then go to the extracted folder, and within the grbl-master folder, select the “grbl” folder and click “Open”.

The next step is to upload the GRBL code, assuming your plan is to operate a standard CNC machine which uses all axis (X,Y,Z). But, if for example, you want to only use the X and Y axis, then you would have to disable the Z-Axis before installing GRBL on the Arduino, otherwise the homing sequence will always fail (will try to home the Z-axis without success). In such example, before installing the Arduino UNO code, go to your Arduino folder and within the Library folder find the “grbl” folder, then open it and find the “config” file. Open it as a text file.

Change homing cycle from 3-axis machine to 2-axis (disabling Z-axis)

Only do this if your machine will not have a Z-axis at all. If this is your case you will want to find this section of code within the “config” file:

And you’ll want to replace with this:
As you can see, we basically remove the step in which we home the Z-axis, and we now only home the X-axis and the Y-axis.

Continue installing GRBL on Arduino

Back on the installation of GRBL on Arduino UNO. Now with Arduino IDE open, go to “File” -> “Examples” ->”grbl” -> “grblUpload”. See the screenshot below. Notice that on mine I went ahead and changed the folder name as I completed the change mentioned above.

You might notice that the code is very short. Don’t worry it works fine! All you now have to do is hit the upload button (with the Arduino connected to your computer) and let the GRBL software load into your Arduino board.

GRBL upload to Arduino

CNC Machine Setup

Before we start running the CNC machine and configuring GRBL, let’s first take a look at my setup so you can relate to what I’ll be walking you through. My machine (shown below) does not have a Z-Axis. It has an X-axis, Y-axis, and then a motor for extrusion/dispensing. I also have limit switches along the X-Axis and Y-Axis to enable homing the machine. The machine is driven by stepper motors and timing belts/gears/etc along stainless steel rods and linear bearings/sliders.

CNC Custom Machine for GRBL use

From an electrical standpoint, my setup is as shown in the image. I drive the X-axis and Y-axis stepper motors through the Arduino running GRBL. Such Arduino also receives signals from limit switches for the X-axis and Y-axis. Spindle Enable and Spindle Direction signals are sent from the Arduino running GRBL to the Arduino running custom code. The Arduino running custom code then takes this signal to control the Dispenser Motor. Note that I installed GRBL 0.9, so my wiring is consistent with this. As you can see my PWM signal for the spindle control is coming from D11. Adjust accordingly if using GRBL 1.1.

CNC XY Machine Electrical Setup

GRBL Controller / G-Code Sender

Now that our Arduino has GRBL installed (I installed GRBL 0.9) and understand how the machine operates, we can proceed to install our GRBL Controller. As mentioned previously, I use USG (Universal G-Code Sender). To install USG in your computer, go to https://winder.github.io/ugs_website/, and click “Download” on the top menu. Then proceed to download.

Setting up your CNC machine on UGS

Connecting UGS to your machine

Once the USG (Universal G-Code Sender) is installed and opened, connect your CNC machine to power and to your computer via USB. Then proceed to the top menu and click on “Machine” -> “Setup wizard…”. Once the pop-up window shows up, you will need to complete step 1 by entering your firmware “GRBL”, your port rate “115200”, and your Port, which for my scenario is COM3. Then click on “Connect”.

GRBL UGS Setup
GRBL Firmware Connection UGS

Once you connect, you will see a message on whether or not the connection was made and it will tell you the firmware that it was connected to. Mine says “Connected to Grbl 0.9j”. If you have issues completing this step, make sure you selected the correct firmware, port rate, port, and that you successfully installed GRBL on your Arduino board.

Connected to GRBL 0.9

Next, you can import setting in the case your machine came with a configuration file. Assuming you built a DIY custom machine (as it was my case), we skip this step and click “Next”.

UGS set up for your motor wiring

In this step you have to press the buttons “X-“, “X+”, “Y-“, “Y+”, etc and confirm on your machine that when you press “X+” that your machine moves in the “X+” direction. If you press “X+” and you see your machine move in the “X-” direction, then you need to press the “Reverse direction” checkbox. Note that the positive and negative direction will define the future movements of your machine relative to the gcode you send. “X+” should be to the right and “Y+” should be vertically up, relative to the origin from the point of view you look at it. Just make sure to be consistent.

Step calibration

In this step, you should simply step your stepper motors and measure the actual movement to obtain a actual system steps per millimeter value. This will then enable the system to accurate translate mm movement commands into step commands to allow your stepper motors to move the necessary number of steps to get you the required displacement. You must to this for every axis in your machine. In mine that is the X-Axis and Y-axis. Once you step, measure the value and enter under “Actual movement”. This will then calculate a value under the “Calibrate” column. Proceed to enter such value under the textbox under the calculated value and then click “Update”. Make sure yo do this for every axis you are using. No need to do it for unused axes.

CNC Calibration setup
CNC Calibration setup

Limit switches setup

In this step you enable the limit switches by clicking the checkbox next to “Enable limit switches”. If needed, you can invert the limit switches. You can also proceed without limit switches, but note that this would remove the ability to home the machine. Once you setup your limit switches, click “Next”.

Setup homing

Before we begin, remember that if you are running a 2-axis machine, you will need to adjust your homing sequence by editing the code within the “config” file BEFORE you install GRBL on your Arduino. Once you do that you can proceed. For more info on this, scroll above on this page and check the section on installing GRBL to Arduino.

If you want to enable homing, click the “Enable homing” checkbox and ensure to properly call out the direction in which the machine will have to move to reach the home limit switches. Once that is done, click “Try homing”, if the machine is moving in the WRONG direction, then click “Abort”. Once you complete these steps, click “Next”.

GRBL Setup Output UGS Command

CNC Machine Soft Limits

If desired, in the final step, you can enable the use of Soft Limits on your CNC machine. These allow you to prevent your machine from moving within the CNC machine’s work area. I normally don’t turn this setting on as I do enable homing and the gcode I run only drives movements within the work area.

Additional setup of CNC machine beyond UGS

There are other setting that you could edit for your CNC machine. You might notice on the console portion that a code shows up every time you connect your CNC (see image). One particular setting that I changed was $27 (homing pull-off, mm). This basically defines how far out the machine will move AFTER completing the homing step.

UGS GRBL setting output

In my machine the limit switch would remain engaged under the standard pull-off, so I increased it to 4.5mm. To do this I simply wrote the following on the command line: “$27 = 4.5”. You can also see that other settings look similar to what you entered while following the configuration steps. For example, $100, $101, and $102 are used to define the steps per mm on the x, y, and z axes, respectively. Net, your CNC controller (the Arduino) is mainly reading the commands sent to it.

How to get your GRBL CNC Machine to move

When starting up a new gcode we will first run the following commands:

  • G21 – To set the units to millimeters
  • G90 – To set the mode to absolute positioning (all coordinates are relative to origin)
    • If you wish, you could use G91 to define all coordinate relative to last position.
  • G17 to set the current plane as XY (default).
    • You could also use G18 or G19 to set the current plane as ZX or YZ.
  • G40 and G49 to cancel radius and length compensation.

Next, we would execute a rapid move with G0, which would essentially take us quickly to the beginning of the point in which we want to start working on. After this, we would turn on the Spindle with M3 and define the speed with S followed by the speed (from 0 to 1000), so example would be M3 S1000. In my case the spindle would be the dispenser/extruder I plan on setting up.

Then we define the rate at which we want to perform the XY plane movements with F followed by the speed (i.e. F400). This would make all subsequent XY plane moves with G1 command to be executed at a speed of 400 mm/min. Note that the feed rate is in mm/minute because we called the G21 command, which sets the units to millimeters. Had we called the G20 command, the units would have been in inches and the feed rate would be in inches/minute.

Simple Example of GRBL g-code for CNC

We will start with the CNC at the origin (X=0, Y=0) and set the appropriate starting parameters. We will then perform a rapid movement to (X=-50, Y=-50). Then, at 400 mm/min, we will move 50mm in the Y- direction, then 50mm in the X- direction, then 50mm in the Y+ direction, and finally 50mm in the X+ direction. We will then return to the origin with a rapid movement, and then end the program. The code for this will look like this:

UGS Visualization of the code above

The following visualization comes right out of UGS (except the red arrows and numbers I added to tie back to the code so you can see how each command leads to each movement). Note that my machine starts at the origin and then its available space is in the negative X and Y direction, as a result, my coordinates are negative, adjust your code accordingly.

GRBL gcode and output

Ways of executing g-code commands on UGS

Directly enter g-code commands into the Console

You can always enter commands one by one into the command window, which allows you to see them get executed one by one. This can be particularly helpful when you are learning to run your new CNC or learning gcode in general. It can also be helpful when troubleshooting.

UGS GRBL Command Line

Execute g-code from file with full program

You can enter the g-code into a text file and run it from there. Simply start a new text file in your computer, enter the g-code, save it. Then within UGS, go to “the “File” -> “Open” and select the file. You can also open by clicking on the folder icon.

Once you open, the g-code will pop-up in a new tab. Feel free to select the tab and drag it to a side. I like dragging it to the right to I have the code on the right and the visualization on the middle.

GRBL gcode and output

G Code for example above

Important G-Code Commands

G Commands

G0 – Rapid Move:
Move the CNC at the max speed to the defined coordinates in a straight line. Typically used to move from a starting point (origin) to the beginning point of a job (in the previous example from this is movement #1). During this movement, the CNC does not perform it’s main function (no 3D printer extrusion, no laser cutting, no cutting with lathe, etc).

Example: G0 X 50.000 Y 50.000

G1 – Linear Move:
Move the CNC at the specified speed (feed rate). We define either the displacement or the end position (depending on whether we previously specified G90 or G91). G1 is normally used to perform movements while the CNC performs its main function (3D printer extrusion, laser cutting, etc).

Example: G1 X 50.000 Y 100.000

G2 – Circular Clockwise Movement:
G1 performs straight line movements, but what if we need to move along an arc? Then we use G2 (or G3). G2 allows us to move along a curved line in the clockwise direction. Similar to G0 and G2, we need to define the coordinates for the end point, and we also need to define the location of the center of rotation (radius of the arc on which we are traveling along).

Example: G2 X 100.000 Y 100.000 I 75.000 J 75.000

Where I 75.000 defines the x offset of the center of the arc and J 75.000 does the same for the y coordinate. Now, what happens if we edit movement #3 of the previous example with the following:
G2 X -100.000 Y -100.000 I -25 J25 ?

GRBL gcode and output

G3 – Circular Counterclockwise Movement:
G3 works the same way as G2, with the exception that is meant for movements along curves in a counterclockwise direction.

G17 / G18 / G19 – Plane Selection
These are used to define the plane in which work takes place. G17 for XY plane (default), G18 for ZX plane, or G19 for YZ plane.

G20 / G21 – Define units of length (millimeters or inches)
Use G20 to set the length units as inches, or G21 to set the length units as millimeters. Note that this will then impact the feed rate units as it will be in either mm/minute or in/minute.

G90 / G91 – Define the positioning style (absolute or relative)
G90 is the absolute style, and movement commands (G0, G1, G2, G3) will move the machine to the defined coordinates. While G91 is the relative style, and movement commands will move the machine to the relative coordinates provided (i.e. it treats the current location as zero/origin instead of the actual position). The examples above used absolute.

M commands

M0 – Unconditional Stop
This stops the execution of the gcode.

M2Program End
This denotes the end of the gcode.

M3 – Spindle On (clockwise)

Running M3 activates the Spindle pin. Note that the output will be a PWM signal which can be set anywhere from 0V to 5V. The specific output voltage is controlled by defining the speed (from S0 to S1000). Such output will come out of the Spindle On pin, either D11 or D12 depending on which GRBL version you are running.

Examples:
M3 S0 results in an output voltage of 0V.
M3 S500 results in an output voltage of 2.5V
M3 S1000 results in an output voltage of 5V

CNC Shield Spindle Output

Another important output is what comes out of the D13 pin. When running M3, which is a clockwise output, you will get a LOW output on D13 (equivalent to 0V).

M4 – Spindle On (counterclockwise)
This works the same way as M3, with the exception that that pin D13 (Direction pin) will output a HIGH (equivalent to 5V).

Note that for the spindle what’s critical is what you do with that direction signal (D13) on the machine you design. What do you want it to do when D13 is LOW? and what do you want it to do when D13 is HIGH? Think about that.

M5 – Spindle Stop
Use this command to stop the spindle.

Others

$H – Home CNC Machine
Use this command to execute the homing sequence.

$X – Unlock
Use this command to unlock CNC after Soft Reset from hard limit switch activation.

Components used in this example

*As an Amazon & Ebay Associate I earn from qualifying purchases.

GRBL CNC Shield and Arduino

ComponentLink
CNC Shield for Arduino UNOhttps://www.amazon.com/gp/product/B01D2HL9T8/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1
Arduino UNOhttps://amzn.to/3uYVAMC

CNC Machine Components

ComponentLink
Extruded Aluminumhttps://www.amazon.com/gp/product/B09BFNYMND/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&th=1
NEMA 17 Stepper Motorshttps://www.amazon.com/gp/product/B091Y1GWNR/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1
Extrusion Motor Assemblyhttps://www.amazon.com/gp/product/B07K7FQ245/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&th=1
Power Distribution Striphttps://www.amazon.com/gp/product/B08PT2LNXL/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1
Power Supplyhttps://www.amazon.com/gp/product/B0865L4GZ7/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&th=1
Linear Bearing Systemhttps://www.amazon.com/gp/product/B09DCLNWXR/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

Leave a Reply

Your email address will not be published. Required fields are marked *