Useful gadgets for stepper motor projects
In this video I show you two different kind of devices which can be useful in project where you have stepper motors. More specifically, where you use either the DRV8825 or the A4988 stepper motor drivers and you want to control the stepper motors manually with a good precision.
Useful resources:
Schematics
STM32/Arduino source code
#define ACE_ADDR 0x20 // PCF8574 0x20 - this is the default address #include <ACE128.h> // Include the ACE128.h from the library folder #include <ACE128map12345678.h> // mapping for pin order 1234-5678 ACE128 AbsEnc(ACE_ADDR, (uint8_t*)encoderMap_12345678); // create AbsEnc instance //The encoder uses i2C uint8_t previousPosition = 255; //So, the next step will start from 0 int16_t relativePosition; //stores the relative displacement/position int16_t absolutePosition; //stores the absolute displacement/position //--Display related---------------------------------------------------------------- #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> //display library #include <Adafruit_SSD1306_STM32.h> //I guess, there are faster libraries available #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); //The display uses i2C //--Stepper motor related---------------------------------------------------------- #include <AccelStepper.h> AccelStepper stepper(1, PA9, PA8);// pulses/steps 9; Direction 8 const int stepperEnablePin = PB12; //enable/disable pin for the stepper motor driver //remember that for Arduino, you don't need the "PA" and "PB" prefixes. Just use 1,2,3...etc. void setup() { //Absolute encoder initialization------------------------------------- AbsEnc.begin(); // initialize the encoder //display initialization--------------------------------------------------------- Wire.begin(); Wire.setClock(400000L); //This speeds up the i2c, so the display will be faster display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) display.clearDisplay(); display.setTextSize(3); //define font size display.setTextColor(WHITE); //define "color" //Stepper setup--------------------------------------------------------- stepper.setSpeed(1000); //SPEED = Steps / second stepper.setMaxSpeed(1000); //SPEED = Steps / second stepper.setAcceleration(5000); //ACCELERATION = Steps /(second)^2 pinMode(stepperEnablePin, OUTPUT); //enable/disable pin is defined as an output digitalWrite(stepperEnablePin, LOW); //enable motor current //disabling the current can prevent the driver and the motor running hot //on the other hand, it can lead to inaccuracies because the motor is not held at place when it is not under power } void loop() { absolutePosition = AbsEnc.upos(); // read the absolute position of the absolute encoder relativePosition = AbsEnc.mpos(); // relative position (multiturn) stepper.moveTo(relativePosition); if (absolutePosition != previousPosition) //if the encoder was moved { //display display.clearDisplay(); //clear the previously printed text display.setCursor(0,0); //move the cursor in the upper left corner display.println(relativePosition); //print the variable - update the position on the display display.display(); //update contents of the display previousPosition = absolutePosition; //update the previous position to the recent position } while(stepper.distanceToGo() != 0) //This blocks the rest of the code! { stepper.runSpeedToPosition(); //Runs to the target position defined by the moveTo() function } }