Arduino library for the uStepper Board

This is the uStepper Arduino library, providing software functions for the different features of the uStepper board.

This version of the library adds a new functions to the library: MoveToEnd() - This function makes the motor turn in the specified direction, until an obstacle is detected, at which point the motor stops and resets its home position Changed the "LimitDetection" example to make use of the new moveToEnd() function

Also some adjustments have been made to different functions. See changelog

The uStepper library contains the following features:

The library uses both timer one and timer two in order to function properly, meaning that unless the user of this library can accept the loss of some functionality, these two timers are unavailable and the registers associated with these timers should not be reconfigured.

Timer one is used for sampling the encoder in order to provide the ability to keep track of both the current speed and the angle moved since the board was reset (or a new home position was configured). Also the drop-in features missed step detection and correction is done in this timer.

Timer two is used to calculate the stepper acceleration algorithm.

In order to get some features working, it was necessary to write functions to control the I2C hardware in the MCU, since the build in wire library of Arduino uses interrupt which doesn't play well when used inside a timer interrupt routine. Therefore if the user of this library needs to use the I2C bus, the user should use the functions defined in this library instead of wire library !
To install the uStepper library into the Arduino IDE, perform the following steps:

The library is tested with Arduino IDE 1.8.5

MAC users should be aware, that OSX does NOT include FTDI VCP drivers, needed to upload sketches to the uStepper, by default. This driver should be downloaded and installed from FTDI's website:
The uStepper should NOT be connected to the USB port while installing this driver !
This is not a problem for windows/linux users, as these drivers come with the arduino installation.

The acceleration profile implemented in this library is a second order profile, meaning that the stepper will accelerate with a constant acceleration, the velocity will follow a linear function and the position will look like an S-curve (it will follow a linear function at top speed and a second order function during acceleration and deceleration). As a result, the position of the stepper motor will have continous and differentiable first and second derivatives.

The second order acceleration profile can be illustrated as follows (Source):


From the above picture it can be seen how the motor moves with this second order acceleration profile. The postion is depicted as orange, velocity as red and the acceleration as green.

The algorithm used is NOT designed by me, I ONLY implemented the algorithm! credit is therefore given to the appropriate author: Aryeh Eiderman.

for those interested, the deriviation of the algorithm used can be found at the following webpage (Which is neither owned nor affiliated with ON Development IVS in any way): Real Time Stepper Motor Linear Ramping Just by Addition and Multiplication


(C)2016 ON Development IVS

Creative Commons License

The code contained in this file is released under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

The code in this library is provided without warranty of any kind - use at own risk! neither ON Development IVS nor the author, can be held responsible for any damage caused by the use of the code contained in this library !

To do list
  • Clean out in unused variables
  • Update comments
  • Add support for limit switches
  • Split the library into multiple files
Known Bugs
  • No known bugs
Thomas Hørring Olsen (
Change Log
  • Changed MoveToAngle() function, to support update of angle setpoint in PID mode
  • Changed getMotorState() function, to support update of angle setpoint in PID mode
  • Fixed bug in moveAngle() function, where negative inputs had no effect
  • Changed the "LimitDetection" example to make use of the new moveToEnd() function
  • Added moveAngle() and MoveToAngle() functions
  • Minor adjustments in setup routines
  • Adjusted parameters in limitDetection example, and dropin example
  • Added setCurrent() function to the uStepper object, for the user to easily change current setting
  • Minor corrections in PID algorithms
  • Fixed a bug in the implementation of the standalone PID controller, primarily anti windup
  • Added example to show how limit detection can be implemented without the use of limit switches
  • Adjusted encoder.setHome() function to also reset the stepsSinceReset variable
  • Adjusted the code related to the PID closed loop position control. Now it actually works as intended!
  • Fixed Bug with encoder.getStrength and encoder.getAgc functions not reading the correct registers in the encoder
  • Changed getStepsSinceReset() to return a 32 bit signed integer, instead of a 64 bit signed integer, which is just overkill, and doesn't really play well with arduino
  • Changed some minor things in the assembly code to optimise stuff with regards to the PID mode
  • Fixed a bug in the assembly code causing the StepsSinceReset variable to always decrease regardless of the direction of rotation. This resulted in the number of steps moved since reset, always being negative
  • Fixed a typo in the documentation of the getCurrentDirection() function, to state the correct return values
  • Added several keywords to the library
  • Added Example to demonstrate the use of PID closed loop position control
  • Fixed bug with encoder.setHome() function, where number of revolutions was never reset, resulting in the angle being reset to the number of revolutions times 360 degrees, instead of 0 degrees
  • Implemented Timeout in I2C functions. This ensures that the program will not lock up if access to a non-existing I2C address is attempted.
  • Added overloaded function to pwmD8() and pwmD3(), in order to be able to revert the function of these to pins, back to normal IO mode
  • Added PID functionality to drop-in Feature
  • Added PID functionality to regular movement functions
  • Added support for servo motors
  • Added two new examples
  • Changed setup of Timer1 and Timer2, to allow for PWM generation on D3 and D8. See documentation of "pwmD3()" and "pwmD8()" functions in class "uStepper", for instructions.
  • Changed setup of external interrupts for dropin feature, to be able to compile with arduino IDE 1.6.10.
  • Updated board installation package to version 1.1.0, to be compliant with arduino IDE 1.6.10. Update uStepper board using package manager in Arduino IDE ("Tools->Board->Board Manager", scroll down to the uStepper board and choose update or install).
  • Added the attribute "used" to declarations of interrupt routines. This enables the library to be compiled in Arduino IDE 1.6.10
  • Updated documentation of "uStepper::setup()"
  • Fixed bug where initial deceleration phase (used when changing speed setting or changing direction while motor is moving), would never be entered, causing motor to accelerate
  • Fixed bug with setHome() causing getAngleMoved() to return alternating values (offset by exactly 180 degrees) between resets.
  • Fixed bug in runContinous(), where direction of the motor did not get updated if the function was called with the motor standing still.
  • Fixed bug with getAngleMoved() returning alternating values (offset by exactly 180 degrees) between resets.
  • Added keywords
  • Updated pin connection description for Drop-in example
  • Added Drop-in feature to replace stepsticks with uStepper for error correction
  • Fixed bug in stepper acceleration algorithm, making the motor spin extremely slow at certain accelerations. Also this fix reduced the motor resonance
  • Implemented an IIR filter on the speed measurement, to smooth this out a bit.
  • Added support for speed readout
  • Added support for measuring the shaft position with respect to a zero reference. (absolute within multiple revolutions)
  • Complete rewrite of the stepper algorithm in assembler
  • Changed from fixed point to floating point variables, due to the need for more precision
  • Removed the getSpeed() method, as it didn't work, and therefore it would make more sense to remove it and re-add it when i get the time to fix it
  • Added a few doxygen comments
  • Added a new method (getStepsSinceReset()), which returns all steps performed since reset of the uStepper. positive values corresponds to steps in clockwise direction, while negative values corresponds to steps in counterclockwise direction.
  • Initial release