using I2C IO expander ( MCP23008 ) to control stepper (with L293D)

Hi Everyone
I’m new and just starting out on this working on a solution for a project.
I think I have the hardware sorted seperately, but trying to figure out how/if I can make it work the way I would like.
Project is 5 small stepper motors (x27.168) driven by a teensy3.2 with I2C network

I have a pretty good grasp on using the L293D to drive the steppers directly from the teensy. ( using the stepper library )

But would like to use IO expander MCP23008 on I2C to control the L293D to drive the steppers instead of directly from the teensy freeing up more of its PINs ( mainly the Analog pins and some others I am using for inputs. )

I can get the MCP23008S to be recognized with their different addresses with the address pins (0x20, 0x22, 0x23, 0x25, 0x26 is where I have them set) confirmed with I2c scanner code*

Just cant seem to figure out how to address the pins on the IO expander in the stepper control code used in the stepper library:

#include <Stepper.h>

// Where the Xs are is where I need to figure out how to call to the pins on the different IO expanders

Stepper stepA(600, X, X, X, X);

// Just not sure how to call to pins 0-4 on Mcp23008 with the address of 0x20

void setup() {
etc etc…

Or of course maybe Im barking up the wrong tree and running the steppers in this was isn’t possible without a ton of trouble?

Thanks for any help!! :stuck_out_tongue:

Did you ever get this working? I am planning an almost identical project (MCP23008 to control 28BYJ-48 stepper) and would love to know of any potential problems before I start?

The Arduino Stepper library expects the stepper motor coil signals to be pins on the actual microcontroller. Looking at the source code for the stepper library, it looks reasonably straight forward to create your own library to drive the stepper via an MCP23008.

However, it may be more trouble than it's worth to try to modify the stepper library for your specific scenario as the library handles various types of stepper motor.

My stepper motor knowledge is a bit rusty but from memory, the key is to energise the coils in the correct sequence to cause the motor to rotate. Reversing the sequence causes the motor to go the opposite way.

From looking around the web, I think that your stepper motor is a 2-pin unipolar stepper motor. Have a look at this page for a schematic of how to wire it up.

It looks like it needs 2 control wires, so in theory you could control 4 steppers with one MCP23008.

Looking at the source code for stepper.cpp, it says:

 * The sequence of controls signals for 2 control wires is as follows
 * (columns C1 and C2 from above):
 * Step C0 C1
 *    1  0  1
 *    2  1  1
 *    3  1  0
 *    4  0  0

In order to drive the stepper(s) from your MCP23008, you just need to replicate the bit pattern for C0 and C1 using 2 pins of the MCP23008.

Writing the value 2 to the MCP23008 is the equivalent of step #1 above (if Bit 0 = C0, and Bit 1 = C1).

So if you were to write a 2 followed by a 3 followed by a 1 followed by a 0, then you would have gone once round the sequence. Writing the sequence backwards sends the stepper the other way.

As the MCP23008 has 8 I/O pins, you could control 4 motors using one chip.

I just had a look and AdaFruit have a library for the MCP23008 here. It may be as simple as installing the AdaFruit MCP23008 library, making a copy of the stepper library in your sketch folder and getting the stepper library to include the AdaFruit library.

You can then modify all the calls to pinMode() and digitalWrite() in the stepper library to call the equivalent functions in the AdaFruit library instead.

That may be a lot less work than trying to roll your own library.

However, it won't be as efficient as rolling your own library as each coil change will result in an I2C message being transmitted to the MCP23008. At least with your own library, you could collate all the coil changes into 1 write to the MCP23008.

Hope some of that helps you out.