#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#include <Servo.h>
//
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2
#define SERVO_PIN 5
// Defining variables, components
LiquidCrystal_I2C lcd(0x27, 16, 2);
HX711 loadcell;
Servo servo;
// int angle;
// float weight;
void setup() {
// Serial data rate 9600
Serial.begin(9600);
// Initialise LCD
lcd.init();
lcd.backlight();
lcd.cursor();
// Init load cell hx711
loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
loadcell.set_scale(420); // set the calibration factor (this will depend on your load cell)
// Attach servo pin 5
servo.attach(5);
}
void loop() {
// Get weight from hx 711 load cell
float weight = loadcell.get_units();
// map the weight range to the servo angle range
int angle = map(weight, 0, 5.0, 0, 180);
// Servo move to weight
servo.write(angle);
// Write weight to LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Weight: ");
lcd.print(weight);
lcd.print(" kg");
// Delay of 500 ms
delay(500);
}