Close

Steppers and the Cloud

A project log for Vendotron

A vending machine that allows purchases of items via mobile phone or bitcoin

mike-machadoMike Machado 08/08/2016 at 01:370 Comments

The brains inside Vendotron is a Particle Photon. This device is super awesome because its small, cheap, powerful and very easy to use thanks to the great documentation and example code. There is one little problem, however. Given that it needs to stay connected to "The Cloud", it spends some time every iteration of the standard loop() checking the network, processing events, etc. Usually this is all well and good, but when stepper motors are involved, it gets a little tricky due to the precise timing requirements for stepping.

I am using the AccelStepper library since to spin a heavy load like Vendotron's carousel filled with goodies, you need to start slow and ramp up to speed. Luckiy, this library was already ported to work with particle devices by the community, so the code was basically copy paste from the Arduino Uno I used during my initial prototyping. On my Uno things worked flawlessly. When I first set the same code up on the photon, I got unexpected results. The motor was very jerky and top speed of the motor was very brief. Have a look:

Far from a smooth acceleration. After much searching, I discovered the timing issue. The particle code that managed the cloud was getting in the way. Luckily I discovered the particle people made it so people wanting more control over the cloud connection could use what is called SYSTEM_MODE of MANUAL. This allows you to say when its ok for the photon to check in with the cloud. I modified my test code so that it only processes cloud 'stuff' while the motor is not moving, and voilà, that fixed it! The motor now spins perfectly smooth, just like it did on the Arduino.


Before and after of the testing code can be found in this gist: https://gist.github.com/machadolab/e3bf5fb534a2a65bd561311044e2df0a


According to the documentation, you need to call the Particle.process() method at least every 20 seconds, preferably more often, in order to avoid cloud/wi-fi connection issues. If your application requires long runs of spinning stepper motors, this technique may not work for you, but for this application, which should never have to spin the carousel more than 20 seconds to line up a door, this works great.

Here is the final electronics mounted, along with another spin video showing the smooth AccelStepper code in action!


Discussions