Stepper motor with Hall sensor using Arduino and Labview

Hi Everyone,

I am working on a new project in that I am using a Filter wheel. Basically, the Filter wheel contains 6 filter slots of diameter 50mm. I want to use one filter at a time. after using the first filter I will go to the next filter. For rotating the filter wheel I am using a stepper motor.
I am using Arduino as a brain for the stepper motor. For front end Part, I am using LabVIEW.

I am attaching my code below. For finding the HOME position I am using a Hall sensor. My idea is whenever we start the motor it has to rotate until it reaches to HOME position. After reaching the HOME position the motor will stop. Now we know that motor is at HOME position. As all the six filter are equidistant from each other the motor has to move the same number of steps for moving from one filter to another.
So whenever Arduino receives a command from LabVIEW the motor has to move the predetermined steps. Can you please suggest me whether the below code work or not?

#include <AccelStepper.h>

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

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

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

int sign = 1;              // Either 1, 0 or -1

int count = 0;              // Count the hall sensor high

int buttonState = 0;        // variable for reading the hall sensor1 status

AccelStepper Stepper(1,2,3);   //use pin 2 and 3 for dir and step, 1 is the "external driver" mode (A4988) or Easy driver



void setup() {
  
  Stepper.setMaxSpeed(300);           //set max speed the motor will turn (steps/second)

  Stepper.setAcceleration(50);       //set acceleration (steps/second^2)

  Serial.begin(9600);
   
  pinMode(ledPin, OUTPUT);            // initialize the LED pin as an output:   
 
  pinMode(hallPin, INPUT);           // initialize the hall effect sensor pin as an input:
  
  
hallState = digitalRead(hallPin);  // read the state of the hall effect sensor:

if (hallState == LOW)
{     
   //digitalWrite(ledPin, LOW); // turn LED off:
   Stepper.move(30000);
    Stepper.run();
 }
 else
  {
   digitalWrite(ledPin, HIGH);  // turn LED on:
   Stepper.stop();
  }

}


void loop() {

  
/* buttonState = digitalRead(hallPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  
  if (buttonState == LOW) {    
    digitalWrite(ledPin, HIGH);  // turn LED on:  
    count++;                    // add 1 to the count
    if (count >= 5) {
      count = 0;
    } 
  }
  else {
    digitalWrite(ledPin, LOW); // turn LED off:
  }
*/

char c;

if (Serial.available() > 0) {

c = Serial.read();

    
    if (c == 'W') {
   hallState = digitalRead(hallPin);  // read the state of the hall effect sensor:

  if (hallState == LOW)
{     
     Stepper.move(30000);
     Stepper.run();
 }
 else
  {
    Stepper.stop();
  }
    }
    if (c == 'U') {     // forward
      sign = 1;
       Stepper.move(5000 * sign);
       Serial.write('u');
       delay(500);
      }
      
    if (c == 'B') {  // forward
      sign = 1;
       Stepper.move(5000 * sign);
       Serial.write('b');
       delay(500);
       }
    
    if (c == 'V') {  // forward
      sign = 1; 
          Stepper.move(5000 * sign);
          Serial.write('v');
          delay(500);
       }
       
    if (c == 'R') {  // forward`
      sign = 1;
           Stepper.move(5000 * sign);
           Serial.write('r');
           delay(500);
       }
       
    if (c == 'P') {  // forward
      sign = 1;
          Stepper.move(5000 * sign);
          Serial.write('p');
          delay(500); 
    }
    if (c == 'S') {  // stop
      sign = 0;
       Stepper.move(0 * sign);
       Stepper.stop();
    }

}
 
  //Stepper.run(); //run the stepper. this has to be done over and over again to continously move the stepper
  Stepper.stop();

}

Hi,
What arduino controller is it written for, does it compile and why not try it.

What do you have to drive the stepper and can you post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom... :slight_smile:

I don't know if it is significant but a stepper motor with 200 steps per revolution cannot move an exact 60 degrees.

And when a program starts the Arduino will have no idea where the stepper motor is. You will need (if you don't already have it) a mechanism to move the system to a known HOME or ZERO position. That is often done by moving the system until a switch is triggered.

And, as @TomGeorge has said, you need to try your program. There would be little point wasting time giving you advice if it works :slight_smile:

...R
Stepper Motor Basics
Simple Stepper Code

There are no errors in compilation. I am using Big Easy Driver along with Arduino. I know this code will work. Why I posted here is to optimize the code. Now I cannot go from the first filter to 4th filter directly. I have to go by one by one. So I want to optimize the code with more loops.

Circuit connections are:
pin1 of Arduino to DIR pin of Driver
pin2 of Arduino to STEP pin of Driver
pin11 of Arduino to Hall Sensor
Pin13 of Arduino to LED
DRIVER M+ pin is connected to 12V
Driver GND pin is connected to GND pin of Arduino

Hi Robin

I am using Hall sensor for finding the HOME position. So whenever I switch on the power supply the motor will start rotating. It will rotate until the HALL sensor was high. This defines the HOME position.

Janatagarage:
Now I cannot go from the first filter to 4th filter directly. I have to go by one by one. So I want to optimize the code with more loops.

I don't understand. I presume it takes X steps to move from one position to another (with an occasional adjustment to allow for the inability to divide 200 by 6.

If you know the home position then just tell if to move X steps and it will be at the next position. Rinse and repeat.

Why do you think "more loops" might be needed?

...R