G codes

Hi,

I want to sent G codes to my arduino uno to control an X/Y (two stepper motors). Once I saw a piece of code that uses the 'read serial method' If I remember correctly, you could sent G codes to throug the serial monitor, but I don't find it anymore.. as I am not a good programmer..

The goal is to use an SD card to control the X/Y

Is there anyone who can help me?

would that be this?

but I don't find it anymore.. as I am not a good programmer..

:slight_smile: no need to be a programmer to use google.... that was the 3rd entry when typing "arduino gcode sender"

@J-M-L:
No, I have already installed the Universal Gcode Sender and that works fine! But I am looking to send the Gcodes throug the arduino serial monitor, so maybe I can start with a stand alone machine (fixed Gcodes in the arduino code) and expand it to use a SD card in the future

Is there a way to send Gcodes to an arduino uno without connected to a computer?

BramWerbrouck:
@J-M-L:
No, I have already installed the Universal Gcode Sender and that works fine! But I am looking to send the Gcodes throug the arduino serial monitor, so maybe I can start with a stand alone machine (fixed Gcodes in the arduino code) and expand it to use a SD card in the future

the top of the page I linked to says you are already there...

Getting Started (For New Users.)
After flashing Grbl to your Arduino, connecting to Grbl is pretty simple. You can use the Arduino IDE itself to connect to Grbl. Experiment or play with it, just to see if you like it. Other serial port programs, such as CoolTerm or PuTTY, work great too. The instructions are pretty much the same.
Open up the Arduino IDE and make sure your Arduino with Grbl is connected to your USB port.
Select the Arduino's Serial Port in the Tools menu, as you would normally with an Arduino.
Open up the 'Serial Window' in the Tools menu.
If you are using Grbl v0.9, make sure to change the baud rate from 9600 to 115200.
Once open, you should see a Grbl welcome message like Grbl v0.Xx ['$' for help]. This means all is good! You're connected!
Make sure you change the "No line ending" drop-down menu to "Carriage return". If you are using any other serial port program, you must do the same.
If you haven't received the welcome message or some garbled characters, make sure that the baud rate is set at 9600 (or 115200 for v0.9+).
From here, you can simply start sending Grbl some G-code commands, and it'll perform them for you. Or, you can type $ to get some help on what some of Grbl's special commands are or how to write some of your machine settings into Grbl's EEPROM memory.

Is there a way to send Gcodes to an arduino uno without connected to a computer?

what would "send" mean in that sentence??

I juist don't understand how to use Gcodes without the computer or software. I know with software this is working, choose the right port and types codes...

But it should be working stand alone (with fixed Gcodes in the arduino code or on a SDcard)

I suspect trying to drive an SDcard alongside GRBL will fail, GRBL is very tight for cpu cycles and
SRAM as is. Consider a separate microcontroller to handle the SDcard.

Might be worth seeing if someone has managed this already

Well it's a speed issue - timing is indeed key. So you can either try to modify the loop where Serial read is done and modify that for a SD card read - or extra flash memory

If that is too slow then indeed need a faster CPU and storage.

Is there an example for this code? As I said earlier, I'm not a professional...
So the goal would be this:

  • read a file from an SD card
  • the x y draws the mandala's (patterns) without any extra software, just stand alone

Maybe it's possibile another way, no Gcodes but something else to 'draw' those mandala's??

MarkT:
Consider a separate microcontroller to handle the SDcard.

This appears to be a good combination of keywords...
https://www.google.com/search?q=grbl+streamer+sd+card

Raspberry Pi seems to be the most common choice.

I believe "headless" is also a good keyword for such things; maybe replace "streamer" with "headless".

I found this on the web:

And here a piece of code:
http://headlesscnc.blogspot.be

They use 2 arduino's, but the don't saybhow to connect this all togheter, does anyone has an id?

If I connect Two Arduino Uno's like this:

MASTER UNO SLAVE UNO
TX <-> RX
RX <-> TX
D13 <-> D7
mount up a stepper shield (1) mount up a SD shield (2)
running GRBL (3) running Bill's Greiman's fat 16 arduino library (4) AND the code I found earlier (5)

Should this be able to work??

(1) Home · synthetos/grblShield Wiki · GitHub(is this everything, or do I need something more?)
(2) SD Card Shield V4 - Seeed Studio
(3) GitHub - grbl/grbl: An open source, embedded, high performance g-code-parser and CNC milling controller written in optimized C that will run on a straight Arduino
(4) GitHub - greiman/SdFat: Arduino FAT16/FAT32 exFAT Library
(5) http://headlesscnc.blogspot.be

/*
//whith this code you can use grbl 
//without using pc 
//we are using two arduino here 
//one for grbl and other for 
//gcode sending from sd crad

Author : magansoma
Note :We dont take any responsibility for any kind of dmaage or anything else its upto you
you can use or modify this code its opensource  
*/
#include <SD.h>
#include <SPI.h>

File myFile;

String x_move, y_move,z_move,gcode_value;
String homing_gcode;
char get_ok;
int ok_word;

const int x_plus = 2;
const int x_minus=3 ;
const int y_plus= 5;
const int y_minus= 6;
const int z_plus= 7;
const int z_minus= 8;
const int home_button=9;
const int send_button=10;

int x_count=0;
int y_count=0;
int z_count=0;
int grbl_rx_buffer;

int rx_buffer_check_rate=500;//rx buffer check rate 500 millisecond

String machine_status="";
int machine_flag=0;
const int rx_buffer_limit =64;//arduino has 128 is 127 byte of buffer 
//so we are here giving less then 127 buffer limit

String get_rx=" ";//this ensures that first gecode doesnt send 
int rx_data=0;//until Rx buffer is cheked or there is no rx tx connection
//200 which less then 127 so first time buffer is checked with 200 
//so after second loop it gets its readed value from rxbuffer

