 # How to count analogWrite() pulses?

Hello, i am trying to control a Stepper motor using a nodeMCU. I have a stepper driver that does 1 step every pulse. I got the stepper running at different speeds using:

analogWriteFreq(speed_frequency) analogWrite(D1, 500);

to stop: analogWrite(D1, 0)

My question is how can i count these pulses? For example if i need my stepper to do exactly 100 steps? I don't want to use digitalWrite() because that blocks the code when the stepper is running.

I don’t want to use digitalWrite() because that blocks the code when the stepper is running.

The program will only block if you write it that way, for instance by using a for loop, but it does not have to be done that way. Something like this pseudo code will work

``````previous step time = millis()
stepping = true
step count = 0

start of loop()
current time = millis()
if current time - start time equals interval between steps and stepping is true
move stepper 1 step
previous step time = current time
increment step count
if step count equals required number of steps
stepping = false
end if
end if

//do other stuff here

end of loop()
``````

Ingmar05: My question is how can i count these pulses? For example if i need my stepper to do exactly 100 steps?

Don't use analogWrite().

Have a look at the examples in this Simple Stepper Code - especially the second non-blocking example.

What is the maximum number of steps per second that you need? Although, with the speed of an ESP8266 you should have no problems.

...R

You can check the status of the output pin by reading the PINx pseudo variables. You can count the state changes manually.

Although it might be easier to just calculate how much time it is going to take. You’re setting the frequency of the pulses. It should be trivial to multiply the period by the number of steps, and check millis() or micros() until that time arrives.

UKHeliBob: The program will only block if you write it that way, for instance by using a for loop, but it does not have to be done that way. Something like this pseudo code will work

``````previous step time = millis()
stepping = true
step count = 0
``````

start of loop() current time = millis()  if current time - start time equals interval between steps and stepping is true    move stepper 1 step    previous step time = current time    increment step count    if step count equals required number of steps      stepping = false    end if  end if

//do other stuff here

end of loop()

``````
Robin2:
Don't use analogWrite().

Have a look at the examples in this [Simple Stepper Code](http://forum.arduino.cc/index.php?topic=277692.0) - especially the second non-blocking example.

What is the maximum number of steps per second that you need? Although, with the speed of an ESP8266 you should have no problems.

...R
``````

On both examples using digitalwrite() won't the steps get delayed if i have functions in loop() that take for example 1 second to complete?

won't the steps get delayed if i have functions in loop() that take for example 1 second to complete?

Yes, so don't use code that takes one second to execute. What is it that you are doing that takes that long ?

UKHeliBob: Yes, so don't use code that takes one second to execute.

+1

...R

UKHeliBob: Yes, so don't use code that takes one second to execute. What is it that you are doing that takes that long ?

I use a library called Blynk to send and read data over wifi and at the same time read multiple sensors and control relays. I did a test when i pushed data over wifi when the stepper ran at a high speed and the stepper jerked every time i did that. It wasn't that bad, but i plan adding functionality in the future and i tought i should use analogWrite() because the PWM will be stable no matter what i do in the code.

Ingmar05: I use a library called Blynk

Maybe the library code (for Blynk or for your sensors) was written carelessly without any thought for what other activities might need to happen. Unfortunately that would not be unusual.

What sensors are you using?

...R

Robin2: What sensors are you using?

Right now just temperature, humidity and 2 reed switches. Even if i used just one Blynk function in every 5 seconds while the stepper was running. The stepper would still jerk at every five seconds and if i put the function in the loop the stepper would slow down about 10 times. The function was Blynk.virtualWrite(virtualPin, "text"). I guess i could just stop using Blynk, but there is no way i am gonna make a more efficient smarphone interface myself. Correct me if i am wrong, but wouldn't using analogWrite() solve all my problems with controlling the stepper? I could use the sample stepper code you provided to (almost) accurately turn analogWrite() on and off. The only problem i have with that is i couldnt accelerate or deccelerate the stepper and for some reason no one uses analogWrite to controll steppers.

Ingmar05: Correct me if i am wrong, but wouldn't using analogWrite() solve all my problems with controlling the stepper?