Help on displaying current step of stepper motor

Hello, im currently work in a project that involves on controlling a stepper motor. I got four wired stepper motor, TB6560 driver, and arduino for the control system. I have made it works using the AccelStepper library made by Mike McCauley, it's already moving in a desired position with the input velocity.

However the next thing I need to do is to display the current step position because i need to get the data to be plotted later with the data that i get from my load cell. Is there any possible way to display this current step position of my stepper motor?

Thank you so much for spending ur time reading this, i would really appreciate if u can help me with my problem ;-;

Below are my current codes that i get from TB6560 Stepper Motor Driver with Arduino Tutorial (2 Examples)

// Include the AccelStepper library:
#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
// Set the maximum speed and acceleration:

void loop() {
// Set the target position:
// Run to target position with set speed and acceleration/deceleration:


Post the code, using code tags.

Hello! Thanks for replying! im actually new to the forum so i dont rlly know how things work here. but i've edited my question with my current codes that i used. Is that what you meant?

The AccelStepper library has a function called currentPosition() that returns the current position of the stepper. You can print the value returned by currentPosition().

But the runToPosition() function blocks so you will need to use a different, non blocking, function (like run()) if you want to track the motor as it moves.

Read the forum guidelines.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

hellooo, thank you so much for replying! does currentPosition() report the current step like in every specific timestamp? and anyway thank you for telling me that the code is blocking function i just knew that. And yes, i'll read the forum guidelines. Thank you so much!!

Here is a example to illustrate using currentPosition() and a non-blocking bounce type example. The position is reported every 100ms (1/10 second). I changed the pin numbers to suit my setup so that I can test the code. My setup, Uno, CNC shield, DRV8825 driver and NEMA 17 stepper. So change pin numbers for your setup and mind the serial monitor baud rate (115200).

#include <AccelStepper.h>

const byte enablePin = 8;  // for CNC shield

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 2, 5);

void setup()
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
   // Change these to suit your stepper if you want

void loop()
   // If at the end of travel go to the other end
   if ( == 0)
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
      timer = millis();

My Multi-tasking in Arduino tutorial has a complete non-blocking stepper motor tutorial with user input and sensor control and position output.
Note carefully, prints can block your stepper from running fast so read the section on Giving Important Tasks Extra Time
Also you can read my Arduino Serial I/O for the Real World tutorial for how to get non-blocking Serial output.
Finally my Stepper Speed Control Library project has a stepper motor example with a plot output.

1 Like

Thank you so much for the insight!! I'll try to modified code that i used based on your code

Woah- you give me complete resources! Thank you so muchh ;-;
Anyways, does it work even tho im not using sensor to measure the position? I'll read your tutorial. Thank youuu

You can replace the temperature sensor input with your position sensor and control on that. Usually the stepper position is tracked by counting the pulses. The Accelstepper library does that but you need to set a reference 0

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.