Controlling stepper motor with loadcell but stepper motor steps step by step

Hi,

I have project, where I am trying to control a stepper motor using a loadcell, but I am facing a problem where my stepper motor is stepping only step by step and smoothly. I doubt it to be cause by the time difference between the data collected from the loadcell using Arduino.

I am using A4988 as a drive for the stepper motor and hx711 as an amplifier for the load cell, and I am using the "Queuetue_HX711_Library" library to utilize the hx711 amplifier.

Please help me with this problem.

The code:

#include <Q2HX711.h>
const int stepPin = 3; 
const int dirPin = 4;
const byte hx711_data_pin = 6;
const byte hx711_clock_pin = 5;
Q2HX711 hx711(hx711_data_pin, hx711_clock_pin);
long loadcell_val;


void setup() {
Serial.begin(9600);
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}

void loop() {
 loadcell_val = hx711.read()/100;
 Serial.println(loadcell_val);
 // set direction, HIGH for clockwise, LOW for anticlockwise
 if (loadcell_val > 86000.0){
   digitalWrite(dirPin, HIGH);
  digitalWrite(stepPin,HIGH);
  delayMicroseconds(400);
  digitalWrite(stepPin,LOW); 
  delayMicroseconds(400);
 }
 else if (loadcell_val < 84000.0){
   digitalWrite(dirPin, LOW);
  digitalWrite(stepPin,HIGH);
  delayMicroseconds(400);
  digitalWrite(stepPin,LOW); 
  delayMicroseconds(400);
 }
}

What values are you seeing on Serial Monitor for loadcell_val?

If you turn on the "Show timestamp" feature in Serial Monitor, how long is it between messages?

The delay looks too short to me. I suggest to start from 10000 as a beginning.

Use a faster baudrate and/or do not print with every loop cycle. You probably fill your buffer up, which will slow down your loop cycle time.

loadCell_val is defined as long. Why compare with a float?

PS. please post code in codetags.

Thank you John for the response and sorry for the late reply.

I have attached below a screenshot of what I am exactly getting on my Serial Monitor for loadcell_val and the time difference between messages after using the "Show timestamp" feature.

Hi Bahner,

I removed the "Serial.begin(9600);" from the code but still no difference. I also changed the "long" to "float" but still no luck with that.

Thank you for the PS btw, sorry I am new to the forum.

Thank you roader it tried that as well but still no luck.

You must not remove the Serial.begin.
Either you increase the baudrate:

Serial.begin(115200);

or you remove the

Serial.println

Once again thank you Bahner for the reply.
I did exactly that, but still no luck unfortunately.
I tried to remove both "Serial.begin" and "Serial.println". I also tried different baudrates but unfortunately the motor still stepping each step at a time.

Well, I don't have experience with that lib. But maybe reading the HX711 needs too much time to reach the step speed you want.

You seem to be getting about 10 loops per second so SOMETHING is taking a long time. The Serial.print() should take less than 8 milliseconds even at the very slow rate of 9600. I'm guessing that most of the other 93 milliseconds is spent waiting in the hx711 library.

Perhaps you should try a different HX711 library.

Hi John thank you for the answer. I tried another library this time "HX711.h" and I actually used I library for the stepper motor this time as well, but unfortunately I am still having the same exact problem.

The new code.

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

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 6;
const int LOADCELL_SCK_PIN = 5;
long loadcell_val;

HX711 scale;

//stepper motor control with accelstepper library
#define dirPin 4
#define stepPin 3
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {

  //stepper motor max speed
  stepper.setMaxSpeed(1000);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}

void loop() {
loadcell_val = scale.read();

 if (loadcell_val < 90000){
  stepper.setSpeed(900);
  stepper.runSpeed();
 }
 else if (loadcell_val > 170000.0){
  stepper.setSpeed(-900);
  stepper.runSpeed();
 }

}

The output rate of the HX711 is 10 Hz using the internal oscillator and the RATE pin LOW. Set the RATE pin HIGH to get 80 Hz.

To get read rates faster than 80 Hz you need an external clock with a rate of over 12 MHz. The maximum external clock is 20 MHz which will get you a sample rate of almost 145 Hz.

Instead of using the Accelstepper you could try the MoToStepper class of my MobaTools library. This lib creates the step pulses in timer interrupts, thus it can step even while waiting for the HX711

1 Like

Thank you Bahner, that is actually a brilliant idea, never crossed my mind.
I will give it a try and share an update with what happened with me

Hello John, thank you for the answer, but how can set the pin rate high I searched online but I couldn't really find a definitive answer

Hi @MicroBahner can the "MoToStepper" class of your "MobaTools" library, be used with "a4988" step motor driver.

Yes, it can be used with any step/dir driver. You must define that when creating a stepper object:
MoToStepper myStepper( stepsPerRev, STEPDIR);
'stepsPerRev' are the steps needed for one revolution (depending on the motor, microstepping, gear .. ). This is needed if you want to define target position as angele and/or speed as rpm.

1 Like

Hey Bahner thank you so much your recommendation worked exceptionally well… thank you for the advice