Arduino Mega 2560 Stepper Motor Control Code

Stepper motors are the most precise motors available. You can order them to move exactly 1.8/16 = 0.1125 degrees/step in each direction. Although this can depend on the motortype. These orders are given out in the form of PWM signals. These signals are square signal. You can read more about PWM on the internet. Everytime the signal jumps to HIGH state the motor turns a step. Our stepper motor devicehandler can control multiple stepper motors simultaneously. It is also capable to handle two endstops for each motor, which are capable to keep the motors moving between a minimum and maximum range.

arduino mega stepper motor
Figure 1 - Arudino Mega Stepper Motor

Required hardware

  • Arduino Mega 2560
  • RAMPS panel
  • Stepper motor(s)
  • Stepper Motor Driver(s)
  • Micro Switche(s)
  • Resistor(s) 1kΩ
  • Power Supply

Source code to install on controller


Before you upload this code to your Arduino, please format the EEPROM...
#include <OzIDManager.h>
#include <OzMotorMultiController.h>

OzIDManager* manager;
OzMotorMultiController* motorController;

void setup() {
  Serial.begin(115200);

  manager = new OzIDManager;
  manager->_sendACK = true;
  manager->_checksum = true;

  OzCommunication::setIDManager(manager);
  
  motorController = new OzMotorMultiController(PinPreset::RAMPS_v14);
  
  int x=1;
  manager->sendLinkSetup();
  manager->PrintWelcomeLine(motorController, x++, "MyStepperMotors");
}

void loop() {
  OzCommunication::communicate();
  motorController->ownLoop();
}

More information