Stepper jerky with hall sensor and if code

Setup:
NEMA 23
Ardiuno Nano
TB6600 controller:

I am looking to have my nema 23 stepper spin when triggered by a latching hall sensor and stop when not triggered. The stepper spins perfectly smooth with no "IF" statement. But when I try to add the "IF", it still works but the stepper gets real jerky regardless if I microstep or not..

I tried the accelstepper library but I really do not need acceleration parameters. Can someone recommend what I should try? Thank You!

int hallSensorPin = 9;
int hallSensorPin2 = 10;
int state = 0;
#define dirPin 2
#define stepPin 3
#define pwrOff 4
void setup() {
  // Declare pins:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(pwrOff, OUTPUT);
  pinMode(hallSensorPin, INPUT);
  digitalWrite(dirPin, HIGH);
 
}

void loop() {
   
   state = digitalRead(hallSensorPin);

  if (state == HIGH)
  {

  digitalWrite(pwrOff, LOW);
    digitalWrite(stepPin, HIGH);
  delayMicroseconds(400);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(400);
  }
   
  else {
    digitalWrite(pwrOff, HIGH);
  }
 
}

Please post schematics. The HIGH and LOW doesn't make it clear.
What's "pwrOff" connected to? The ENA (enable)?

Yes- pwrOff is connected to enable+ pin. All else is as shown in image minus the hall sensor.

Code has 2 hall sensors for future use, but as long as I can get 1 working smoothly, should be OK.

Thank You!

Why not post a complete schematics? The ENA handling code doesn't look good assuming ENA- is connected to GND like PUL- and DIR-.

Seems like the Hall sensor part of the schematic is as important as anything else.

If the motor spins okay without the "if" then maybe the "if" is responding to a bad Hall sensor input.

If you don't have an oscilloscope you might try this code and the serial plotter to see how stable your Hall input is:

int hallSensorPin = 9;
int hallSensorPin2 = 10;
int state = 0;
#define dirPin 2
#define stepPin 3
#define pwrOff 4
void setup() 
{
    Serial.begin(115200);
    
    // Declare pins:
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    pinMode(pwrOff, OUTPUT);
    pinMode(hallSensorPin, INPUT);
    digitalWrite(dirPin, HIGH);
 
}

void loop() 
{    
    Serial.println( digitalRead(hallSensorPin) ? 1:0 );
 
}

What do you see on the serial plotter with this?

Thanks Blackfin.. Plotter looks smooth from as much as I see.

I'm thinking it's because the hall sensor state is checked between every step with this code, adding a slight delay to every step causing jerkiness.

Obviously I am a beginner- and I admittedly do not know what code would be better and smoother.

Could anyone recommend code to complete XXX number of steps before checking the hall state again?

Confirmed... I tested by pasting this section 40 times in the code.

 if (state == HIGH)
  {

  digitalWrite(pwrOff, LOW);
    digitalWrite(stepPin, HIGH);
  delayMicroseconds(400);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(400);

digitalWrite(pwrOff, LOW);
    digitalWrite(stepPin, HIGH);
  delayMicroseconds(400);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(400);

digitalWrite(pwrOff, LOW);
    digitalWrite(stepPin, HIGH);
  delayMicroseconds(400);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(400);

digitalWrite(pwrOff, LOW);
    digitalWrite(stepPin, HIGH);
  delayMicroseconds(400);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(400);

digitalWrite(pwrOff, LOW);
    digitalWrite(stepPin, HIGH);
  delayMicroseconds(400);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(400);

40 times!
  }

and the jerkiness goes away...

Now just how to do this a little more efficiently without counting hundreds of lines to determine how many steps i need.

Declare a static int counter in the loop.

Increment it every time through the loop, every step that is.

When the counter gets to N, read the sensor and reset the counter.

Blind while counting, you pick N…

HTH

a7

Many HESs have open collector, NPN outputs and need INPUT_PULLUP, post the datasheet for yours.

What is turning the motor or engaging/disengaging the Hall sensor in that plot?

Yes, what unlatches the HES?

Better:

Use a micros() based timer to decide if it is time and appropriate to take a step.

In the same now unfettered loop(), read you sensor and decide if stepping or not.

a7

It's a little too much to explain here. but it's for this project Flipping magnet motor hooked up to 2 nema 17 stepper motors as generators - YouTube

There will be 2 hall sensors. One triggered by the center magnet- one triggered by the dogbone rotor position.

Rotate center magnet till in position then stop. When dogbone rotor (which will be behind 5-8 degrees) is in position, fire the center magnet again to make the next 1/2 turn.

The center shaft will have 2 magnets to latch / unlatch hall 1. And one of the dogbone rotors (which both have 2 magnets opposite polarities) will latch / unlatch the other

I have no idea if this is the tree y'all are barking up:

but without a life, I had time to play with it a bit.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.