Stepper motor is vibrating due to reading output current using Modbus or serial read using micros or multithreading!

While doing so, I use the Accellstepper to pulse the stepper motor and the Modbus protocol to read the VFD's output current.
Stepper motor is vibrating due to reading output current using modbus or serial read.
I also attempted to use micros for multithreading, but the output remained the same.
Please assist in writing flawless code.
Thank you for reply!
All type of timer library try but output put remain same.

#include <AccelStepper.h>
#include <SoftwareSerial.h>
#include <FlexiTimer2.h>
#include <ModbusMaster.h>
#include "TeensyThreads.h"
#include <Arduino.h>
#include <TimerOne.h>
#include <Bounce.h>
#define RS485Serial Serial1

ModbusMaster node;

#define z_Step 2
#define z_Dir 3
#define z_AXIS_Speed 50000  // number per 1mm
#define z_AXIS_Accelration 50000
#define z_PostionMultiplier 10000  // number per 1mm
#define zstepPermm 1 //divide by 1000

#define SSerialRX        0  //Serial Receive pin rx
#define SSerialTX        1  //Serial Transmit pin tx

//Z axis parameter
bool bool_zReach = 0;
int z_CurrentPos = 0;
bool autoLoop = false;
bool autoLoop1 = false;

bool en = false;
bool bool_Emergency = 0;
unsigned long currentTime_Auto;
unsigned long previousMillis_Auto0;
unsigned long modTime;
unsigned long modMicros;
unsigned long modMicros0;

SoftwareSerial dis_Serial(17, 18);
//SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
AccelStepper z_Axis(1, z_Step, z_Dir);
//volatile int blinkcode = 1;
volatile int data[1];
volatile static uint32_t i;
volatile uint8_t j, result;
elapsedMillis sincePrint;
elapsedMicros sincePrint1;

void blinkthread() {
  //  static uint32_t i;
  //  uint8_t j, result;
  //  uint16_t data[1];
  if (sincePrint > 1000) {      // "sincePrint" auto-increases
    sincePrint = 0;
    result = node.readHoldingRegisters(0x1019, 1);
    if (result == node.ku8MBSuccess)
      data[0] = node.getResponseBuffer(0);


void setup() {
  node.begin(1, RS485Serial);

void loop() {
  float value = 400;
  en = true;
  if (Serial.available() > 0) {
    char cmd =;
    if (cmd == 'M' and en == true) {
      autoLoop = true;
      en = false;

  while (autoLoop == true) {
    if (z_Axis.currentPosition() == 1000000)
      a = true;
      autoLoop = false;
      Serial.println("X Completed");
blinkthread() is busy-waiting during the readHoldingRegisters() call, so that doesn't get called. I suspect that modbus library is blocking rather than non-blocking.
Have a look - I've not used a modbus library and the only stepper libraries I know aren't interrupt driven it seems, but someone will have written one somewhere I reckon.
When googling for "arduino library modbus non-blocking", found this library:
Perhaps that lib fits your need.

error while compiling
Arduino: 1.8.13 (Windows 10), TD: 1.58, Board: "Teensy 4.1, Serial, 600 MHz, Faster, US English"

WARNING: library eModbus-master claims to run on esp32, FreeRTOS architecture(s) and may be incompatible with your current board which runs on avr architecture(s).

In file included from C:\Users\DIVINE\Documents\Arduino\libraries\eModbus-master\src/ModbusClientRTU.h:8,

from F:\DIVINE_INTELLECT_RAVIBHAI\modbus\nonblocking_modbus\nonblocking_modbus.ino:2:

C:\Users\DIVINE\Documents\Arduino\libraries\eModbus-master\src/options.h:51:2: error: #error Define target in options.h

51 | #error Define target in options.h

| ^~~~~

Error compiling for board Teensy 4.1.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
It looks like this library is specifically designed for ESP32 only [with the exception of ESP8266]. Too bad.
I think you need to research further. Personally I don't have experience with Modbus so I can't really help you on this.
Ran into this library: It is supposed to be non-blocking but I did not look into the details.

Last edited:
Looking back at the original program, blinkthread() is not a thread. It's just a function being called from loop(), so of course it's blocking the rest of loop(). Maybe if you make blinkthread() a thread, TeensyThreads will provide the necessary non-blocking behavior on blinkthread(), and that will solve your problem. One thing you will have to do differently, though, is you can't call Serial.println() from two threads. Instead, after you read the VFD current in blinkthread(), assign its value to a global volatile variable, and print that variable from loop() after the call to