Arduino+stepper motor+LabVIEW using VISA

Hiii All,

In my project for rotating the filter disc i am using a stepper motor, this motor will have a well defined home position by using hall effect sensor. I want to rotate the motor shaft 60 deg when it receives command from the LabView(VISA).

Help me to start this. Thank u...

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. I would recommend using the technique in the third example.

Most stepper motors have 200 steps per revolution and cannot give exactly 60 deg. They could move either 33 or 34 steps. If you use microstepping the error will be smaller.

...R
Stepper Motor Basics
Simple Stepper Code

Hii Robin2,

Thanks for the reply. I don't need exact 60 degrees with small error is Ok. I am using a hall effect sensor for obtaining the home position. After reaching the home position the motor has to rotate 60 degree whenever i give command from LabVIEW.

Janatagarage:
I am using a hall effect sensor for obtaining the home position. After reaching the home position the motor has to rotate 60 degree whenever i give command from LabVIEW.

Have you studied the links I gave you?

Have you written short Arduino programs to {a} move the stepper to the HOME position and {b} to move it N steps without any connection to LabView?

Then write a short program to receive data from LabView without any attempt to drive any motors.

Break your development into small pieces.

...R

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Also, please don't use unnecessary white space in the program as that just requires more scrolling when reading it.

...R

#include <Stepper.h>

#include <CustomStepper.h>

const int hallPin = 12;     // the number of the hall effect sensor pin1 for defining the home position

//const int hallPin2 = 14;     // the number of the hall effect sensor pin2 for calculating no of levels the disc has to rotate

const int ledPin =  13;     // the number of the LED pin

int hallState = 0;          // variable for reading the hall sensor status

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // initialize the stepper library on pins 8 through 11

CustomStepper stepper(8, 9, 10, 11);


void setup() { 
  myStepper.setSpeed(60); // set the speed at 60 rpm:
   
  pinMode(ledPin, OUTPUT);  // initialize the LED pin as an output:   
 
  pinMode(hallPin, INPUT);  // initialize the hall effect sensor pin as an input:

 Serial.begin(9600);         // initialize the serial port:

}


void loop() {
 
     //stepper.run();

  // read the state of the hall effect sensor:
 
hallState = digitalRead(hallPin);
if (hallState == LOW)
{     
    // turn LED on:
    digitalWrite(ledPin, LOW);
     
    Serial.println("HOME Position");
    Serial.println("clockwise");
   myStepper.step(stepsPerRevolution);
   delay(500);
     
  }
 
  else
  {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
    stepper.setDirection(STOP);
  }
     
}

You seem to be trying to use two different libraries at the same time -Stepper and CustomStepper. That won't work.

I am not familiar with the CustomStepper library.

What stepper motor driver are you using?

...R

You can't have two stepper libraries at the same time.
Repair this and then you have to decide, if and what microstepping you will use.

Let's suppose, you use 1/4 microstepping.
Then a full revolution (= 360 degrees) requires 4*200 steps, thus 62 degrees will be reached after:
800 steps/360 * 62 = 138 steps (1/8 microstepping requires 276 steps, 1/16 -> 551)

Your pseudo code will look like:

Include the (right) library
Initiate the stepper (constructor)
Assign all needed pins (at least the one for the hall sensor and the LabView signal as inputs and the LED as output)
In setup() run the stepper to find the hall sensor signal (= 0 position) and stop it there
In loop() read the input pin of the LabView
When a signal was detected, send the calculated '62 degrees' steps

Dear rpt007 and robin2

thanks for the reply. I am using my own driver . here is the updated code

#include <CustomStepper.h>

const int hallPin = 12;     // the number of the hall effect sensor pin1 for defining the home position

const int ledPin =  13;     // the number of the LED pin

const int Labviewpin1= 5;

int hallState = 0;          // variable for reading the hall sensor statu

int Labviewstate = 0;

CustomStepper stepper(8, 9, 10, 11);



void setup() {

stepper.setRPM(30);   

stepper.setSPR(200);
   
pinMode(ledPin, OUTPUT);  // initialize the LED pin as an output:   
 
pinMode(hallPin, INPUT);  // initialize the hall effect sensor pin as an input:

pinMode(Labviewpin1, INPUT);  // initialize the hall effect sensor pin as an input:

Serial.begin(9600);         // initialize the serial port:



hallState = digitalRead(hallPin);  // read the state of the hall effect sensor:
if (hallState == LOW)
{     
    
    digitalWrite(ledPin, LOW); // turn LED on:
    stepper.run(); 
    delay(500);
     
  }
 
  else
  {
   
    digitalWrite(ledPin, HIGH);  // turn LED off:
    stepper.setDirection(STOP);
  }

}


void loop() {

Labviewstate = digitalRead(Labviewpin1);

 if (Labviewstate == HIGH)
 { 
  stepper.setDirection(CW);
  stepper.rotateDegrees(62.0);
 }

 else
 {
  stepper.setDirection(STOP);
 }
 
 }

You have posted updated code but you have not told us what it does and does not do - i.e. what do you still need help with.

And if the problem lies with the use of the CustomStepper library I will not be able to help.

...R

Hii Robin2

Thanks for reply. By my understanding the code should work for my application. In my code i want to store the number of degrees the motor has rotated and no of times the arduino got command from LabIEW.

Janatagarage:
By my understanding the code should work for my application.

Until you tell us otherwise I will assume that it does.

...R

In my code i want to store the number of degrees the motor has rotated and no of times the arduino got command from LabIEW. whenever hall sensor was high these values( no of degrees the motor rotated and no of times labviewpin is high) should become zero.

And does your program do what you want?

If not, you need to tell us what it actually does.

...R

I want to store the number of degrees the motor has rotated and no of times the arduino got command from LabIEW. whenever hall sensor was high these values( no of degrees the motor rotated and no of times labviewpin is high) should become zero. can u please suggest me how to start this??

Janatagarage:
can u please suggest me how to start this??

I think this is the 3rd time I have asked you to explain what the code in your Reply #8 actually does and how that relates to what you want it to do.

I assume that the code in Reply #8 is your most recent attempt to achieve what you want?

...R

The motor will rotate till the hall sensor is high, when it is high it will stop( this code i included in void setup)

the motor will rotate 62 when ever it receives signal from labview(this code i included in void loop)....

adding to this i want to write a code which stores the no of times the motor got command from labview and no of degrees the motor had rotated and whenever hall sensor was high these values( no of degrees the motor rotated and no of times labviewpin is high) should become zero....

Your code for moving to the HOME position does not seem to be robust. It should just move a single step between each test of the hall sensor.

Why not add some code like this, for keeping count whenever a command is received

numCommands ++;
totalDegrees += 62;

or have I misunderstood what you want?

...R

Hii robin2

Thanks for the reply. Code for moving the motor to HOME seems Ok for me. Whatever you suggested for counting the no of commands and degrees the motor move it will try to incorporate in my Final code.