DIY 3D Tech

The On-Line Magazine for 3D Printing & CNC

Arduino Code

Below is the first draft of code for the 28BYJ-48 stepper driven gimbal mount. Note this does use the Accel library to operate in a no blocking manor.  More updates will be coming with time, however wanted some working code posted.

#include <AccelStepper.h>
#define HALFSTEP 8
// 28BYJ-48 Dual Stepper Driver Software
// www.DIY3DTech.com

// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

#define motorPin10  10    // IN1 on the ULN2003 driver 2
#define motorPin11  11    // IN2 on the ULN2003 driver 2
#define motorPin12  12    // IN3 on the ULN2003 driver 2
#define motorPin13  13    // IN4 on the ULN2003 driver 2

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin10, motorPin12, motorPin11, motorPin13);

void setup() {
  // Set up Azm Stepper
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(100.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(400);
  
  // Set up Alt Stepper
  stepper2.setMaxSpeed(1000.0);
  stepper2.setAcceleration(100.0);
  stepper2.setSpeed(200);
  stepper2.moveTo(400);

}//--(end setup )---

void loop() {

  //Change direction when the stepper reaches the target position
  
    if (stepper2.distanceToGo() == 0) {
    stepper2.moveTo(-stepper2.currentPosition());
    if (stepper1.distanceToGo() == 0) {
      stepper1.moveTo(-stepper1.currentPosition());
    }
  }

  stepper1.run();
  stepper2.run();
}