I'm controlling 4 stepper motors with my raspberry PI, and I need to step them in a control loop cycling at a specified rate as low as once every 40 microseconds. The motors won't always be stepped every loop; instead, one will be stepped every 3 loops for example while another will be stepped every two. This I'll be able to code in myself. I'm just stuck with getting a timer interrupt on the Raspberry Pi that calls a function every 40 or more microseconds.

I've seen from research on the internet that Linux wouldn't be able to do this well because of other interrupts going on in the OS. So is there a bare metal way I could do this? I want to preferably program the actual stepper controller in C++.

I know this is doable with an Arduino because of the hardware timers, but unless it's totally not possible on the RPI I want to stick to using the RPI.

EDIT: Due to some confusion in comments, I'm gonna clear up what exactly I need. I need about 48 different possible frequencies for 4 different pins. The frequency individual pins give out will change from time to time as well. I will use a serial pin for communication to change the frequencies of pins.

1 Answer 1


I think the best chance you will have of achieving this on the Pi is my pigpio library or possibly RPIO.GPIO (not RPi.GPIO).

Both allow for the construction of arbitrary waveforms using the DMA engine.

With pigpio you would use a functionality called waves (C, Python).

Waves are an arbitrary sequence of GPIO level changes. They can be repeated indefinitely or a set number of times using wave chains.

For an example (not stepper) see http://abyz.me.uk/rpi/pigpio/examples.html#Python_wave_PWM_py.

  • I've tried both libraries already. Manually toggling the pins with write is not the problem, because what I need is the cycle timing to be correct. I've already tried the wave functionality from your pigpio library but it doesn't work for what I need. Using the waves I'd need to make a wave form for every possible frequency and pin combination (I think)
    – catsock
    May 19, 2016 at 17:38
  • And I'd need to switch frequencies on desired pins within a hair's width of a second after I want it to. From what I've read on the waveforms on pigpio, it's meant for one single frequency on a preordained pin, and not for switching all the time.
    – catsock
    May 19, 2016 at 17:43
  • 1
    @NeonWizard Remember you are the only person who actually knows the details of what you want to achieve. The rest of us have to make a best guess using the details in the question. If your control loop has to make changes to a GPIO output within 40 microseconds of a GPIO input change then Linux on the Pi is not suitable. Waveforms can generate multiple frequencies on different GPIO. abyz.co.uk/videos/multi-stepper.webm shows 5 steppers (4 GPIO each, one per phase) and two servos being driven.
    – joan
    May 19, 2016 at 17:55
  • So then what is the solution? (I also made an edit to my original post)
    – catsock
    May 19, 2016 at 18:26
  • Okay, it boils down to how long can there be between a serial message arriving at the Pi (say all the bits have been sent over the physical link) and the new output being applied at the stepper GPIO?
    – joan
    May 19, 2016 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.