Arduino Uno and Getting a stepper motor to move

Hi Folks

What I am trying to do is use a c# application to move a stepper motor using an arduino uno and a DRV8825.
I have written the app, it is connecting to the serial port and sending the word "azleft" through the serial port when I click the button.

private void button5_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("azleft");
           
        }

If I upload this code to the arduino :

/*Example sketch to control a stepper motor with DRV8825 stepper motor driver, AccelStepper library and Arduino: continuous rotation. More info: https://www.makerguides.com */

// Include the AccelStepper library:
#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
String val;
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  // Set the maximum speed in steps per second:
  stepper.setMaxSpeed(1000);
}

void loop() {
  
    // Set the speed in steps per second:
  stepper.setSpeed(400);
  // Step the motor with a constant speed as set by setSpeed():
  stepper.runSpeed();
  
}

The stepper motor runs just fine.

So I thought if I changed the above code slightly so it is like this :

/*Example sketch to control a stepper motor with DRV8825 stepper motor driver, AccelStepper library and Arduino: continuous rotation. More info: https://www.makerguides.com */

// Include the AccelStepper library:
#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
String val;
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() 
{
  Serial.begin(9600);
  // Set the maximum speed in steps per second:
  stepper.setMaxSpeed(1000);
}

void loop() {
  if(Serial.available())
  {
  val=Serial.readString();  
  }
    if (val=="azleft")
  {
    // Set the speed in steps per second:
  stepper.setSpeed(400);
  // Step the motor with a constant speed as set by setSpeed():
  stepper.runSpeed();
  }

  
  }

Then the motor would only move when I click the button.
However the motor is not moving , I see a light on the arduino flash when I click the button but that is all.

A little help would be appreciated
thanks :slight_smile:

paulr1:
...

So I thought if I changed the above code slightly so it is like this :

.....
Then the motor would only move when I click the button.
However the motor is not moving , I see a light on the arduino flash when I click the button but that is all.

A little help would be appreciated
thanks :slight_smile:

I would suggest that you read Robin2's excellent tutorial Serial Input Basics to see how to receive and parse serial data

hope that helps....

add debug-output to your arduino-code to analyse what the arduino really receives.

/*Example sketch to control a stepper motor with DRV8825 stepper motor driver, AccelStepper library and Arduino: continuous rotation. More info: https://www.makerguides.com */

// Include the AccelStepper library:
#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
String val;
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup()
{
  Serial.begin(9600);
  // Set the maximum speed in steps per second:
  stepper.setMaxSpeed(1000);
}

void loop() {
  if (Serial.available()) {
    Serial.println("if(Serial.available() is true");
  
    val = Serial.readString();
    Serial.print("content of variable val is #");
    Serial.print(val);
    Serial.println("#");
  }
  
  if (val == "azleft")
  {
    // Set the speed in steps per second:
    stepper.setSpeed(400);
    // Step the motor with a constant speed as set by setSpeed():
    stepper.runSpeed();
  }
}

best regards Stefan

Change your code with this and run it.

if (Serial.available())
   {
      val = Serial.readString();
      Serial.print('<');
      Serial.print(val);
      Serial.print('>');
   }

You will see this in serial monitor:

<azleft
>

That is because serial monitor is sending, 'a','z','l','e','f','t','\r','\n'. That does not = 'a','z','l','e','f','t'.
The '\r' and '\n' are the CR and LF characters sent by serial monitor with the line endings set to Both CR&LF. Change that to No line ending and those characters will not be sent.

And I agree with sherzaad, the serial input basics tutorial shows better ways to receive serial data without using the problematic String class and blocking functions.

Thanks guys , figured it our , changed the app to send a number , then used serial.write instead of writeline and instead of using val=Serial.readString used val=Serial.Parseint.

Seems to work and is doing exactly what I need

:slight_smile: