Servo motor working with load cell on Arduino Uno

Thread Starter

syahir982

Joined Jan 18, 2021
6
Hello guys, I am final year student of agricultural engineering. I would like to do a project regarding servo motor and load cell. May I know what code that able to move my servo motor by referring the weight on load cell. For example, when the load cell weighing 100 gram, then the servo motor will be run. I appreciated you help. Thank you :)
 

Thread Starter

syahir982

Joined Jan 18, 2021
6
Here are links to tutorials on how to use a load cell and how to control a motor with an Arduino. Once you understand how they work and how to program them, you can combine code from the two examples. Have fun.

https://www.brainy-bits.com/post/how-to-use-a-weight-sensor-load-cell-hx711-with-an-arduino

https://www.tutorialspoint.com/ardu...torialspoint.com/arduino/arduino_dc_motor.htm
Thank you for your help! but the tutorial link on how to control a motor with an Arduino, the page I was looking for doesn't exist :(
 

ericgibbs

Joined Jan 29, 2010
11,858
hi 982,
Welcome to AAC.
Do you have a particular Servo motor in mind for the project.

I have some Arduino Code for Steppers , Servo's and LCell modules.

E
 

Thread Starter

syahir982

Joined Jan 18, 2021
6
hi 982,
Welcome to AAC.
Do you have a particular Servo motor in mind for the project.

I have some Arduino Code for Steppers , Servo's and LCell modules.

E
I'm currently using Micro Servo Motor SG90. For the load cell, I'm using 20kg load cell with HX711.
 

ericgibbs

Joined Jan 29, 2010
11,858
hi s,
Watch this video for the Servo.
The link includes a number of libs, that should help to code the project.

Post any code you write, we can check it.
E

 

Thread Starter

syahir982

Joined Jan 18, 2021
6
hi s,
Have you written any code for the HX711 module.?
This is a basic HX711 test sketch, change the .txt to .ino

E
I already found the code and it successful to weighing object on load cell. But I don't know how to combine the code of the load cell with the servo motor code. I'm still searching for the code of servo motor.
 

Thread Starter

syahir982

Joined Jan 18, 2021
6
This code I already combined. Only load cell able to read the weight of the object but the servo motor does not respond.

Code:
#include "HX711.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

Servo myservo;

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = A1;
const int LOADCELL_SCK_PIN = A0; 
int servoPin = 8;

LiquidCrystal_I2C lcd(0x27, 16, 2);
HX711 scale;

void setup() {
  Serial.begin(9600);
  Serial.println("Make sure there is no load okay?");
  lcd.begin();
  lcd.backlight();
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(96.3);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare(50);                        // reset the scale to 0
  Serial.println("Go weight it");
  myservo.attach(8,600,2300);
 
}

void loop() {
  Serial.print("Weight");
  float berat= scale.get_units(25);
  if(berat<=0.1)
  {
    berat=0.0;
  }
  Serial.println(berat,1);
  if(berat<500)
  {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Go weight it");
  lcd.setCursor(0, 1);
  lcd.print("Weight= ");
  lcd.print(berat,1);
  lcd.print(" g");
  }
  if(berat>=500)
  {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Go weight it");
  float hasil=berat/1000;
  lcd.setCursor(0, 1);
  lcd.print("Weight= ");
  lcd.print(hasil);
  lcd.print(" kg");
  }
  scale.power_down();
  delay(250);
  scale.power_up();
  }

void loop2() {
  float berat= scale.get_units(25);
  if(berat<200) {
    myservo.write(180);
    delay(1000);
  }
  else {
    myservo.write(0);
  }
}
 

ericgibbs

Joined Jan 29, 2010
11,858
hi s,
Downloaded your code, I will build the hardware.
I have the Arduino, LCell, HX711 and Servo Motor SG90.

I will post back tomorrow with the results.

E
 

djsfantasi

Joined Apr 11, 2010
7,386
This code I already combined. Only load cell able to read the weight of the object but the servo motor does not respond.

Code:
#include "HX711.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

Servo myservo;

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = A1;
const int LOADCELL_SCK_PIN = A0;
int servoPin = 8;

LiquidCrystal_I2C lcd(0x27, 16, 2);
HX711 scale;

void setup() {
  Serial.begin(9600);
  Serial.println("Make sure there is no load okay?");
  lcd.begin();
  lcd.backlight();
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(96.3);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare(50);                        // reset the scale to 0
  Serial.println("Go weight it");
  myservo.attach(8,600,2300);

}

void loop() {
  Serial.print("Weight");
  float berat= scale.get_units(25);
  if(berat<=0.1)
  {
    berat=0.0;
  }
  Serial.println(berat,1);
  if(berat<500)
  {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Go weight it");
  lcd.setCursor(0, 1);
  lcd.print("Weight= ");
  lcd.print(berat,1);
  lcd.print(" g");
  }
  if(berat>=500)
  {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Go weight it");
  float hasil=berat/1000;
  lcd.setCursor(0, 1);
  lcd.print("Weight= ");
  lcd.print(hasil);
  lcd.print(" kg");
  }
  scale.power_down();
  delay(250);
  scale.power_up();
  }

void loop2() {
  float berat= scale.get_units(25);
  if(berat<200) {
    myservo.write(180);
    delay(1000);
  }
  else {
    myservo.write(0);
  }
}
Unusual construction of your code. I assume that you rename loop2() to loop() and the original loop() to something else?
 

KeithWalker

Joined Jul 10, 2017
1,509
Take the lower and upper values you get from the scale and map them to the servo, 0 to 180. The servo will then move to a position corresponding to the weight.
 

ericgibbs

Joined Jan 29, 2010
11,858
hi s,
This is a modified version of your program.
// out the I2C LCD section, used the Serial Monitor.
Problem was the program never entered loop2.
Used two dummy weight values 123 & 567 [ ie: below and above the 500g]

This image is a serial print out for 123g and 567g
Change the attached file extension from .txt to .ino.

E
BTW: I would recommend that you use the Serial Monitor for debugging your programs, before added the extra hardware.
Enter dummy test values.


Update:
Checked with a SG90 Servo, driving back and forward about 120 degrees. OK
 

Attachments

Last edited:

Thread Starter

syahir982

Joined Jan 18, 2021
6
hi s,
This is a modified version of your program.
// out the I2C LCD section, used the Serial Monitor.
Problem was the program never entered loop2.
Used two dummy weight values 123 & 567 [ ie: below and above the 500g]

This image is a serial print out for 123g and 567g
Change the attached file extension from .txt to .ino.

E
BTW: I would recommend that you use the Serial Monitor for debugging your programs, before added the extra hardware.
Enter dummy test values.


Update:
Checked with a SG90 Servo, driving back and forward about 120 degrees. OK
Thank You. I already got it. I appreciated your help :D
 
Top