Notifications
Clear all

Stepper and load cell

150 Posts
8 Users
31 Likes
3,483 Views
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

Good day. I'm having trouble controlling a steppe motor with a load cell. I would like for the motor to slow down when the pressure increase and speed up when it decreases. I have the code for the load cell working but don't know how to incorporate the stepper in the code. 


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6974
 

@the-apprentice While I don't have a direct answer if I had that problem, I would look in the stepper motor library sample sketches for a sketch that shows you how to control the motors some other way, like a potentiometer and then replace the potentiometer code with the load sensor code.

If you create functional modules with sufficient abstraction, it will be a relatively easy project.

Good luck.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 

@the-apprentice

Since you haven't given us any specifics of your stepper or your range of weights, we can't give you ny specifics on solving your problem(s).

However, if you start by sort of following Ron's (@zander's) advice and google using a stepper motor without a library, you can alter the speed of the motor by setting the interpulse delay time longer (to slow the motor down) or shorter (to speed the motor up).

You should start by deciding on an upper an lower limit for the low cell's potential range and then set an upper and lower limit for the delay to the stepper. Use a MAP command to convert from a load cell reading to a delay time.

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

   
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

@zander Easy if you know how. LOL My problem is I have no experience with coding and only now started. Bill always explains it great and then supplies the code. This is the first time I'm trying to do my own project recycling plastic waist.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@the-apprentice 

Can you post the code for the load cell that you have working and then maybe someone will be able to show how to incorporate the stepper the code.

 


   
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

   
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

@robotbuilder I know it is the wrong way to post codes but i have posted and it wont show. 

#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
 
//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
 
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
 
const int calVal_eepromAdress = 0;
unsigned long t = 0;
 
void setup() {
  Serial.begin(57600); delay(10);
  Serial.println();
  Serial.println("Starting...");
 
  LoadCell.begin();
  //LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
  unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag() || LoadCell.getSignalTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
  else {
    LoadCell.setCalFactor(1.0); // user set calibration value (float), initial value 1.0 may be used for this sketch
    Serial.println("Startup is complete");
  }
  while (!LoadCell.update());
  calibrate(); //start calibration procedure
}
 
void loop() {
  static boolean newDataReady = 0;
  const int serialPrintInterval = 0; //increase value to slow down serial print activity
 
  // check for new data/start next conversion:
  if (LoadCell.update()) newDataReady = true;
 
  // get smoothed value from the dataset:
  if (newDataReady) {
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
      Serial.print("Load_cell output val: ");
      Serial.println(i);
      newDataReady = 0;
      t = millis();
    }
  }
 
  // receive command from serial terminal
  if (Serial.available() > 0) {
    char inByte = Serial.read();
    if (inByte == 't') LoadCell.tareNoDelay(); //tare
    else if (inByte == 'r') calibrate(); //calibrate
    else if (inByte == 'c') changeSavedCalFactor(); //edit calibration value manually
  }
 
  // check if last tare operation is complete
  if (LoadCell.getTareStatus() == true) {
    Serial.println("Tare complete");
  }
 
}
 
void calibrate() {
  Serial.println("***");
  Serial.println("Start calibration:");
  Serial.println("Place the load cell an a level stable surface.");
  Serial.println("Remove any load applied to the load cell.");
  Serial.println("Send 't' from serial monitor to set the tare offset.");
 
  boolean _resume = false;
  while (_resume == false) {
    LoadCell.update();
    if (Serial.available() > 0) {
      if (Serial.available() > 0) {
        char inByte = Serial.read();
        if (inByte == 't') LoadCell.tareNoDelay();
      }
    }
    if (LoadCell.getTareStatus() == true) {
      Serial.println("Tare complete");
      _resume = true;
    }
  }
 
  Serial.println("Now, place your known mass on the loadcell.");
  Serial.println("Then send the weight of this mass (i.e. 100.0) from serial monitor.");
 
  float known_mass = 0;
  _resume = false;
  while (_resume == false) {
    LoadCell.update();
    if (Serial.available() > 0) {
      known_mass = Serial.parseFloat();
      if (known_mass != 0) {
        Serial.print("Known mass is: ");
        Serial.println(known_mass);
        _resume = true;
      }
    }
  }
 
  LoadCell.refreshDataSet(); //refresh the dataset to be sure that the known mass is measured correct
  float newCalibrationValue = LoadCell.getNewCalibration(known_mass); //get the new calibration value
 
  Serial.print("New calibration value has been set to: ");
  Serial.print(newCalibrationValue);
  Serial.println(", use this as calibration value (calFactor) in your project sketch.");
  Serial.print("Save this value to EEPROM adress ");
  Serial.print(calVal_eepromAdress);
  Serial.println("? y/n");
 
  _resume = false;
  while (_resume == false) {
    if (Serial.available() > 0) {
      char inByte = Serial.read();
      if (inByte == 'y') {
#if defined(ESP8266)|| defined(ESP32)
        EEPROM.begin(512);
#endif
        EEPROM.put(calVal_eepromAdress, newCalibrationValue);
#if defined(ESP8266)|| defined(ESP32)
        EEPROM.commit();
#endif
        EEPROM.get(calVal_eepromAdress, newCalibrationValue);
        Serial.print("Value ");
        Serial.print(newCalibrationValue);
        Serial.print(" saved to EEPROM address: ");
        Serial.println(calVal_eepromAdress);
        _resume = true;
 
      }
      else if (inByte == 'n') {
        Serial.println("Value not saved to EEPROM");
        _resume = true;
      }
    }
  }
 
  Serial.println("End calibration");
  Serial.println("***");
  Serial.println("To re-calibrate, send 'r' from serial monitor.");
  Serial.println("For manual edit of the calibration value, send 'c' from serial monitor.");
  Serial.println("***");
}
 
