Teensy 4.0 two pins at the same time


Active member
Hey everyone,
I’m trying to drive two steppers at the same time in a CoreXY system using a Teensy 4.0. As you might know the steppers need to move at the same time to go straight and I was wondering if using digitalWriteFast() will be enough. If not how can I do port manipulation with 4.0. I’m sorry if there’s an obvious way for this but I have not done it before and don’t know where to start :).
Last edited:
digitalWriteFast() should be plenty fast enough. Even the slower digitalWrite() is probably fine.

To keep a little perspective, consider the motor windings are basically inductors. The force applied to the rotor depends on the current. The current in an inductor is 1/L times the integral of the applied voltage. When you apply a fixed voltage, that means the current is a linear ramp with a slope of the voltage divided by the inductance. The key concept of this math is the current (and mechanical force) in the motor increases as a linear ramp.

The delay of only nanoseconds between the 2 pins changing is tiny compared to the time that linear ramp takes to get the current up to the steady state.

Of course, you can drive stepper motors in more exotic ways, where a much higher voltage is initially applied. Some very high performance drivers apply 50 volts or more to get the current up quickly, and then back off to a much lower voltage to maintain the desired current. If you're using a circuit like that with a pretty high voltage, I still would be skeptical that several nanoseconds would matter, but it would be worth investigating carefully.

For ordinary fixed voltage driving on the coils, the current just can't change on a fast time scale anywhere near the time digitalWriteFast() takes.
I think T4 GPIO pins are on a 150 MHz clock, meaning sequential writes might be 6.67 nanoseconds apart.
Choosing a random NEMA17 stepper, I find coil R,L specs at R=1.1 ohm and L=2.6 mH. The time constant of that motor coil is L/R = 2.4 msec, so if a drive signal is delayed by one T4 peripheral bus clock period (6.67 ns), that is three millionths of one coil time constant. Therefore, to make that delay significant, the inductance and resistance in your two motors must to be stable and matched at the 3 parts per million level, and I am pretty confident that is not the case.
Just for reference, copper resistance goes up with temperature, and the resistance tempco of copper near room temp. is +0.393 percent per degree C. So if your motors have a temperature difference of *one* degree C, the relative time response has already changed about 1400 times more than the effect of one clock tick on the T4 GPIO bus.