Stepper Motor, step counting. Possible?

Hi all,

I am working on a automated blind project. I have a code which works perfectly as it is but I want to add some safety features in the code.

At present I can fully or partly open and close the blinds using commands on my phone or pc via mqtt.

The problem is, if the blinds are for example part open I can still select fully close which will cause the motor to try to turn the blinds to far.

Is there a way to count and log the amount of turns of the stepper motor so this can then be used to stop the motor going to far.

The code incorporates limit switches to solve this problem but physically using limit switches in no practical.

My code is as attached as it is to big to post.

Any Help on this would appreciated

working_mqt_switch_upload.ino (8.89 KB)

You can certainly count the steps that the stepper motor takes (if it is not overloaded), and keep track of the current position.

However, you need a limit switch or some other other means to "home" the stepper, every time the program starts running (i.e. the Arduino is powered on).

It does not damage a stepper motor to instruct it to take steps when stalled, so many people simply run a stepper into an end stop at program startup.

If I find an way of fitting one limit switch to use to home the motor what would I put in the code to count the position( turns of the motor)

Thanks.

pseudo code:

home:  
take a step
check switch
if (switch not closed) go to home:
position = 0

Elsewhere in the code:

take a step
if (DIR == 1) position=position+1
if (DIR == 0) position=position-1

Would I have to declare DIR somewhere in the code?

DIR is typically the name of the motor driver input that sets the step direction.

Your code must determine which direction the motor is to turn, and set the DIR input accordingly.

Then, either increment or decrement the current position, each time the code instructs the motor to take a step.

These links may help

Stepper Motor Basics
Simple Stepper Code

...R