int gecode_send_delay=100;
//so this will put some delay fofre sending next g code
//this may pervent some problem when we lost rx tx connection 
//so that time insted of sending g code at full speed this will put some delay

void setup()
{
  pinMode(x_plus, INPUT);
 // Open serial communications and wait for port to open:
  Serial.begin(115200);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
//sd card initializtion  starts here

  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing is cool");
 // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  
  //sd card initializtion ends here
  
  digitalWrite(x_plus,HIGH);
  digitalWrite(x_minus,HIGH);
  digitalWrite(y_plus,HIGH);
  digitalWrite(y_minus,HIGH);
  digitalWrite(z_plus,HIGH);
  digitalWrite(z_minus,HIGH);
  digitalWrite(home_button,HIGH);
  digitalWrite( send_button,HIGH);
 
  x_move = String("G0X");
  y_move = String("G0Y");
  z_move = String("G0Z");
  homing_gcode= String("G0X0Y0Z0");
  String gcode_value = String("");
  String get_ok= String();
  
}


void loop()
{
  while(1){
    
 //x movement
 // x++ movement
 if(!digitalRead(x_plus)) {
   x_count++;//printing here solves the problem
  gcode_value =x_move+x_count;
  Serial.println(gcode_value);  
   //x_count++; placing it here 
   //creats problem because its already 
   //incremented but not printed 
   //so when we go in rrevese direction 
   //that unprinted variable will comes and 
   //thats we dont want
   //so soultion is increment x_count before prints 
   //so that gives us crrent value of x_count
   delay(100);
 }   
//x-- movement
if(!digitalRead(x_minus)) {
    x_count--;//read comments of x++
  gcode_value =x_move+x_count;
  Serial.println(gcode_value);  
//  x_count--; not here 
   delay(100);
 }   
//x  movement ends here 
 
//y movement
// y++ movement
if(!digitalRead(y_plus)) {
  y_count++;// read x+= comment
  gcode_value =y_move+y_count;
  Serial.println(gcode_value);  
  //y_count++; not here
   delay(100);
 }   
// y-- movement
if(!digitalRead(y_minus)) {
   y_count--;//read x++ comment
  gcode_value =y_move+y_count;
  Serial.println(gcode_value);  
 // y_count--; not here
   delay(100);
 }   
//y movement ends here
 
//z movement
//z++ movement  
  if(!digitalRead(z_plus)) {
    z_count++;//read x++ comments
  gcode_value =z_move+z_count;
  Serial.println(gcode_value);  
  ///z_count++; not here
   delay(100);
 }
// z-- movement 
 
 if(!digitalRead(z_minus)) {
   z_count--;//read x++ comments
  gcode_value =z_move+z_count;
  Serial.println(gcode_value);  
  ///z_count--; not here 
   delay(100);
  }
// z movement ends here 

// home movement // 
  if(!digitalRead(home_button)) {
  Serial.println(homing_gcode);  
   delay(100);
 }
// home movement 


// go for next process
//ex. sending data from sd card


   if(!digitalRead(send_button)) {
   //----------- need todo some initilize prodedure here --------------
   //------------before sending g code to grbl----------------------------
   //Serial.println("starting g code sending");  
   delay(100);
   break;
 }


//go for next process ends here



 }//while loop ends here
  
   String l_line = "";
  //opens the file here
   myFile = SD.open("circle.txt");
   ///myFile = SD.open("CROSS.TAP");
   while (myFile.available() != 0) { 
   //A inconsistent line length may lead to heap memory fragmentation    
   l_line = myFile.readStringUntil('\n');    
   if (l_line == "") //no blank lines are anticipated    
   break;   
    // 
    

 // G code flow control starts here 
   //this checks ok is sent by grbl 
   //it stays here untill ok is recived 
   //it checks ok  from ok reading string is
   // not worked here  may be its taking too much times so it may miss some "ok"
   // from grbl
   //gcode flow control strats here
   

   //only reading o character
   //below two method needs rx buffer setting on  by $10=8 in grbl 
   //we can recheck it by $ 
  
   
   //<<<<<<<<<<<<<this line sends g code >>>>>>>>>>>>>>>>>>>
   Serial.println(l_line);
   
 //<<<<<<<<<<<above lines sends g code >>>>>>>>>>>>>>>>>>  
   
   //reading one character MEHOD 1 
   //this mehod is first send g code 
   //and check responce after  gcode sent 
  ////with ok massage from grbl 
  while(1){
  
   get_ok = Serial.read();
  
  // get_ok.trim();//clear off white spaces in 
   //strarting end ending of string
   if (get_ok == 'o'){ 
     get_ok=' ';
    //ok_word++;
    break;
    
   }
   //delay(100);
 
   
  }
  
//gcode flow control ends here


//this sends G code
//<<<<<<<<<<<<<this line sends g code >>>>>>>>>>>>>>>>>>>
  //Serial.println(l_line);
   
 //<<<<<<<<<<<above lines sends g code >>>>>>>>>>>>>>>>>>  
 
  delay(gecode_send_delay);//smooth opration in gcode sending 

} 

//this closes file 
myFile.close();
delay(1000);//may need some refresh time
//to SD CARD after closing file 
//total program completes here 
//put here want you wqant to do after programme compltes  delay(1000); 
}
1 Like

I version .9 of GRBL, Pin 13 is reserved for spindle direction.

BramWerbrouck:
If I connect Two Arduino Uno's like this:

GND is missing.

You're right, I forgot the GND

but I saw there is another option, communicate through SPI..

Which one should I choose?

It is my understanding GRBL expects the stream through USART0 so the first one.