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

Unfortunately, the software I usually use to draw my schematics does not have most of the components, so I took a picture of them and “manually” wired it in another software. The PCF8574 and the OLED display share the same lines: +5 V, GND, SDA and …

Unfortunately, the software I usually use to draw my schematics does not have most of the components, so I took a picture of them and “manually” wired it in another software. The PCF8574 and the OLED display share the same lines: +5 V, GND, SDA and SCL. The encoder is connected to the PCF8574 in the same way as the numbers of the pins are shown on the back side of the I/O expander (encoder’s pin 1 goes to the expanders pin 1…etc.). Notice, that the I/O pins on the expander start from second jumper pin from the right and the first jumper pin is the ‘int’ pin, which is not used here. Also keep in mind that that encoder have the following pin layout starting from the top left corner and going CCW: 1, 2, GND, 3, 4, 5, 6, GND, 7, 8. The stepper motor breakout board is connected to the A8 (dir), A9 (step) and B12 (enable) pins as well as the +5 V (v) and GND (g). The power input should be connected to the green screw terminal, and the motor is connected to white JST XH2.54 terminal.



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
  }

}
Previous
Previous

Testing a capacitive proximity sensor

Next
Next

Adding stepper motors to the MINIQ BG6300 milling table