Read gcode and control a CNC

Good afternoon. Currently, I am a student of electronic engineering at the university, and together with two classmates, we are developing, building and programming a CNC.

We have already built the CNC, but now, we want to create an Arduino program that controls the two servo motors and the laser of our CNC, following the instructions provided by a GCODE file.

The point is that all the examples I have found on the internet are using external applications that work directly with the Arduino, sending them the information in GCODE format, and interpreting it to control the operation of the machine.

But in my case, as it is a project for a specific subject, and they have asked us for "originality", we have created our own program that creates our GCODE without the need for any other application.

The question is: I am looking for some code (among all those published on the internet) that I can use to control my CNC, but instead of being a program that sends the information to the arduino, it is the arduino itself that loads the file hosted on my computer with the instructions, and interpret them later to control the CNC.

Somebody could help me?

I think you have no idea the complexity of that job, unless your machine will only move VERY slowly. Trajectory planning multiple axes is VERY complex. Study the source code for LinuxCNC, EMC, or even Grbl to get an idea. It would be impossible for anyone here to tell you how to do it. Might as well ask someone on a forum to tell you how to build a nuclear reactor.

1 Like

At this point, can you give some examples of what this code might look like?

It sounds like you are wanting to reinvent grbl.

1 Like

Marlin GitHub - MarlinFirmware/Marlin: Marlin is an optimized firmware for RepRap 3D printers based on the Arduino platform. Many commercial 3D printers come with Marlin installed. Check with your vendor if you need source code for your specific machine. and Teacup GitHub - Traumflug/Teacup_Firmware: Firmware for RepRap and other 3D printers and many other 3D Printer softwares support reading directly from an SD card.

I think Teacup's internals and gcode parsing are easier to understand.

Thanks for nothing :frowning: ...

only, i have this: (183) GCODE - Trabajo de Microrobotica - YouTube

So, give several examples of what that will look like. You have to start documenting what you are trying to do.

#define MOTOR_X 1
#define MOTOR_Y 2
#define LASER 3

void setup() {
  pinMode(MOTOR_X, OUTPUT);
  pinMode(MOTOR_Y, OUTPUT);
  pinMode(LASER, OUTPUT);
}

void loop() {
  // Looking for the gcode file...
  if( /* I find the file because exist */ ){
    // Read the instructions and control my CNC
  }
  else{
    // Continue to searching the file
  }
}

Code that can drive a real CNC with all features is really complex.
One thing you have to learn about such projects is precise description

example

What does "CNC" mean in this case? The device containing a computerised control and a mechanic, power-supply and evrything ?

Or does "CNC" mean just a part of the above?

Where is the big difference between a file "hosted on a computer that loads into the arduino"

and a software like grbl running on an arduino receiving data from a Windows-Computer???
I can't see a big difference.

This is another example for writing precise what you want is nescessary

You wrote that you have two servomotors and "the laser"
unprecise! What does "the laser" do?

  • measuring distances because it is a ToF-sensor?
  • used as optical fence for emergency switch-off?
  • demonstrating the movement of your XY-mechanic on a surface?

If you reduce the rich instructionset of real CNC GCode to three commands
set velocity
move to position XY
switch laser on/off

It will become easy
Then you can use an algorithm called Bresenham-algorithm.
It is able to calculate the velocity (steps per second) of two axles = your XY-axles
for any direction = any slope-value dy / dx

This is pretty good managable.
As you are looking for code: does this mean if you find a "code" that fits to your needs you would use it?
Where is the originality of the code then?
The main functionality:

  • put an SD-Card with a Gcode-Textfile stored on the SD-Card into an SD-card slot mounted on your device
  • read in the lines of text containing the GCode
  • send the GCodes to another microcontroller running GRBL

For this reading-in data from a SD-Card you can use a second microcontroller.
Even a - in physical size - small one like the seeediuno XIAO is capable of reading in data from an SD-card and send it over a serial connection to another microcontroller running GRBL

anyway you should describe on more detail what you really want to do

best regards Stefan

Does this mean interpret the GCODE in the file?

