Ok, this is what I am trying to do. I have got an ESP32S that I want to operate 6 Stepper Motors. First of all, PLEASE do not ask me why I am using stepper motors, not servos. Load is the main reason. That being said, I also have 16 inputs for them from position switch to hall sensor to home them to a potentiometer the will control 3 of the steppers. There are too many inputs and outputs so I want to add one, maybe two MCP23017 I/O Expansions to the system. I looked everywhere and only found one thing that was posted around 5 years ago on how the set up the 2 pins to the driver in Arduino IDE. I have it working just fine right now with one drive without the I/O Expansion card.
Can anyone help with how to code this? I posted the code I am using right now without the expansion. By the way, this is going to run 6 Nema 23 with 6 DM542T drivers.
/* Motor Homing code using AccelStepper and the Serial Monitor
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
This is to opperate the steering and the shift from drive to nutrral and reverse of 3 motors in a full size houseboat*/
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x27, 20, 4);
const byte FRSTEP_PIN_PORT = 2;
const byte FRDIR_PIN_PORT = 4;
const byte STSTEP_PIN_PORT = 5;
const byte STDIR_PIN_PORT = 12;
const byte FRSTEP_PIN_CENTER = 13;
const byte FRDIR_PIN_CENTER = 14;
const byte STSTEP_PIN_CENTER = 15;
const byte STDIR_PIN_CENTER = 16;
const byte FRSTEP_PIN_STARBOARD = 17;
const byte FRDIR_PIN_STARBOARD = 18;
const byte STSTEP_PIN_STARBOARD = 19;
const byte STDIR_PIN_STARBOARD = 20;
// AccelStepper Setup
AccelStepper stepper1(1, FRSTEP_PIN_PORT, FRDIR_PIN_PORT); // 1 = Easy Driver interface
AccelStepper stepper2(1, STSTEP_PIN_PORT, STDIR_PIN_PORT); // 1 = Easy Driver interface
AccelStepper stepper3(1, FRSTEP_PIN_CENTER, FRDIR_PIN_CENTER); // 1 = Easy Driver interface
AccelStepper stepper4(1, STSTEP_PIN_CENTER, STDIR_PIN_CENTER); // 1 = Easy Driver interface
AccelStepper stepper5(1, FRSTEP_PIN_STARBOARD, FRDIR_PIN_STARBOARD); // 1 = Easy Driver interface
AccelStepper stepper6(1, STSTEP_PIN_STARBOARD, STDIR_PIN_STARBOARD); // 1 = Easy Driver interface
// Define the Pins used. The ones that are set at pin 26 will be changed once I am able to use
// the MCP23017
#define PFRhome_switch 26 // connected to Home Switch (MicroSwitch)
#define PShome_switch 26 // connected to Home Switch (MicroSwitch)
#define CFRhome_switch 26 // connected to Home Switch (MicroSwitch)
#define CShome_switch 26 // connected to Home Switch (MicroSwitch)
#define SFRhome_switch 26 // connected to Home Switch (MicroSwitch)
#define SShome_switch 26 // connected to Home Switch (MicroSwitch)
#define PortForward 39
#define PortReverse 35
#define CenterForward 36
#define CenterReverse 34
#define StarboardForward 32
#define StarboardReverse 27
// Stepper Travel Variables
const long forwardPosition = 0;
const long neutralPosition = 200;
const long reversePosition = 400;
const long initial_homing = -4000; // Used to Home Stepper at startup
int ForwardPortSwitch;
int ReversePortSwitch;
int ForwardCenterSwitch;
int ReverseCenterSwitch;
int ForwardStarboardSwitch;
int ReverseStarboardSwitch;
void setup() {
// Serial.begin(115200); // Start the Serial monitor with speed of 9600 Bauds
// Initialise the LCD 1
lcd1.begin();
lcd1.clear();
lcd1.noCursor();
lcd1.backlight();
lcd1.setCursor(0, 0);
// Hall Sensors for homing
pinMode(PFRhome_switch, INPUT);
pinMode(PShome_switch, INPUT);
pinMode(CFRhome_switch, INPUT);
pinMode(CShome_switch, INPUT);
pinMode(SFRhome_switch, INPUT);
pinMode(SShome_switch, INPUT);
// Switches to set each motor into Forward, Neutral, or Revers.
pinMode(PortForward, INPUT);
pinMode(PortReverse, INPUT);
pinMode(CenterForward, INPUT);
pinMode(CenterReverse, INPUT);
pinMode(StarboardForward, INPUT);
pinMode(StarboardReverse, INPUT);
delay(50); // Wait for EasyDriver wake up
// Start Homing procedure of Stepper Motor at startup
// This will be showing in a display when the power is first turned on
bool ready = false;
while ( !ready ) {
beginning:
ready = true;
ForwardPortSwitch = digitalRead(PortForward);
ReversePortSwitch = digitalRead(PortReverse);
ForwardCenterSwitch = digitalRead(CenterForward);
ReverseCenterSwitch = digitalRead(CenterReverse);
ForwardStarboardSwitch = digitalRead(StarboardForward);
ReverseStarboardSwitch = digitalRead(StarboardReverse);
lcd1.setCursor(0, 0);
lcd1.print(" Drive Status ");
if (ForwardPortSwitch == HIGH) {
lcd1.setCursor(0, 1);
lcd1.print(" Port in Forward ");
// Serial.println("Port in Forward");
ready = false;
}
if (ReversePortSwitch == HIGH) {
lcd1.setCursor(0, 1);
lcd1.print(" Port in Reverse ");
// Serial.println("Port in Reverse");
ready = false;
}
if (ReversePortSwitch == LOW && ForwardPortSwitch == LOW) {
lcd1.setCursor(0, 1);
lcd1.print(" Port in Neutral ");
// Serial.println("Port in Reverse");
}
//-------------------------------------------------------------------------------------------
if (ForwardCenterSwitch == HIGH) {
lcd1.setCursor(0, 2);
lcd1.print(" Center in Forward ");
// Serial.println("Center in Forward");
ready = false;
}
if (ReverseCenterSwitch == HIGH) {
lcd1.setCursor(0, 2);
lcd1.print(" Center in Reverse ");
// Serial.println("Center in Reverse");
ready = false;
}
if (ReverseCenterSwitch == LOW && ForwardCenterSwitch == LOW) {
lcd1.setCursor(0, 2);
lcd1.print(" Center in Neutral ");
// Serial.println("Center in Reverse");
}
//---------------------------------------------------------------------------------------------------------------
if (ForwardStarboardSwitch == HIGH) {
lcd1.setCursor(0, 3);
lcd1.print("Starboard in Forward");
// Serial.println("Starboard in Forward");
ready = false;
}
if (ReverseStarboardSwitch == HIGH) {
lcd1.setCursor(0, 3);
lcd1.print("Starboard in Reverse");
// Serial.println("Starboard in Reverse");
ready = false;
}
if (ReverseStarboardSwitch == LOW && ForwardStarboardSwitch == LOW) {
lcd1.setCursor(0, 3);
lcd1.print("Starboard in Reverse");
// Serial.println("Starboard in Reverse");
}
//--------------------------------------------------------------------------------------------------------------------
if ( !ready == false ) {
delay(4000);
lcd1.setCursor(0, 3);
lcd1.print(" ALL drives MUST ");
delay(3000);
lcd1.setCursor(0, 3);
lcd1.print(" be in Neutral ");
delay(3000);
lcd1.setCursor(0, 3);
lcd1.print(" You have 5 seconds ");
// Serial.println("ALL drives MUST be in Neutral");
// Serial.println("You have 5 seconds from NOW!");
delay(5000);
goto beginning;
}
}
//-------------------------------------------------------------------------------------------------------------------
// Once all of the switches are in neutral, is homes the stepper motors.
// Make the Stepper move CCW until the switch is activated
// Set Max Speed and Acceleration of each Steppers at startup for homing
//--------------------------------------------------------------------------------------------------------------------
lcd1.setCursor(0, 0);
lcd1.print("Homing Bucket ");
stepper1.setMaxSpeed(200.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper1.setAcceleration(2000.0); // Set Acceleration of Stepper
lcd1.setCursor(0, 1);
lcd1.print("Port Homing ");
// Serial.println("Port Drive Homing");
stepper1.moveTo(initial_homing); // Set the position to move to
while (digitalRead(PFRhome_switch) == HIGH) {
stepper1.run(); // Start moving the stepper
delay(5);
}
stepper1.setCurrentPosition(0); // Set the current position as zero for now
// Serial.println("Port Drive Homing Completed");
stepper1.setMaxSpeed(200.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper1.setAcceleration(2000.0); // Set Acceleration of Stepper
stepper1.runToNewPosition(neutralPosition);
lcd1.setCursor(0, 1);
lcd1.print("Port Complete ");
//----------------------------------------------------------------------------------------------------------
stepper2.setMaxSpeed(200.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper2.setAcceleration(2000.0); // Set Acceleration of Stepper
lcd1.setCursor(0, 2);
lcd1.print("Center Homing ");
// Serial.println("Center Drive Homing");
stepper2.moveTo(initial_homing); // Set the position to move to
while (digitalRead(CFRhome_switch) == HIGH) {
stepper2.run(); // Start moving the stepper
delay(5);
}
stepper2.setCurrentPosition(0); // Set the current position as zero for now
stepper2.setMaxSpeed(200.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper2.setAcceleration(2000.0); // Set Acceleration of Stepper
stepper2.runToNewPosition(neutralPosition);
lcd1.setCursor(0, 2);
lcd1.print("Center Completed ");
// Serial.println("Center Drive Homing Completed");
//----------------------------------------------------------------------------------------------------------
stepper3.setMaxSpeed(200.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper3.setAcceleration(2000.0); // Set Acceleration of Stepper
lcd1.setCursor(0, 3);
lcd1.print("Starboard Homing ");
// Serial.println("Starting Port Initialization");
stepper3.moveTo(initial_homing); // Set the position to move to
while (digitalRead(SFRhome_switch) == HIGH) {
stepper3.run(); // Start moving the stepper
delay(5);
}
stepper3.setCurrentPosition(0); // Set the current position as zero for now
stepper3.setMaxSpeed(200.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper3.setAcceleration(2000.0); // Set Acceleration of Stepper
stepper3.runToNewPosition(neutralPosition);
lcd1.setCursor(0, 3);
lcd1.print("Starboard Complete ");
// Serial.println("Homing Completed");
//----------------------------------------------------------------------------------------------------------
lcd1.setCursor(0, 0);
lcd1.print("Homing Steering ");
stepper4.setMaxSpeed(200.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper4.setAcceleration(2000.0); // Set Acceleration of Stepper
lcd1.setCursor(0, 1);
lcd1.print("Port Homing ");
// Serial.println("Starting Port Initialization");
stepper4.moveTo(initial_homing); // Set the position to move to
while (digitalRead(PShome_switch) == HIGH) {
stepper4.run(); // Start moving the stepper
delay(5);
}
stepper4.setCurrentPosition(0); // Set the current position as zero for now
stepper4.setMaxSpeed(200.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper4.setAcceleration(2000.0); // Set Acceleration of Stepper
stepper4.runToNewPosition(neutralPosition);
lcd1.setCursor(0, 1);
lcd1.print("Port Completed ");
// Serial.println("Homing Completed");
//----------------------------------------------------------------------------------------------------------
stepper5.setMaxSpeed(200.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper5.setAcceleration(2000.0); // Set Acceleration of Stepper
lcd1.setCursor(0, 2);
lcd1.print("Center Homing ");
// Serial.println("Starting Port Initialization");
stepper5.moveTo(initial_homing); // Set the position to move to
while (digitalRead(CShome_switch) == HIGH) {
stepper5.run(); // Start moving the stepper
delay(5);
}
stepper5.setCurrentPosition(0); // Set the current position as zero for now
lcd1.setCursor(0, 2);
lcd1.print("Center Completed ");
// Serial.println("Homing Completed");
stepper5.setMaxSpeed(200.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper5.setAcceleration(2000.0); // Set Acceleration of Stepper
stepper5.runToNewPosition(neutralPosition);
//----------------------------------------------------------------------------------------------------------
stepper6.setMaxSpeed(200.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper6.setAcceleration(2000.0); // Set Acceleration of Stepper
lcd1.setCursor(0, 3);
lcd1.print("Starboard Homing ");
// Serial.println("Starting Port Initialization");
stepper6.moveTo(initial_homing); // Set the position to move to
while (digitalRead(SShome_switch) == HIGH) {
stepper6.run(); // Start moving the stepper
delay(5);
}
stepper6.setCurrentPosition(0); // Set the current position as zero for now
stepper6.setMaxSpeed(200.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper6.setAcceleration(2000.0); // Set Acceleration of Stepper
stepper6.runToNewPosition(neutralPosition);
lcd1.setCursor(0, 3);
lcd1.print("Starboard Completed ");
// Serial.println("Homing Completed");
//----------------------------------------------------------------------------------------------------------
// Print out Instructions on the Serial Monitor at Start
//Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
}
void loop() {
// Here it read two of the three switch positions to see what position the stepper should be in and puts it there.
ForwardPortSwitch = digitalRead(PortForward);
ReversePortSwitch = digitalRead(PortReverse);
ForwardCenterSwitch = digitalRead(CenterForward);
ReverseCenterSwitch = digitalRead(CenterReverse);
ForwardStarboardSwitch = digitalRead(StarboardForward);
ReverseStarboardSwitch = digitalRead(StarboardReverse);
lcd1.setCursor(0, 0);
lcd1.print(" Drive Status ");
//
if (ForwardPortSwitch == HIGH) {
lcd1.setCursor(0, 1);
lcd1.print("Port in Forward ");
// Serial.println("Forward");
stepper1.runToNewPosition(forwardPosition);
delay(2000);
}
else if (ForwardPortSwitch == LOW && ReversePortSwitch == LOW)
{
lcd1.setCursor(0, 1);
lcd1.print("Port in Neutral ");
//Serial.println("Nutral");
stepper1.runToNewPosition(neutralPosition);
delay(2000);
}
else if (ReversePortSwitch == HIGH) {
lcd1.setCursor(0, 1);
lcd1.print("Port in Reverse ");
// Serial.println("Reverse");
stepper1.runToNewPosition(reversePosition);
delay(2000);
}
if (ForwardCenterSwitch == HIGH) {
lcd1.setCursor(0, 2);
lcd1.print("Center in Forward ");
// Serial.println("Forward");
stepper1.runToNewPosition(forwardPosition);
delay(2000);
}
else if (ForwardCenterSwitch == LOW && ReversePortSwitch == LOW)
{
lcd1.setCursor(0, 2);
lcd1.print("Center in Neutral ");
// Serial.println("Neutral");
stepper1.runToNewPosition(neutralPosition);
delay(2000);
}
else if (ReverseCenterSwitch == HIGH) {
lcd1.setCursor(0, 2);
lcd1.print("Center in Reverse ");
// Serial.println("Reverse");
stepper1.runToNewPosition(reversePosition);
delay(2000);
}
if (ForwardStarboardSwitch == HIGH) {
lcd1.setCursor(0, 3);
lcd1.print("Starboard in Forward");
// Serial.println("Forward");
stepper1.runToNewPosition(forwardPosition);
delay(2000);
}
else if (ForwardStarboardSwitch == LOW && ReversePortSwitch == LOW)
{
lcd1.setCursor(0, 3);
lcd1.print("Starboard in Neutral");
// Serial.println("Neutral");
stepper1.runToNewPosition(neutralPosition);
delay(2000);
}
else if (ReverseStarboardSwitch == HIGH) {
lcd1.setCursor(0, 3);
lcd1.print("Starboard in Reverse");
// Serial.println("Reverse");
stepper1.runToNewPosition(reversePosition);
delay(2000);
}
}