Skip to content

Machine Building Preparation

Board

Ref. Norwway bootcamp

alt text
alt text alt text alt text

alt text

FAB25(Moduler Things) BootCamp Leon2024 BootCamp Norway2026
OA2, OA1, OB1, OB2 OA1, OA2, OB1, OB2 Out1, Out2, Out3, Out4
(OA1, OA2, OB1, OB2)
stepperDriver.cpp
#define AIN1_PIN 6 // on D4 #define AIN1_PIN 7 // on D5 #define AIN1_PIN 29 //(IN1) on D3
#define AIN2_PIN 7 // on D5 #define AIN2_PIN 0 // on D6 #define AIN2_PIN 6 //(IN2) on D4
#define BIN1_PIN 28 // on D2 #define BIN1_PIN 2 // on D8 #define BIN1_PIN 4 //(IN3) on D2
#define BIN2_PIN 4 // on D9 #define BIN2_PIN 4 // on D9 #define BIN2_PIN 28 //(IN4) on D9
#define APWM_PIN 27 // on D1 #define APWM_PIN 6 // on D4
#define BPWM_PIN 29 // on D3 #define BPWM_PIN 3 // on D10
simple_stepper.ino
#define PIN_LIMIT 26 // on D0 #define PIN_LIMIT 29 // on D3 #define PIN_LIMIT 26 // on D0
.
└── simple_stepper
    ├── cobs.cpp
    ├── cobs.h
    ├── COBSUSBSerial.cpp
    ├── COBSUSBSerial.h
    ├── fixedPointUtes.cpp
    ├── fixedPointUtes.h
    ├── lutgen.js
    ├── motionStateMachine.cpp
    ├── motionStateMachine.h
    ├── serializationUtes.cpp
    ├── serializationUtes.h
    ├── simple_stepper.ino
    ├── stepperDriver.cpp
    ├── stepperDriver.h
    └── stepperLUT.h

Stepper Motors

Nema 17

alt text

DRV8421A sample code

// Pin definitions for XIAO
#define AIN1_PIN 29  // DRV8421A IN1
#define AIN2_PIN 6   // DRV8421A IN2
#define BIN1_PIN 4   // DRV8421A IN3
#define BIN2_PIN 28  // DRV8421A IN4

// PWM value to limit effective voltage to 2.8V with a 5.0V supply
// Calculation: 255 * (2.8V / 5.0V) approx 143
const int limitPWM = 143; 

// Step speed in microseconds (lower is faster)
int stepDelayUs = 3000; 

// Steps required for a 360-degree rotation (360 / 1.8 = 200 steps)
const int stepsPerRev = 200;

void setup() {
  // Set all control pins as outputs
  pinMode(AIN1_PIN, OUTPUT);
  pinMode(AIN2_PIN, OUTPUT);
  pinMode(BIN1_PIN, OUTPUT);
  pinMode(BIN2_PIN, OUTPUT);
}

/**
 * Sends control signals to the 4-wire input of DRV8421A.
 * Uses analogWrite for HIGH states to limit the motor current.
 */
void setStep(bool a1, bool a2, bool b1, bool b2) {
  // Phase A control
  if (a1) analogWrite(AIN1_PIN, limitPWM); else digitalWrite(AIN1_PIN, LOW);
  if (a2) analogWrite(AIN2_PIN, limitPWM); else digitalWrite(AIN2_PIN, LOW);

  // Phase B control
  if (b1) analogWrite(BIN1_PIN, limitPWM); else digitalWrite(BIN1_PIN, LOW);
  if (b2) analogWrite(BIN2_PIN, limitPWM); else digitalWrite(BIN2_PIN, LOW);

  delayMicroseconds(stepDelayUs);
}

// Perform a 360-degree forward rotation
void moveForward() {
  for (int i = 0; i < stepsPerRev; i++) {
    int phase = i % 4;
    switch(phase) {
      // Full-Step sequence based on data sheet Figure 7-4
      case 0: setStep(true,  false, true,  false); break; // Step 1
      case 1: setStep(false, true,  true,  false); break; // Step 2
      case 2: setStep(false, true,  false, true);  break; // Step 3
      case 3: setStep(true,  false, false, true);  break; // Step 4
    }
  }
}

// Perform a 360-degree backward rotation
void moveBackward() {
  for (int i = 0; i < stepsPerRev; i++) {
    int phase = i % 4;
    switch(phase) {
      // Reverse of the Full-Step sequence
      case 0: setStep(true,  false, false, true);  break; // Step 4 rev
      case 1: setStep(false, true,  false, true);  break; // Step 3 rev
      case 2: setStep(false, true,  true,  false); break; // Step 2 rev
      case 3: setStep(true,  false, true,  false); break; // Step 1 rev
    }
  }
}

void loop() {
  // 1. Rotate 360 degrees forward
  moveForward();
  delay(1000); // Wait for 1 second

  // 2. Rotate 360 degrees backward
  moveBackward();
  delay(1000); // Wait for 1 second
}