Automatically activativating G Code on an Arduino

Hi all,
I am currently making an irrigation system with two axis that are controlled by stepper motors. I have the basics of the system down packed. One Arduino reads an analog signal from a soil moisture sensor, which activates a relay, which turns on a water pump once the soil is below a certain level. I have also uploaded GRBL to another Arduino and I am using Universal G-Code Sender to send the G Code which controls the movement of the stepper motors, at the moment the stepper motors are constantly moving until the G-Code has finished (I have a very long G-Code which loops the movement for about 3 hours) which is very inefficient. My question is: is there a way in which I can have the soil moisture sensor activate the G Code so that the stepper motors are only on and moving whilst the system is actually watering and not just constantly moving for 3 hours?

1 Like

Hi Mitchell,

How complex is your G Code? You can control a stepper motor directly from an Arduino and Motor Driver, using the stepper.h library. There are some examples of how to use it in our Stepper Motors with Arduino Tutorial. You could easily command your steppers on only when the soil moisture is correct by using a While() loop.

If you want to continue using GRBL I read that it supports a Cycle Pause and Cycle Start button. By connecting A1 to ground it should pause the G Code. Connecting A2 to ground should start the cycle again.

Hope that helps! Be sure to share your finished project with us either here on the forums or in the projects section of the website!

Thanks for the reply, the G code is very simple. After doing some research, I have found an example code that I think will work, here it is:

#define EN        8  

//Direction pin
#define X_DIR     5 
#define Y_DIR     6
#define Z_DIR     7

//Step pin
#define X_STP     2
#define Y_STP     3 
#define Z_STP     4 

int delayTime=30; //Delay between each pause (uS)
int stps=6400;// Steps to move

void step(boolean dir, byte dirPin, byte stepperPin, int steps)


  digitalWrite(dirPin, dir);


  for (int i = 0; i < steps; i++) {

    digitalWrite(stepperPin, HIGH);


    digitalWrite(stepperPin, LOW);




void setup(){

  pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);

  pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);

  pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT);

  pinMode(EN, OUTPUT);

  digitalWrite(EN, LOW);


void loop(){

  step(false, X_DIR, X_STP, stps); //X, Clockwise
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  step(false, Z_DIR, Z_STP, stps); //Z, Clockwise


  step(true, X_DIR, X_STP, stps); //X, Counterclockwise
  step(true, Y_DIR, Y_STP, stps); //Y, Counterclockwise
  step(true, Z_DIR, Z_STP, stps); //X, Counterclockwise



All I need to do is change how long the motor runs for in the X direction and Y direction for this code to work, they also need to be separate times as well. e.g. X-direction runs for 5 seconds and Y direction will run for 2 seconds.

I have played with the delays in the code, however, when I change “delayTime” it only changes the speed of the motor.

Any help on how to change the timings would be greatly appreciated.

This looks good! The delayTime variable controls the length of the pause between each step in the stepper motor I believe. Thats why its changing the speed. Probably the easiest way to do this is to set the stepper motor speed to something that you’re happy with, then just play with the number of steps ( int stps=6400;) until the motor runs for the amount of time that you want. You could calculate it as well using the delayTime.

The times are all in milliseconds. 5 seconds is 5,000ms. With delayTime=30, 167 steps should take approximately 5 seconds.

To make the x and y times different. Add a new variable “int stpsY=” and use that variable for the Y axis:

step(true, X_DIR, X_STP, stps); //X, Counterclockwise
step(true, Y_DIR, Y_STP, stpsY); //Y, Counterclockwise
step(true, Z_DIR, Z_STP, stps); //X, Counterclockwise

I should point out that stepper motors aren’t typically controlled directly by the amount of time that they run. They are built to turn precise distances. So usually you calculate how many steps you want them to turn, and then set the speed to whatever you want.

Good luck! Hopefully that gets you moving!