I have two Nema 17 stepper motors and one 25kg servo motor. The stepper are connected to a 24V power supply, while the servo is connected to an external 5V power supply. What would be the best way to wire everything using Arduino Uno? As I would like to use the CNC shield v3 for the stepper motor, can I connect the servo (+ external power supply to the CNC shield v3)? If not could I use two separate Arduino?
Yes you can control a servo with the CNC shield, but the external supply + output connects only to the servo. See below.
What pins on the CNC shield are unused? The Image below shows the pin mapping of the Uno to the CNC shield. You can attach the control wire of the servo to any of the unused pins (12, 13, A0-A3,* or unused limit switch or motor pins). Connect servo ground to external servo supply ground and a ground on the CNC shield. Connect the servo power to the external servo supply +.
Here is tested code to demonstrate the stepper and servo operation using a stepper bounce code combined with a servo sweep code.
#include <AccelStepper.h>
#include <Servo.h>
const byte enablePin = 8; // for CNC shield
const byte servoPin = 12; // Spindle enable pin (SpinEn).
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 2, 5);
Servo myservo;
int pos = 0;
void setup()
{
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
myservo.attach(servoPin);
// Change these to suit your stepper if you want
stepper.setMaxSpeed(1000); // may need slower if the stepper won't run
stepper.setAcceleration(500);
stepper.moveTo(10000);
}
void loop()
{
// If at the end of travel go to the other end
if ( stepper.run() == 0)
{
stepper.moveTo(-stepper.currentPosition());
}
static unsigned long timer = 0;
static unsigned long interval = 20;
if (millis() - timer >= interval)
{
timer = millis();
if (pos < 160)
{
myservo.write(pos);
pos++;
interval = 20;
}
else
{
pos = 20;
interval = 1000;
}
}
}