Grbl CNC Firmware on ESP32

Woot!

I have been working on this for months. I am happy to announce that it is finally working right. The ESP32 will be a great option for CNC. Here are some things I am excited about.

  • Fast – Two 32 bit Cores at 240MHz Each, FPU, 80MHz Timers
  • Memory – Tons of Flash and RAM
  • Low Cost – $3-$10 depending how you buy it.
  • Small Size – Not much bigger than an Arduino Nano
  • I/O – It has about the same number of pins as an Arduino UNO, which is the target for Grbl
  • BlueTooth and WiFi – This is great and actually the primary reason for my port.

Porting

The bulk of the code was very easy to port over, but getting very low jitter, step pulse timing took a long time to achieve. This is not my first port of Grbl. My first port was to the PSoC5. That was much easier, but this was my first major project on the ESP32.

A goal was to use the Arduino IDE to develop the code. I thought it would make the project a lot more accessible to novice programmers that just need to make a few tweaks. I also tried to make the code easy to maintain with Grbl. This means there a few throwbacks/workarounds to AVR port numbering. etc.

FreeRTOS

The ESP32 uses an RTOS (Real Time Operating System). While “real time” sounds perfect for CNC, it is not good for step pulse timing. An RTOS allows multiple tasks to run “at the same time” and it manages the priorities and interaction of those tasks. The RTOS switches tasks at a “tick rate”. The tick rate is typically about 1000Hz. This means each task gets at least 1ms of time and the others wait. You can designate a high priority task to prevent these interruptions, but some tasks have watchdogs that must be reset so you need to give them some time.  You can set the tick rate higher, but I need a more than 50,000 hz step rate. That is not practical for the RTOS. I can turn off the RTOS and/or the watchdogs, but a major appeal of the ESP32 is the WiFi and BlueTooth. These need the RTOS.

Interrupts

The normal Grbl way generate step pulse timing is to use interrupts. As long as you follow the rules for interrupts, they will interrupt the RTOS without any delays in very deterministic manner. The rules are about how much time you can spend in the interrupt and what things you can do in the interrupt. The interrupt duration was not an issue because the code in the interrupt only takes a few micro seconds. What you can do in the interrupt took a while to figure out. The ESP32 does a “panic” reboot when it does not like something. That happened a lot during development. Most of my problems were related to how I used the peripherals. Things like the RMT feature simply can’t be used.

My Dev Board

I designed a little Dev Board to help me with this project. The features include…

  • 3 Stepper Drivers
  • 3 Limit Switches
  • 1 Touch Probe connector
  • Mist / Flood Coolant outputs
  • Start / Hold / Reset / Door switch connector.
  • Spindle / Servo / Laser – Connector
  • SD Card – This is just a breakout to a header that can be wired into the CPU if I ever decide to do that.

Next Steps

Testing – I need to do a lot of testing. I tested most of the features along the way, but need to double check all the config options.

Clean up – I have a lot of commented out debug code that needs to be removed

Publish on GitHub – Done. GitHub Repo

BlueTooth – That is the first new feature I want to add.

Hobby Servo Features – I use them a lot in my small machines and 328P Grbl doesn’t handle them well because the lack of 16 bit timer availability. I want high resolution jitter free servos as an option for any axis.

Simple Kinematics – GCode can be converted on the fly to alternate coordinate systems. This would not consider joint dynamics.

Video


If you want to be notified of future blog posts, please subscribe.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks

13 Responses to “Grbl CNC Firmware on ESP32”


  1. Craig Hollabaugh

    This is great!

  2. James Lewis

    Amazing work! I’ve been looking for a first time project to try out the ESP32, I think I just found it.

  3. Grbl Ported to the ESP32 - HudsonWerks

    […] Well, quite a few things in fact. As [bdring] explains, Grbl pushes the capability of the Arduino to the very limit; making it something of a dead-end for future development. Plus the Arduino needs to be plugged into the host computer via USB to function, a rather quaint idea to many in 2018. These were just some of the reasons he decided to port Grbl to the ESP32 board. […]

  4. avion23

    Hey thanks for all the work!

    I think the writeup doesn’t do your project justice. Can you look it over? I found some strange sentences and mistakes, f.e.
    Grbl does handle them well -> doesn’t
    That was CPU easier -> That was much easier

    I’ve invested roughly 16 hours into getting a high rate Interrupt running on the ESP8266 without RTOS. When I added a webserver the interrupts crashed. This doesn’t happen immediately, sometakes it takes 10 minutes. Do you run into the same problem in your project?

  5. bdring

    Thanks for the edits.

    I have not used any WiFi or BlueTooth features yet with this code, I hope I don’t have problems. The dual cores might help. How long are your interrupts? What is your interrupt frequency?

  6. Daniel

    Thats a great work! Thanks a lot for this!

    Do you use at the moment only one Core of the ESP32?
    If so, it would be a great platform to run a webserver on the second core offering a webinterface for machine control and gcode storage/streaming. Therefor the otherwise required PC (or Raspberry Pi) just for sending over gcode to the machine could be eliminated.

    One feature that was quite often requested in GRBL is the ability of controlling 4 axis. On the ATMega328 this is not possible to implement due to resource limitations. On the ESP32 of course you have a lot more resources available.
    What do you think how much effort it would be to add a forth axis to the code?

  7. bdring

    @Daniel

    Only 1 core is currently used. The other will be good for WiFi and BlueTooth.

    Adding additional axes is not hard at all. Search my blog and YouTube channel for PSoC related posts. I have had up to 6 coordinated axes working without any issues.

    Bart

  8. jmaniac

    The Git you have given is pointing to your previous work of Grbl port of PSoC. The right repo is https://github.com/bdring/Grbl_Esp32

  9. bdring

    @jmaniac Thanks…fixed

  10. iosias

    Hi there, i’m really happy to see your work! i think the esp32 has a huge potential over the old school ATMega328. i’m looking forward to a standalone solution, with a jog-pad and a display-solution (LCD).

    cheers

  11. bdring

    Right now that is any smart phone or tablet via bluetooth.

    Added a jog pad to the firmware would be easy, but it is not a priority because a phone is preferred right now.

  12. xinosuke

    It looks awesome!

    I’m doing a little project and I will need to clone one axis. I have been looking you board and it is just 3 axis. Do you think is it difficult to add this to PCB? Or maybe the firmware can not handle it?

    Thank you very much!

    PD: I am sorry for my poor English…:(

  13. bdring

    A cloned axis would not affect firmware. It would require a new revision of the PCB. I could do that for you, but there would be development cost. I can do it for $100+ $35 per board.

*