Created
January 30, 2026 21:03
-
-
Save InputBlackBoxOutput/7737612b0a90ef6c0aed5af9cd6ae8ed to your computer and use it in GitHub Desktop.
Arduino sketch to test all stepper motors connected to a Arduino Uno CNC shield
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Defines pins numbers | |
| #define NUM_AXIS 3 | |
| #define ENABLE 8 | |
| #define STEP_X 2 | |
| #define DIRECTION_X 5 | |
| #define STEP_Y 3 | |
| #define DIRECTION_Y 6 | |
| #define STEP_Z 4 | |
| #define DIRECTION_Z 7 | |
| int stepPin = 0; | |
| int directionPin = 0; | |
| void moveStepperMotor(char axis, char direction, int steps) { | |
| switch (axis) { | |
| case 'X': | |
| { | |
| stepPin = STEP_X; | |
| directionPin = DIRECTION_X; | |
| break; | |
| } | |
| case 'Y': | |
| { | |
| stepPin = STEP_Y; | |
| directionPin = DIRECTION_Y; | |
| break; | |
| } | |
| case 'Z': | |
| { | |
| stepPin = STEP_Z; | |
| directionPin = DIRECTION_Z; | |
| break; | |
| } | |
| } | |
| // Set direction GPIO | |
| if (direction == '+') { | |
| digitalWrite(directionPin, HIGH); | |
| } else { | |
| digitalWrite(directionPin, LOW); | |
| } | |
| // Generate signal for step GPIO | |
| for (int x = 0; x < steps; x++) { | |
| digitalWrite(stepPin, HIGH); | |
| delayMicroseconds(200); | |
| digitalWrite(stepPin, LOW); | |
| delayMicroseconds(200); | |
| } | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| // Step step and direction GPIO conneted to stepper motor drivers | |
| pinMode(DIRECTION_X, OUTPUT); | |
| pinMode(DIRECTION_Y, OUTPUT); | |
| pinMode(DIRECTION_Z, OUTPUT); | |
| pinMode(STEP_X, OUTPUT); | |
| pinMode(STEP_Y, OUTPUT); | |
| pinMode(STEP_Z, OUTPUT); | |
| // Enable all stepper drivers | |
| pinMode(ENABLE, OUTPUT); | |
| digitalWrite(ENABLE, LOW); | |
| } | |
| void loop() { | |
| moveStepperMotor('X', '+', 4000); | |
| moveStepperMotor('X', '-', 4000); | |
| moveStepperMotor('Y', '+', 4000); | |
| moveStepperMotor('Y', '-', 4000); | |
| moveStepperMotor('Z', '+', 4000); | |
| moveStepperMotor('Z', '-', 4000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment