Showing posts with label Stepper Motor Library. Show all posts
Showing posts with label Stepper Motor Library. Show all posts

Wednesday, December 20, 2023

Library for TMC2209 driver - AVR

This library is used to control one or more TMC2209 modules via UART using an AVR microcontroller. Apart from reading and writing the TMC2209 registers, this library can also be used to drive a stepper motor by using the stepperCon library in the background. The stepperCon library provides non blocking functions by using an interrupt, to drive multiple stepper motors with custom acceleration and speed, to keep track of motor positions, 3-axis motor coordination, as well as other useful functions. UART library is also provided that can use up to 2 USART peripherals at the same time: one for interfacing with the driver IC and one for debugging (assuming the microcontroller has two USART peripherals). 

The serial communication has some extra features for checkup such as:

  • reads the interface transmission counter (IFCNT) register after every write to ensure the IC received the correct data.
  • compares CRCs.
  • checks if correct number of bytes has been received.
  • if an error occurs it retries 2 times then sets a communication error flag that the user can check after each function is executed and take the appropriate action if an error occurs. 

Library for TMC2209 driver - AVR
The principle of operation is read-modify-write so there is no need to keep all driver IC registers in memory. The library only uses an 8 bytes union structure called replyDatagram where received and data to send is stored temporary.

If you wish to learn on how to wire the TMC2209 driver, you can find a tutorial here.


Tuesday, October 3, 2023

stepperCon library for controlling stepper motor drivers using timer interrupt on AVR devices

The stepperCon library allows you to control multiple stepper motors at the same time. It can interface with any stepper motor driver that can take inputs such as step and direction. The library also has some extra useful functionalities such as motor coordination using Bresenham line algorithm - useful for controlling 3 axis motors for example, free spin - where the motor keeps spinning, and positioning the motor at a certain degree from 1 to 360 by finding the shortest path.

The maximum speed (step rate) on a 16MHz microcontroller is 25kHz, however the maximum practical speed is 6.25kHz or 8.33kHz depending on the motor, load, supply voltage and micro-step resolution. A higher voltage can yield higher RPM and with 1/32 micro-step resolution the step rate can be 25kHz as oppose to 6.25kHz when using 1/8 micro-step resolution.

Even with multiple motors running at the same time, the speed can be maintained and that is because the speed profile is segmented and calculated while the interrupt routine (ISR) is not executing steps. So the time spent inside the ISR is low since the calculations for acceleration and deceleration are not done there. Only the stepping is done inside the ISR. Having an interrupt to generate the steps is preferred over a function in the main loop where other code could delay the stepping function thus causing motor stuttering.

The stepper library includes the micros library that is used to trigger a timer interrupt every 40us and as a perk you also have a way to keep track of time in your project with a 40us time resolution.


stepperCon library characteristics

  • interface with multiple stepper motor drivers
  • up to 3 coordinated motors for X, Y and Z (can be extended for more)
  • maximum stepping rate speed of 6.25kHz (or 8.33kHz if the motor can have high acceleration). Speeds are given for a 16MHz CPU. With higher CPU clock the stepping rate will also be higher.
  • angular positioning function that finds the shortest path from current angle to target angle
  • perpetual motion
  • timer interrupt driven
  • individual settings for acceleration and deceleration

Saturday, October 28, 2017

Library for A4988 stepper motor driver using timer interrupt

Update 4, October, 2023: I have made a new library for controlling stepper motor drivers that supports multiple motors and has a better acceleration algorithm. It can also coordinate xyz steppers.

This library is designed for AVR ATmega328 microcontroller, but with few adjustments can work with any AVR microcontroller with at least 3-4 Kb of flash program memory.
At the moment only one motor is supported.


  • the interrupt can be triggered by Timer0 or Timer1
  • automatic microstepping mode selection
  • can work with constant and very low speeds
  • accelerated speed mode, with separate acceleration and deceleration settings
  • can work with only one pin of the microcontroller if the rest are hardwired

Nema 17 Bipolar Stepper Motor


Program Memory
Data Memory (SRAM)
All enabled
3552 bytes
40 bytes
Acceleration, no microstepping
3088 bytes
40 bytes
Microstepping, no acceleration
2104 bytes
14 bytes
No microstepping, no acceleration
1724 bytes
12 bytes

Compiled using avr-gcc (WinAVR 20100110) 4.3.3 and -Os optimization level and MATH_LIB = -lm.
If MATH_LIB = -lm is commented out in Make file, the memory with all functions enabled is 6520 bytes program and 304 bytes data memory.So allways uncomment MATH_LIB = -lm.