Controlling an Arduino controlling a CNC

After quite some work writing test sketches for stepper motors I look at the next step in order to control my 2 soon digitized milling machines. Writing dedicated sketches for different CNC operations is one simple way to go. The higher level would be writing G-code and sending it to the Arduino. I see that I need a G-code interpreter installed in my UNOs. Sending the G-code instructions is the next question to solve. Using Serial monitor typing the lines one by one might work as a test but not much more.

Can anyone of You tell me a suitable architecture of the higher level software to achive this?

Can anyone of You tell me a suitable architecture of the higher level software to achive this?

Google GRBL.

So I did. Now I have a grbl folder in the Arduino/library.

Somehow I am supposed to compile the grbl and download it to my UNO.

Started with a new skect, included grbl and compiled. Lots of errors.

The “code” looks like:

#include <eeprom.h>
#include <gcode.h>
#include <grbl.h>
#include <jog.h>
#include <limits.h>
#include <motion_control.h>
#include <nuts_bolts.h>
#include <planner.h>
#include <print.h>
#include <probe.h>
#include <protocol.h>
#include <report.h>
#include <serial.h>
#include <settings.h>
#include <spindle_control.h>
#include <stepper.h>
#include <system.h>

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:


I’m missing things…

Can You tell me what?

Sorry, I missed some lines.

Trying again:

#include <config.h>
#include <coolant_control.h>
#include <cpu_map.h>
#include <defaults.h>
#include <eeprom.h>
#include <gcode.h>
#include <grbl.h>
#include <jog.h>
#include <limits.h>
#include <motion_control.h>
#include <nuts_bolts.h>
#include <planner.h>
#include <print.h>
#include <probe.h>
#include <protocol.h>
#include <report.h>
#include <serial.h>
#include <settings.h>
#include <spindle_control.h>
#include <stepper.h>
#include <system.h>

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:

Next try, and it downloads!

#include <grbl.h>

How do I proceed?

Opening Serial Monitor I read:

Grbl 1.1f ['$' for help] ⸮

Typing $ and Cr gives nothing.

Can You tell the next steps?

Can You tell the next steps?

Post ALL of your code. The code you posted so far does absolutely nothing, and did NOT produce the output that you claim it did.

I agree with You, something is wrong.

Compiling that h-file gives the following in the orange error window in the window. Attached below.

I have got into and try to follow it. Sometimes opening folders doesn't show what the advice tells. Then I'm lost, making desperate tries to make something show up.

Archiving built core (caching) in: C:\Users\User\AppData\Local\Temp\arduino_cache_246674\core\core_arduino_avr_uno_0c812875ac70eb4a9b385d8fb077f54c.a
Sketch uses 30562 bytes (94%) of program storage space. Maximum is 32256 bytes.
Global variables use 1633 bytes (79%) of dynamic memory, leaving 415 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:\Users\User\AppData\Local\Temp\arduino_build_692663/grblUpload.ino.hex:i 

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
         Copyright (c) 2000-2005 Brian Dean,
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM4
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 3
         Firmware Version: 4.4
         Vtarget         : 0.3 V
         Varef           : 0.3 V
         Oscillator      : 28.800 kHz
         SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "C:\Users\User\AppData\Local\Temp\arduino_build_692663/grblUpload.ino.hex"
avrdude: writing flash (30562 bytes):

Writing | ################################################## | 100% 5.93s

avrdude: 30562 bytes of flash written
avrdude: verifying flash memory against C:\Users\User\AppData\Local\Temp\arduino_build_692663/grblUpload.ino.hex:
avrdude: load data flash data from input file C:\Users\User\AppData\Local\Temp\arduino_build_692663/grblUpload.ino.hex:
avrdude: input file C:\Users\User\AppData\Local\Temp\arduino_build_692663/grblUpload.ino.hex contains 30562 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 4.74s

avrdude: verifying ...
avrdude: 30562 bytes of flash verified

avrdude done.  Thank you.

Trying to get forward I have tried to get hold of the grbl code i need to compile and download. I just haven't found that code. I have been exploring Github and grbl advice but I haven't found any opening. There are lots of unrelevant information but where is the piece i need?

Compiling that h-file gives the following in the orange error window in the window.

You do NOT compile header files. You compile source files that MIGHT include header files.

If you can not compile some code, it is absolutely pointless to claim that that code produced some output.

I know, thought it looked strange. I also found ab EEPROM test program in the Grbl folder, compiled and run it. Still I haven't found the code I believe I must compile and download. Did You read #8?

If grbl is properly installed in the sketchbook libraries folder all that you need to do is to go to Files, Examples, grbl and load the grblUpload example then upload the grblUpload file to your Uno. I that what you did in reply #7? It is normal to get the low SRAM warning. Make sure that serial monitor is set to 115200 baud to talk to grbl from the serial monitor.

The higher level would be writing G-code and sending it to the Arduino.

There are several programs for sending G code to grbl. Universal Gcode Sender is one.

How you write the G code is up to you. You can write the commands in a text file that UGS can send or use a CAM program to generate G code from a drawing.

Hi gF!

I guess something is wrong in my installation. It was done a long time ago but never used. In the Arduino library i have folder named grbl. following that I end up with a file like this. There is no grbl folder in the sketch folder.

This sketch compiles and uploads Grbl to your 328p-based Arduino! 

To use:
- First make sure you have imported Grbl source code into your Arduino
  IDE. There are details on our Github website on how to do this.