void changeSavedCalFactor() {
  float oldCalibrationValue = LoadCell.getCalFactor();
  boolean _resume = false;
  Serial.println("***");
  Serial.print("Current value is: ");
  Serial.println(oldCalibrationValue);
  Serial.println("Now, send the new value from serial monitor, i.e. 696.0");
  float newCalibrationValue;
  while (_resume == false) {
    if (Serial.available() > 0) {
      newCalibrationValue = Serial.parseFloat();
      if (newCalibrationValue != 0) {
        Serial.print("New calibration value is: ");
        Serial.println(newCalibrationValue);
        LoadCell.setCalFactor(newCalibrationValue);
        _resume = true;
      }
    }
  }
  _resume = false;
  Serial.print("Save this value to EEPROM adress ");
  Serial.print(calVal_eepromAdress);
  Serial.println("? y/n");
  while (_resume == false) {
    if (Serial.available() > 0) {
      char inByte = Serial.read();
      if (inByte == 'y') {
#if defined(ESP8266)|| defined(ESP32)
        EEPROM.begin(512);
#endif
        EEPROM.put(calVal_eepromAdress, newCalibrationValue);
#if defined(ESP8266)|| defined(ESP32)
        EEPROM.commit();
#endif
        EEPROM.get(calVal_eepromAdress, newCalibrationValue);
        Serial.print("Value ");
        Serial.print(newCalibrationValue);
        Serial.print(" saved to EEPROM address: ");
        Serial.println(calVal_eepromAdress);
        _resume = true;
      }
      else if (inByte == 'n') {
        Serial.println("Value not saved to EEPROM");
        _resume = true;
      }
    }
  }
  Serial.println("End change calibration value");
  Serial.println("***");
}

   
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

I'm busy with a project on recycling plastic's. I need the stepper motor to coil up the treated string. This needs to be at about 1Kg tension on the string. I need the stepper to try and keep the tension but at a set max speed. This I control with a potentiometer. I would like if the load cell could communicate with the stepper and adjust the speed according to the tension on the load cell. Speed up if the tension is low and slow down if the tension is high but not exceed the max speed. A busser would be great if it stops. Not sure how to do this as my coding skills are crappy. I got the load cell to work on the serial monitor and the potentiometer with the stepper. How to get the 2 working together is a challenge. 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6974
 

@the-apprentice First use the HELP to learn how to posy code. Also please use IDE/Tools/Auto Format first. Try posting some code in the practice forum so are sure how it works. Use the Preview first. 

I actually do it a but different, I select all and copy in the IDE then in the forum click the <> button then paste into the little window., it then loks like the following

void setup() {
    }

void loop() {
}

As to how to do what you want, in the loop you will see a statement that says getvalue. Use that value to drive the stepper. You will likely need to remap it and that may be a trial and error exercise.

  // get smoothed value from the dataset:
  if (newDataReady) {
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
      Serial.print("Load_cell output val: ");
      Serial.println(i);
      newDataReady = 0;
      t = millis();
    }
  }

Put the stepper code in a module so all you need to do is add one line after the getData to pass that value to the stepper code.

Good luck.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Inst-Tech reacted
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

@zander Thanks


   
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

   
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 4 months ago
Posts: 61
Topic starter  

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6974
 

@the-apprentice Did you forget something?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6974
 

@the-apprentice Same question

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Page 1 / 10