Or pass the GCODE out through a serial port to a CNC (such as GRBL, a 3D printer, or a Fanuc?)

The Arduino can NOT read a file on your computer. The Arduino can listen to the serial port, where some application on the PC is reading the file and sending the data.

The PC code can be written to open and read a file on the PC and send the contents to the Arduino when requested.

It's unclear to me what you want to be sent from the PC to the Arduino.

I have to wonder how you all were able to test your machine and ensure it could actually perform something on a line of GCODE text?

@sergio98ssct

we (all the posters so far) including me forgot to write you
a warmhearted "welcome to the arduino-user-forum".

The post above may have sounded somehow "rough".

Yes of course you are really welcome. In the past there have been a lot of newcomers that learned how to program with the support of the users here.

So just ask more questions. If the questions are specific and show you need some initial support to get yourself going like
"where can I find information about ....."

or presenting a self written sketch with a question like
my code should do ......
but what I can watch is ......

This user-forum is a neverending source of help and support.

best regards Stefan

Yes, interpret the GCODE in the file

tips how to improve your posting style

1 Like

Agree. I will try to explain my situation in a much more detailed and argumentative way than before.

I am currently studying electronic engineering at university. In a subject that I have recently started studying (microrobotics) we have been asked to carry out, in groups of 3 people, a project that has something to do with the subject itself. My group has proposed to manufacture a CNC.

What kind of CNC? Our CNC must be able to draw on a wooden board, with a 5.5W laser, any figure, image or drawing that we tell it to do. To do this, the laser must burn the wood in its path, and leave the silhouette of the image marked on the wood.

To do this, we have built a structure composed of a servomotor that rotates an axis horizontally, and another servomotor that rotates an axis vertically. The rotation of these axes allows the laser to move in a 2-dimensional matrix, being able (if we get it to work) to burn the different traces of wood to result in our drawing/image/silhouette.

To carry out this project, we rely on the following tutorial: https://www.instructables.com/Arduino-Laser-Engraver-Wood-Design/

Regarding hardware, the tutorial explains everything to perfection, and we thought that in the software section it would also be perfectly explained. Also, one of our groupmates has previously worked with 3D printers, so we figured that if all the documentation was in the tutorial, it would be very easy to implement.

Therefore, apart from assembling the entire structure of our CNC, I made a small program, which, from an image or photo taken from the computer camera, obtained a ".txt" file with the different instructions that my CNC machine I should continue to draw the silhouette/drawing/image that I was requesting. The complete video where I show the program (although today I have modified it a bit) is as follows: Prueba de la App final | Microrobotica | PrintLaser - YouTube

With this program, I get the instructions that my CNC is supposed to carry out to carry out the printing.

I just need to be able to transmit this information to my Arduino UNO, and that my servomotors and my laser work following these instructions so that the final result is the desired one.

Apart from what you can recommend me to achieve it, I found this a few hours ago:

Regardless of what you can recommend or suggest me to do, could this also work?

Ok well done explaining in more detail what you want to do.

The main thing excerpted from your video is:

I have a converter-software that can do this:

*.jpg-file----->converteroftware---->standard-GCODE as a textfile
example of a line inside the GCODE-textfile:

G01 X55.123 Y63.567 Z1.789 F1000

I have found the software universal-GCODE-Sender
which can send GCODE-Data to an Arduino.
This Arduino is running the CNC-Firmware grbl.

Yes this works.

Now the next thing to check is:
The grbl-firmware expects stepper-motors that drive linear axles

You have written something about

rotating looks like this

The picture of the project at instructables shows

linear axles

Now I want to comment again on your posting-style:

YOU

are asking for help. This means

you

should take the effort to make it

as easy as possible

for others to get a clear picture of your project !
by posting

directly

pictures into this thread
into your thread. Like I have done it now.

This demonstrates that you really do that part of the work you can already do.
best regards Stefan

1 Like

Here's an example:

I have a very rough 1-axis gcode+Accelstepper example that isn't as well done as that project.

Here it is implemented in Wokwi:

And here's a thread on several other Wokwi GCODE simulations:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.