- Select your Arduino Board and Serial Port in the Tools drop-down menu.
  NOTE: Grbl only officially supports 328p-based Arduinos, like the Uno.
  Using other boards will likely not work!

- Then just click 'Upload'. That's it!

For advanced users:
  If you'd like to see what else Grbl can do, there are some additional
  options for customization and features you can enable or disable. 
  Navigate your file system to where the Arduino IDE has stored the Grbl 
  source code files, open the 'config.h' file in your favorite text 
  editor. Inside are dozens of feature descriptions and #defines. Simply
  comment or uncomment the #defines or alter their assigned values, save
  your changes, and then click 'Upload' here. 

Copyright (c) 2015 Sungeun K. Jeon
Released under the MIT-license. See license.txt for details.

#include <grbl.h>

// Do not alter this file!

I Googled on grblupload and fond this on grbl/main.c at master · grbl/grbl · GitHub.
Modified some main_loop() into loop(), compiled and downloaded it. I don’t know much what I have done. Completely wrong?

  main.c - An embedded CNC Controller with rs274/ngc (g-code) support Part of Grbl

  Copyright (c) 2011-2015 Sungeun K. Jeon

  Copyright (c) 2009-2011 Simen Svale Skogsrud

  Grbl is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by  the Free Software Foundation,
  either version 3 of the License, or (at your option) any later version.
  Grbl is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of

  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with Grbl.  If not, see <>.

#include "grbl.h"

// Declare system global variable structure

system_t sys; 

//int main(void)
void setup()

  // Initialize system upon power-up.

  serial_init();   // Setup serial baud rate and interrupts

  settings_init(); // Load Grbl settings from EEPROM

  stepper_init();  // Configure stepper pins and interrupt timers

  system_init();   // Configure pinout pins and pin-change interrupt


  memset(&sys, 0, sizeof(system_t));  // Clear all system variables

  sys.abort = true;   // Set abort to complete initialization

  sei(); // Enable interrupts

  // Check for power-up and set system alarm if homing is enabled to force homing cycle

  // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the

  // startup scripts, but allows access to settings and internal commands. Only a homing

  // cycle '$H' or kill alarm locks '$X' will disable the alarm.

  // NOTE: The startup script will run after successful completion of the homing cycle, but

  // not after disabling the alarm locks. Prevents motion startup blocks from crashing into

  // things uncontrollably. Very bad.


    if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }



  // Force Grbl into an ALARM state upon a power-cycle or hard reset.


    sys.state = STATE_ALARM;



  // Grbl initialization loop upon power-up or a system abort. For the latter, all processes

  // will return to this loop to be cleanly re-initialized.

  for(;;) {

    // TODO: Separate configure task that require interrupts to be disabled, especially upon

    // a system abort and ensuring any active interrupts are cleanly reset.


    // Reset Grbl primary systems.

    serial_reset_read_buffer(); // Clear serial read buffer

    gc_init(); // Set g-code parser to default state





    plan_reset(); // Clear block buffer and planner variables

    st_reset(); // Clear stepper subsystem variables.

    // Sync cleared gcode and planner positions to current system position.



    // Reset system variables.

    sys.abort = false;

    sys_rt_exec_state = 0;

    sys_rt_exec_alarm = 0;

    sys.suspend = false;

    sys.soft_limit = false;


    // Start Grbl main loop. Processes program inputs and executes them.




  return 0;   /* Never reached */

/*void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:


I have never had any reason to modify the grblUpload sketch. You are making the grbl installation process way more complicated than it needs to be.

In the IDE.
Go to File, Examples, grbl.
Double click on the grblUpload example to load grblUpload into the IDE.
Click the upload button to install grbl to the Uno.
That’s it.

If you modified the grblUpload.ino file, delete the grbl folder from the libraries folder and re-install the library.
You may want to save the config.h file if you made changes to it (for the homing sequence, for instance) and replace the config.h in the new installation with the old one to retain your changes.

You are surely rigth. Following Your advice, File, Example, Grbl gives me the file I showed at #4. It only contains

#include grbl.h

That was turned down by a previous Forum guy.

I have not even seen a grblUpload.ino file at all. Config.h? I don't recall ever seen or working in it.

You are sure right. This is not my home street. Following Your advice, File, Examples, Grbl I get a file that only contains: #include grblupload. Nothing else. Look att my post #4.

I have not modofied, not even seen any .ino file. Config.h? I don't recall seing it either.

That was turned down by a previous Forum guy.

This is a rare case where the rule is broken. Once you have grblUpload in the IDE, just click on upload to install grbl.

If you go to the libraries folder in the sketchbook (where your sketches are saved (usually ..documrnts\arduino\libraries) there shoud be a grbl folder. The config.h file is in that folder. There is also an Examples folder. That is where grblUpload.ino shoud be.

Thanks a lot gF! My sketches are saved in Dokuments\Arduino folder.

In Documents\Arduino\libraries\grbl I see a config.h and a numeber of .h and .c files. In Dokuments\Arduinolibraries\grbl\examples i find the grblupload that opens Arduino IDE and loads the grbl.h. Compiles and downloads without erors. 3 backwards questionmarks appear in Serial Monitor.

Have I understood and done the rigth, prepared the UNO for receiving G-code now?

Make sure that the serial monitor baud rate is set to 115200. When grbl starts you should see a message in serial monitor with the grbl version number. If grblUpload uploaded without errors or warnings, aside from the low memory warning, it should be ready to go.