Arduino with Matlab: To control a DC motor by measured values of a load cell

Hello, I connected Arduino to Matlab using serial communication. I want to control a DC motor by measured value from a load cell.

For example, if the scale value measure 50 lb of compression or tension, the DC motor release or tighten the attached cable.

I used and changed basic codes of the load cell and the dc motor. The value of the load cell was measured on Matlab. When I individually tried, it worked on Arduino.

However, when I combined the codes and run it on Matlab, the data collection not only became so slow, but also stopped collecting data while the motor was running. The motor repeatedly ran and stopped approximately 4 seconds. The motor doesn't follow the code.

Can I get some advanced suggestions? I attached my circuit drawing and code

#include "HX711.h"

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define DOUT  3
#define CLK  2


HX711 scale;
float scaleValue = 0;

int enA = 9;
int in1 = 8;
int in2 = 7;

void setup() {
  Serial.begin(9600);
  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
 
  
  pinMode(enA, OUTPUT);

  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

}

void loop() {
  scaleValue = scale.get_units();
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.println();
  
  directionControl();
  delay(30);
  speedControl();
  delay(30);

      
    }
    
  void directionControl() {
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  analogWrite(enA, 255);

  if(scaleValue > 50.0)
  { 
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);

  delay(30);
  }
  
  //change motor directions
  if(scaleValue < -50.0)
  {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);

  delay(30);
  }
}

// This function lets you control speed of the motors
void speedControl() {
  // Turn on motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);

  // Accelerate from zero to maximum speed
  for (int i = 0; i < 256; i++) {
    analogWrite(enA, i);
    delay(20);
  }
  
  // Decelerate from maximum speed to zero
  for (int i = 255; i >= 0; --i) {
    analogWrite(enA, i);
    delay(20);
  }
  
}

Nice post (picture, code tags, schematic). Well done. :slight_smile:

You need to learn to not use delay in your code ever again. :slight_smile:

Please look at the following example.

File -> Examples -> 02.Digital -> BlinkWithoutDelay

Every time you call delay you waste thousands or millions of cycles where your Arduino could do something useful. Your Arduino is not human and does not need a vacation.

To Klaus_K,

Thank you for your answer. I removed the delay and it actually was a battery problem. However, the DC motor doesn't change the direction.

Are you sure this is correct?

if(scaleValue > 50.0)

if(scaleValue < -50.0)

What happens if the value is between -50 and 50?

On an Uno (5volt logic), both analogue and digital supplies (VDD, VCC) of the HX711 must be connected to the 5volt supply (assuming you use the Sparkfun board.
VDD is only connected to 3.3volt if a 3.3volt MCU is used.
Leo..

To Klaus and Wawa,

Thank you for your answer. When I moved 'if' command to speed control, it worked. Also, after I changed starting speed to 100, it worked well. Thank you guys!

Hi gsung2!!
I have the same problem, I want control dc servo encoder with value loadcell input (arduino uno), you can send code for me.!!!
I will appreciate you!!! :slight_smile: :slight_smile: