G Code Example: How to Run Your First CNC Program

Visualization from Universal gCode Sender of the g code example program.

Congratulations, you machine is under its own power. By now you have spent some time jogging the machine around using the keyboard or the buttons on Universal G-Code Sender. This is all well and good, but the real power of your CNC machine lies in its ability to run long complex gcode programs. The following is a step by step guide on how to run a CNC program. We even include a gcode example program to run that will draw a circle. If you don’t have your machine running just yet and would like some guidance, check out How to setup your Arduino CNC Machine.


G Code Example

The following is exactly what is saved in the circle.nc file. Click the link below to download the file.

Download the g code example file

 

G17 G20 G90 G94 G54
G0 Z0.25
X-0.5 Y0.
Z0.1
G01 Z0. F5.
G02 X0. Y0.5 I0.5 J0. F2.5
X0.5 Y0. I0. J-0.5
X0. Y-0.5 I-0.5 J0.
X-0.5 Y0. I0. J0.5
G01 Z0.1 F5.
G00 X0. Y0. Z0.25

This simple program will draw a 1” diameter circle about the origin. Use this program to test your CNC machine and confirm your axis directions are set correctly.

Note all gcode programs a simple text files save with a “.nc” extension. You can open the “circle.nc” file downloaded above in a text editor like Notepad. You can also copy and past the above lines into a text editor and create your very own CNC program. Simply save the file with a .nc extension.

Start Here

Use the following steps to run the program… If you are more of a visual learner, scroll down and click on the video below for walk through of the process.

1. Load a marker into the spindle (Keep the spindle off!) Markers work better than pens or pencils because the tip is softer and more forgiving. Also setup a piece of paper or other material on which to draw near the center of your machine’s work envelop.
2. Power on your machine
3. Connect to the machine with Universal gCode Sender

Set Your Zero Position

4. Jog (move) the spindle to the center of your machine table

a. Under the “Machine Control” Tab enter “.1” into the “Step Size” box & press enter
b. Ensure “inches” is selected
c. Use the X+,X-,Y+ and Y- buttons to move the machine close to the center of your work table (You can also use the arrow keys on the keyboard)
d. Press the “Reset X Axis” button (This tells the machine to remember this position as your X zero location)
e. Press the “Reset Y Axis” button (This tells the machine to remember this position as your Y zero location)
f. Now slowly use the “Z-“ button to lower the marker tip. When the marker tip gets close to the paper, stop.
g. Adjust the “Step Size” to .01 (Remember to hit enter after you enter your value)
h. Continue lowering the marker until it touches the paper by tapping the “Z-“ button.
i. Press the “Reset Z Axis” button (This tells the machine to remember this position as your Z zero location)
j. Retract the marker from the paper using the “Z+” button. Go at least .1” above the paper. This is equal to 10 clicks of the “Z+” button when your step size is set to .01

Load the CNC Program

5. Ok, now the machine is almost ready.
6. Go to the “File Mode” tab
7. Select “Browse” and search for the “circle.nc” file (If you have not downloaded the file click here)
8. Click “Open”
9. The next button press will send the “circle.nc” g Code program to your machine and it will start to move. As soon as I hit “Send” I like to move my mouse cursor over the “Pause” button. If anything goes wrong, all I have to do is click the mouse to stop the program since it’s over the pause button.
10. This is for all the marbles. Click the “Send” button.
11. The machine will start moving.
a. First, it will move the Z axis to .25”
b. Next, the machine will move to X = -0.5 and Y = 0.0 (Note the marker should not be touching the paper yet)
c. Now the machine will lower to Z = 0.0 and then marker should touch the paper)
d. The machine will slowly draw a clockwise circle.
e. When the circle is complete, the machine will move the Z axis up to .1”
f. Now that machine will return to the X & Y zero position.
g. Finally the z axis will move back up to the original .25” starting position.

Video Walk Through

 

Click here to watch the video on YouTube.

Conclusion

That’s it! You just ran your first CNC program. As you run more programs, this process will become second nature. Again you can download the g code example by clinking the link below.

Download the g code example file

 

If you run into any problems, contact me at tim@diymachining.com or in the comment section.

What CNC program are you going to run next? Leave a comment and let me know.

Thanks for reading. Until next time…

Tim

P.S.

Now that your machine is running, check out the following options for tooling. Note, these are affiliate links and DIY Machining earns a commission if you purchase using the links below. However, there is no additional cost to you.

54 Comments

  1. bovb

    home made cnc…Arduino Uno with a cnc shield running universal gcode sender…and your code drew a square….what am i doing wrong?

    Reply
    1. Tim (Post author)

      Hmm, that’s frustrating. The first thing I would try is using the visualize feature in Universal GCode Sender. If you run the visualization and the gcode draws a circle, then the problem is likely with your GRBL settings.

      Reply
  2. Alexis Jiménez

    Hi, I am knew in cnc world. I try to learn before to buy a cnc machine. I am from. Dominican Republic. What books you recomend to study and be familiar with this vast World. I want start with basic program lessons and go increasing my level. Thanks for your article.

    Reply
    1. Tim (Post author)

      Alexis, glad you are interested in learning more about CNC machining. In place of books, I am going to recommend a website and a software tool. First, the web site, I would check out http://www.cnccookbook.com/ This site has tons of general CNC information. Great place to learn. Second, the software tool, I recommend you start with Fusion360 https://www.autodesk.com/products/fusion-360/overview to learn the CAD and CAM side of CNC machining. In my experience the CAD and CAM present the biggest learning curves. Hope that helps. Thanks for the comment.

      Reply
  3. CAN

    GREAT JOB THANKS

    Reply
  4. Afshin

    Hi dear tim.

    my name is afshin and i recently made a cnc just for fun.i used arduino uno and cnc shield plus A4988 drivers.and for stepper motors i using nema 23.after plenty search i fined your article and i did everything you said but when i ran the circle.nc code my stepper made the weird sound.i didn’t do any flashing or adjusting my arduino or drivers.i think it needs some kind of adjustment.i dont know what to do pleas help me bro im devastated.thank you for your help.

    Reply
    1. Tim (Post author)

      Afshin, I am not too familiar with the CNC shield and A4988 drivers. However, based on the weird sound from the stepper motor, I suspect you may have one of the motor connections wired incorrectly. I would double check the wiring and try it again.

      Reply
      1. CatweazleMagic

        Another possible cause can be that your speed or accelaration is set too high.Then it also starts to make al kinds of horrible sounds without moving. Seen the lot of it. *Grmble*

        Reply
        1. Tim (Post author)

          Ah yes, good point…

          Reply
  5. can

    no, you should read about A4966 user manuel
    there is tiney potansiometer you must turn it till souns come normal..
    cheers

    p.c. :for all axis

    Reply
    1. Tim (Post author)

      Can – Good suggestion. I take it you are referring to the comment left by Afshin looking for help with his A4966 drivers. gShield has the same current limiting potentiometers for each axis. This should be set once the final assembly of the machine is complete. If I recall correctly when I set mine, I started with the potentiometers at ~1/2 of the available current and fine tuned from there.

      Reply
  6. Vaire

    Can I use this with the 3d printer its the same setup right?

    Reply
    1. Tim (Post author)

      Vaire – Good question, I don’t see why it would not work. I am not sure how you hold a pencil or marker in the hot end of a 3D printer. Maybe you can tape the marker to the side of the hot end. Will you give it a try and let us know how it goes?

      Reply
  7. UCHE

    Can you run fusion360 with this arduino setup, i am a novice yet thanks TIM

    Reply
    1. Tim (Post author)

      Great question! Yes, you most certainly can us Fusion360 with the arduino + grbl setup. That’s what I use. Fusion360 includes a post processor specifically for grbl. My work flow looks like the following: CAD – Fusion360 then CAM – Fusion360, then post process – Fusion360 which produces the .nc (gcode) file. Next I copy the .nc (gcode) file to my laptop that runs Universal Gcode Sender and is connected to my Arduino+grbl controlled CNC machine. Let me know if you have other questions. -Tim

      Reply
  8. FA

    HI
    my GRBL controller cant send only ( .nc ) file and i dont know how to save it in that form with inkspace ?

    thanks

    Reply
    1. Tim (Post author)

      FA – You are correct, GRBL will only accept gcode (.nc files) streamed to the Arduino using something like Universal Gcode Sender. To generate a gcode file from InkScape, you need an extension. The good people at Norwegian Creations have a great write up about how to do exactly what you want. See the link below…

      https://www.norwegiancreations.com/2015/08/an-intro-to-g-code-and-how-to-generate-it-using-inkscape/

      If that doesn’t get you going, let me know and we can find another solution.

      Reply
  9. FA

    Thanks Pal — it solve the issue
    now i have new one i am trying to fiend G-code controller to my UBUNTU system and run
    smooth ?

    Thanks again

    Reply
    1. Tim (Post author)

      I don’t have any experience with LinuxCNC but I have heard good things. Check out the following link… http://linuxcnc.org/ If that’s not what you are looking for let me know and we will keep working on this.

      Reply
      1. CatweazleMagic

        Linuxcnc does not run very well on ubuntu, Debian is standard.
        But linuxcnc is really great and free!!!! Yeah!!!!
        Starting with it is a bit a steep learning curve, but once you get the idea you’ll quickly find out how flexible it is and well built it is.

        Reply
  10. Mayuresh ghadge

    Plz send me some more cnc programs for my diy cnc milling machine

    Reply
    1. Tim (Post author)

      Mayuresh – Let me know what other types of programs you would like and I would be happy to send some your way. In the mean time, you can use the link you received in the email confirming your email address and download the gcode for a pumpkin or X key chain. I also have the gcode for a collet wrench for a multi tool (Dremel) that I could post.

      Reply
  11. Maria P

    Hi. I have a mini CNC machine. I have GRBL controller. I downloaded Inkscape to convert images into gcode but for some reason Inkscape is not saving my file. It has an error. What other pogram can I use to save as gcode for a CNC machine? Thank you so much for your help!

    Reply
    1. Tim (Post author)

      Maria – Great to hear from you. Unfortunately, Inkscape seems to be the most common tool used to generate gcode from an image. You might also try this site… http://www.thuijzer.nl/image2gcode/ I have never used it and would be interested in your feedback. I personally use Autodesk Fusion360 for all of my projects. Granted, I am not converting images to gcode. You can get a copy here https://www.autodesk.com/products/fusion-360/students-teachers-educators

      Reply
  12. jesus

    thanks a lot man, i just made my first test with my made from scrap cnc and it ran smoothly.
    🙂

    Reply
    1. Tim (Post author)

      Hi Jesus, that’s excellent! Nice work. I would love to see a picture of your machine.

      Reply
  13. acmeh

    hi i have question you how install limit switch and how tune for universal g-code

    Reply
    1. Tim (Post author)

      Hi Acmeh, I recommend you take a look at the following for a guide on installing limit switches. https://github.com/gnea/grbl/wiki/Wiring-Limit-Switches

      Reply
  14. Guillermo Montañez

    Hi Tim. Thank you so much for writing me. I have just reassembly my mini cnc because it drew a strange line after finished to draw a picture. I made the cnc from mdf and I am testing it as a plotter. I think to transform it into a milling machine.

    Reply
    1. Tim (Post author)

      Hi Guillermo – Sounds like a great project. Will you send or post some pictures when you get it finished?

      Reply
  15. Suneel Michigan

    I’m coming late to the party. I just built my first CNC machine with some scrap wood and old dot matrix printer stepper motors. It has a pen attachment as well as a motor to attach a milling bit. I’m testing it out with the pen before I try the higher stress of milling. Works like a dream! Thank you for your guide here, it was a big help.

    Reply
    1. Tim (Post author)

      Cool, glad to hear the information was helpful. Thank you for the feedback.

      Reply
  16. Idris

    Thank you for the article, it helps me test-run my cnc router easily.

    I am a beginner, still like to know how to use gcodes generated by image to gcodes converter.

    Reply
    1. Tim (Post author)

      Hi Idris, thanks for writing. This is a common question. I am actually in the middle of writing a blog post about this very topic. The short answer, I recommend you try F-Engrave. See the link to download a copy. Be sure you scroll down the page to the download links. There are some ads at the top of the page that are confusing. You don’t want those.
      http://www.scorchworks.com/Fengrave/fengrave.html
      Its really meant for engraving but does a nice job a taking an image and converting it to gcode so it can be engraved on a CNC router or laser. The program outputs a .ngc file but this can be renamed to a .nc file and read by Universal Gcode Sender.

      Reply
  17. Joe Landau

    Failed to run for me (Raspberry Pi, Protoneer hat. Here’s the result:
    >>> G17G20G90G94G54
    ok
    >>> G0Z0.25
    ok
    >>> X-0.5Y0.
    ok
    >>> Z0.1
    ok
    >>> G01Z0.F5.
    ok
    >>> G02X0.Y0.5I0.5J0.F2.5
    >>> X0.5Y0.I0.J-0.5
    >>> X0.Y-0.5I-0.5J0.
    >>> X-0.5Y0.I0.J0.5
    >>> G01Z0.1F5.
    >>> G00X0.Y0.Z0.25
    ALARM:2
    [MSG:Reset to continue]

    What shocks me is that there is no interpretation of ALARM:2, or help concerning it.

    Reply
    1. Tim (Post author)

      Hi Joe, thanks for reaching out. The ALARM:2 means “Numeric value format is not valid or missing an expected value.” Click here for an explanation of the alarm codes. Did you machine run any of the code? Based on the feedback above, it looks like the machine ran until it go to the ARC gcodes G02. I copied to code you provide and it ran without issue on my machine. My current setup is Arduino UNO with GRBL v1.1, with a NetBook PC running Universal Gcode Sender 2.0 Classic. What version of GRBL are you running? What gcode sender & version are you using?

      Reply
  18. Jay Holt

    I use Vetric cut2d it ask what file type to output as? for example, x-carve (mm)(*.gcode) do you know what type to use for 3018 pro cnc. Thanks

    Reply
    1. Tim (Post author)

      Hi Jay, thanks for reaching out. Good question… I have never used Vetric Cut2D however, assuming your 3018 Pro CNC is using a standard GRLB load on the controller, the most basic .nc (gcode) format is a good place to start. I use Fusion 360 with the standard “GRBL” post processor. I have to delete the tool change command lines in the .nc file. These are the commands that start with “T” followed by a number. For example “T8 M6”. This may be specific to my post processor and version 2.0 of Universal Gcode Sender.

      Reply
  19. Peter Johns

    What a great blog for us newbies.
    Have just been given a CNC3040 engraver, and don’t know where to start playing with it.
    I have found a Compaq deskto PC with Win 7 32 bit and a parallel port. I am about to download free Mach3.
    Can you please give me a sequence of things to learn and do to master this CNC stuff. Would like to test the system first, so that all the step motors and spindle are working, but not sure how to do that either.
    Is there a demo Goode that will fully test the engraver?
    Best wishes, and thanks for sharing
    PJ

    Reply
    1. Tim (Post author)

      PJ, thank you for the comment. First, you will want to know what type of controller your CNC machine is using… (GRBL with USB cable or Mach3 with parallel cable) Next, you will want to test the machine. Power it on and jog the machine to the extent of each axis. Then run a test gcode. I recommend the circle.nc that you can download. This will indicate if the machine settings are correct. You know you should get a 1″ diameter circle. If you don’t then you know you need to tweak some of the machine controller settings. Once the machine is running, then you can tackle the CAD/CAM post processor learning curve required to generate your own Gcode.

      Reply
  20. Guuz

    Hi,

    I’ve got a CNC 3018 and I’m having issues running code off the SD card. The files they provide run fine, but when I copy and paste the code into notepad and save again, the offline controller just quits immediately when I try to run the nc file. I’ve tried your example code, their example code, my code whatever and nothing seems to work. If I send via usb it runs just fine.

    Its weird, do you know if there is a special txt format for the files? end of line characters, unicode, ansi, etc?

    Thanks,
    Guuz

    Reply
    1. Tim (Post author)

      Hi Guuz, glad you reached out… I am not familiar with CNC 3018 controller. I suspect you are correct and there is an issue with the file extension. Will you forward a copy of the files provided with the machine that work via the SD card? Please send them to Tim@DIYMachining.com and I will take a look and let you know what I find.

      Reply
      1. Tony L.

        I have the exact same issue. when connected directly to the computer router runs code fine. test codes that came on the SD card run fine. When I send code to the SD card and put into the external control – nothing. It does nothing and says that its done. Using Carbide Create and Candle. The .nc file has size to it so I know that its creating a file. Let me know what you find out working on Guuz.

        Reply
        1. Tim (Post author)

          Tony, glad you reached out. Have you tried reading the gcode program you created that is saved on the SD card with your computer? We found that the SD card itself was the issue. I recommend trying another SD card. Let me know if that resolves your issue.

          Reply
  21. James Borst

    I like your recommendations to use a marker in place of the spindle to run the program. My neighbor has a CNC machine and said that I could use it whenever I want. If I owned a business or CNC machine, I’d probably consider contracting the CNC programming out to a professional.

    Reply
    1. Tim (Post author)

      James, thank you for the comment.

      Reply
  22. Denis Jenkins

    Oh Brother! I have two CNC machines one I ordered from China 4 years ago and now a new one which is GRBL. I think for both of these machines I am required to open and save files with the .NC extension. I have acquired Aspire 9 and to my surprise I cannot save my creation under the .NC extension.
    Can you help me?
    I keep searching the web and references are made to G-Code but the only extensions I see available for my machine are .nc, .ncc, .ngc, .tap, .txt
    Aspire offers so many options but not those.
    Thanks in advance

    Reply
    1. Tim (Post author)

      Hi Denis, thanks for the comment. First let me say, I have never used Aspire. Take the following for what it’s worth… I found this video which walks through the process of generating a gcode (.nc) file from Aspire. Note, you will not be able to save your creation as a .nc file. You will need to click the Save Toolpath button to generate the gcode. You will want to select a GRBL post processor if available. Otherwise select a generic post processor. The process of saving or generating gcode from tool paths is referred to as “Post Processing.” This process is common across most CAM programs. Feel free to comment back or email me at Tim@DIYMachining.com if you have more questions.

      Reply
  23. Heiko

    Hey Tim,

    thank you so much for your discretion and tutorial. It helps me so much and its fantastic, that people like you help others with their knowledge.

    Sittin´here in North-Germany, ordered my first milling machine, mounted it very laboriously and stood like an ox in front of the mountain, because I didn’t know how to get it running. I spent hours researching the internet, found your site and followed the instructions. And now I have drawn my first circle on a piece of paper. I’m so much proud and happy 😉

    Kind regards from Lüneburg

    Heiko

    Reply
    1. Tim (Post author)

      Heiko, Congratulations! I am thrilled the information was helpful. I appreciate your comment. Enjoy your CNC machine. Feel free to contact me if you have any questions.

      Reply
  24. harshal

    Very nice article which is best informative . An easy for beginners I also write article on CNC program & operating skill .

    Reply
    1. Tim (Post author)

      Thank you Harshal.

      Reply
  25. Richard McConchie

    Hi Tim. My name is Rick. I have a 4030 from sainsmart using candle and grbl. Until now I haven’t been able to get my cnc to do anything at all. I downloaded your circle g-code and it works great. I changed the feed settings and that also worked. I wish I could of found you much sooner. I use fusion 360 and do up something in cam and nothing works. I was hoping you could help me with that problem if you have time. Also to get my spindle to turn on in your circle g-code and my visualizer in candle doesn’t move. Im sure it all has to do with g-code. I want to give you a big thank you for helping me.

    Reply
    1. Tim (Post author)

      Rick, I appreciate the comment. Glad the site was helpful. Feel free to reach out to me via email at Tim@DIYMachining.com

      Reply

Leave a Comment

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