Controlling a stepper motor using Stepper library - Photon crashes with 'usage fault'

Hi all,

I’ve got a stepper motor rigged up and it’s all working fine, however, all the libraries I’ve found are blocking. I’m looking for a non-blocking library as I want to use the motor in conjunction with sensors and the cloud (my use case is simple - turn the stepper motor at a defined speed until a cloud function or sensor is triggered and then stop or reverse the motor. I guess it should also accelerate / deaccelerate if that’s necessary).

I came across the stepper library installed in the particle cloud IDE simply named ‘Stepper’, and looking through the code it appears to be non-blocking. I clicked ‘verify’ and the code compiles fine. However, when deploying to the photon it immediately crashes with a red flash SOS signal. 5 red flashes is apparently a ‘usage fault’.

Has anyone else had any problems using this library out of the box? If not, how do I go about finding the fault?

Thanks!

There is a problem with a zero-division when passing 0 as whatSpeed (and also if number_of_steps was set to 0).

/*
  Sets the speed in revs per minute
*/
void Stepper::setSpeed(long whatSpeed)
{
  this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
}

The sample does this stepper1.setSpeed(0); causing the usage fault.

Version bumped with the fix from @ScruffR do so give it another spin :slight_smile:

1 Like

Thanks guys! Out of interest how did you spot the issue? Is there a debugging tool you can use or was it a case of going through the code line by line till you find the problem?

Kenneth, pulled the latest version but it’s failing to compile… it’s looking for a non-existant application.h in Stepper.cpp on line 48

hmm… it worked for me. Can you remove the library and include it in your app again?

Recreated the app again and it compiled this time - very odd!

Unfortunately the version of the library available via Particle Build is still 0.0.2 and doesn’t have ScruffR’s fix in it. Do you have to re-publish the library or does it just take time to update?

There’s a weird thing going on that i brought up to the :particle: team.

If you don’t mind, can you open in incognito mode and see if that library is v.0.0.3? thanks!

Yep that did the trick, though I ran into the application issue again:

stepper-demo.cpp:2:29: fatal error: Stepper/Stepper.h: No such file or directory
#include "application.h"
^
compilation terminated.
make[1]: *** [../build/target/user/platform-6stepper-demo.o] Error 1
make: *** [user] Error 2

Had to relaunch the IDE and recreate the app to fix the issue.

For now I've set speed=100 as I don't have a pot to hand. The stepper motor turns successfully, but the program crashes after half a revolution (no SOS flashes). What's the best way of approaching debugging an issue like that? Sorry for all the questions.. new to this style of coding and development!

1 Like

@ScruffR will have to jump in here. I don’t have spare time to look into it :wink:

Happy to assist the awesome @kennethlimcp!

So @richw, are you using the library sample or any other code, when you see the crash?

If it’s your own, then I’d need to see it.

1 Like

I’m using the library sample, but I’ve hardcoded speed=100 because I don’t have a potentiometer to hand.

When speed is set to 100 it crashes after half a revolution. When speed is set to 50 it crashes after a quarter of a revolution.

Does this also happen without stepper attached?

Since you said

you are not really running the original sample anymore, so could you please post your code for me to try out.

Could you also try SYSTEM_MODE(SEMI_AUTOMATIC) to exclude issues with connection loss.

If this does not help, try commenting out the display() line too.


I've got it running for several hours without any SOS.

I’ve tried a few things in the past days but now I’m even more confused. Without the stepper motor plugged in it the board runs fine. When I plug the stepper in the board restarts after a certain amount of time.

I’ve been using a breadboard voltage regulator power supply that allows up to 700mA. I wondered if it was exceeding the current and stopping a consistent flow of electricity getting to the photon, hence the restart. I purchased a 2A 5V power supply and connected that to the photon and stepper motor driver board. Within half a revolution it blew the power supply!

From what I’ve read online the stepper motor only draws around 92mA - don’t understand why the power supply blew :frowning:

How does your wiring look?
What stepper driver are you using?
Have you got flyback diodes in place?

I’m using a 28BYJ-48 with ULN2003 driver board. They were connected to the same 5V 2A power supply when it blew. I’ve now tried powering the Photon via USB and everything works perfectly. I think the limit on a USB port is 500mA, so can’t see how it could have exceeded 2A.

That looks OK (nicely done :+1:)

It does seem odd, to actually blow a 2A supply this way. I wouldn’t think this should happen (again) :wink:

But the thing with 5V DC power supplies is that the rating does not tell you anything about the quality of the DC.
You might need an extra filtering stage to get pulsing 5V DC into “good quality DC”.

This should still not harm the supply, but might upset your circuit.

Cheers for confirming. I’ll try and order another power supply (it was quite cheap… only about £4) and see if it does the same. If it does I’ll take a look into the filtering and take it from there :smile:

If you have any means of checking the quality first (e.g. oszillograph) do that first :wink:

But this cheap, I wouldn’t expect it to be well filtered :stuck_out_tongue_winking_eye:

1 Like

I have tried this setup and it seems that you need 5v logic to control the ULN2003. I had to use a logic level converter to turn the photon’s 3.3v into 5v in order to satisfactorily drive this motor. I would love to figure out a way to do away with this complex setup and use the 3.3v logic directly in the stepper driver.