Help on stepper motor and load cell control

Hello everyone, i would like to ask your help if u dont mind please

So im working in a project involving a stepper motor and load cell. I use TB6560 as a driver for my motor, typical load cell that uses HX711, stepper motor (i think its Nema 42 but im not sure because the seller doesnt put the exact brand), and arduino Mega 2560. I set the voltage as 12 V and 1.4 A. I meant to make a control system on the stepper motor to be controlled by defined velocity, acceleration and displacement. I also need to display the current step and load read by the load cell.

Current result is im able to display both result from the current step and load, however idk why the movement of the motor works very slowly and not fast like my defined velocity and acceleration. While when i try separate code for the motor control and load cell, it works perfectly fine. Can anyone help me in figuring out whats wrong with my code? Thank you very much for your willingness in helping me out ;-;

Btw i didnt make this code by my own, credit to circuits4you and maker guides

#include <AccelStepper.h>
#include "HX711.h"

#define dirPin 52 
#define stepPin 53
#define motorInterfaceType 1
#define DOUT  3
#define CLK  2

// Define a stepper and the pins it will use
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
HX711 scale(DOUT, CLK);
float calibration_factor = -233500;

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

   // Change these to suit your stepper if you want
   stepper.setMaxSpeed(1000);
   stepper.setAcceleration(500);
   scale.set_scale(-233500);  //Calibration Factor obtained from first sketch
   scale.tare();
}

void loop()
{
   stepper.moveTo(-3000);
   stepper.run();
   Serial.print(stepper.currentPosition());
      Serial.print(",");
      Serial.print(scale.get_units(), 2);  //Up to 3 decimal points
      Serial.println(","); //Change this to kg and re-adjust the calibration factor if you follow lbs
}
1 Like

Serial output statements in your loop() function are slowing the step pulses. Remove them.

Hello, cedarlakeinstruments!
Thank you so much for replying to my thread! I'm afraid i cannot delete the serial.print part because i need to display the data and put it in a way so that the excel data streamer can read my data and do real time plotting. Do you have another suggestion to make the motor stop jittering?

Start by increasing the baud rate.

Hello, wildbill!

Thank you so much for replying to my question! Oh i dont know that baud rate can affect the stepper movement. I'll try your suggestion and let you know when its work. Thank you!!

1 Like

Your program architecture using AccelStepper is

take one step
read the scale
print the location and weight
repeat

Clearly there is a lot going on between steps beyond the inter step delay set by AccelStepper.

First, increase the Serial baud rate from 9600 to 115200.

Second, you need to address the scale reading time. There are some multiple readings built into the library. You may want to do some testing to see what your reading times are and if they can be improved. There is an alternative library which may be faster.
https://github.com/olkal/HX711_ADC

It's not clear what your expectations are for the frequency of plot points. What do you want with excel data streamer?

Hello, cattledog!

Thank you for passing by and reply to my question. I will try your suggestion and see whether it's work. Im not sure i understand what do you mean by the second part, im quite newbie in arduino thingy hehe. Ah about the data streamer, i want to display a real time data of load vs displacement and if possible create a graph from there. That's basically what i want to do by using data streamer. Thank you for your suggestion ^^

Your load cell scale reading is a process which takes some time. Real time data of the load is going to be dependent upon maximum reading frequency of the scale.

Your step speed can not be higher than your scale reading speed if you want a new reading for each step of displacement.

Ah i see, then i need to find out the maximum reading frequency of the load cell. Does it same with data interval time?

Add printing millis() to the Serial.print! Then check how often the printings are comming. I'm quite sure You don't need load cell info from every step.
Use millis() and Serial.print at resonable intervalls. That will speed up loop.

The problem is that most arduino libraries are written to do things one at a time and don't support any kind of multi-threaded operation. The motor stepping could be done on a timer interrupt, but that would require a complete rewrite.

Try doing something like what Railroader says and update the step processor every time through the loop, but only read the load cell/output serial data maybe every 100 times or so.

I have a current project that does just that
A few thing that will help.
For the stepper motor control see my tutorial on Multi-tasking in Arduino which has a complete stepper example controlled by user commands and a temperature input (my other project use a load cell)
That tutorial includes a loopTimer that tells you how fast your code is running, max delay, average delay etc.

For the problem of you debug print statement blocking the loop() when the TX buffer fills up see my tutorial on Arduino Serial I/O for the Real World which shows you how to get non-blocking Serial prints.

If your stepper needs to be speed controlled rather than position controlled see my modified Stepper Speed library Stepper Speed Control Library project with plotting

For the load cell I am using HX771 library. To make the code non-blocking use the isReady() to check if a new reading is available.

1 Like

Hi,
The company I work for did a similar project with real time graphical display.
Rasp Pi, does it all, and connected to factory Ethernet to send the data at the end of the day.
No PC required.

Tom... :smiley: :+1: :coffee: :australia:

Hello, Railroader!

Thanks for replying, but i do need to get a equivalent load data for each step. Is it possible if i remove that though?

but then the data is not connected right? the thing is i need to plot a graph for load vs step (well actually displacement), Or actually I need to eliminate several data from the stepper to make them both connected?

Hello, @drmpf !

Thank you for replying, i will definitely check the resources one by one and tried it out. I'll let you know the updates!

Hello, Tom!

Thanks for sparing your time in replying this thread. I already wasted my money for arduino mega and i don't have rasp pi available lol. I'm more familiar with the arduino also since i did not receive any lesson about rasp pi. But thank you, I'll definitely try your suggestion when i work in future project.

Hello, @everyone !

Thank you so much for helping me out in this problem. My motor now works fine! Im so happy ;-; I really do appreciate y'all helps and time u spent by replying this thread! This is beyond my knowledge lol

I combined @cedarlakeinstruments , @wildbill , @cattledog and @Railroader suggestion to increase the baud rate and add millis() to my code, and apparently it works! Thank you. I would definitely thank @TomGeorge and @drmpf also for your suggestion, i'll definitely try it on the future projects.

By the way you can work out if the motor is a NEMA42 by measuring it, NEMA42 is just a size specification, 4.2 inches wide. NEMA17's are 1.7 inches, etc etc.

1 Like

Nice You tell!
I use a baudrate of, is it 152000 bps. to showel out the prints as quickly as possible. That is similar to 15200 characters per second. Count how many chars each printing produces. Knowing the time for one run in loop, without the usual printout, You see what the margin is, hopefully positive, or negative if the print pipeline is overloaded.