#include <HX711.h>
#include <ESP32Servo.h>
#include <LiquidCrystal.h>
// Define the pins for the HX711
#define DOUT 21 // HX711 data pin
#define CLK 22 // HX711 clock pin
// Define the servo object
Servo servo1;
// Define the LCD object (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(12, 13, 14, 27, 26, 25);
// Define the calibration factor for the load cell
const float calibration_factor = 1000.0; // Change this value according to your load cell
// Create an instance of the HX711 library
HX711 scale;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize the LCD
lcd.begin(16, 2);
// Initialize the servo
servo1.attach(14); // GPIO14
// Initialize the scale
scale.begin(DOUT, CLK);
scale.set_scale(calibration_factor);
scale.tare(); // Reset the scale to 0
// Display initial message on LCD
lcd.clear();
lcd.print("Weight: ");
}
void loop() {
// Read weight from the load cell
float weight = scale.get_units();
// Clear the LCD screen and move cursor to the beginning
lcd.setCursor(8, 0); // Set cursor position to print weight value
lcd.print(weight, 1); // Print weight with 1 decimal place
lcd.print(" g "); // Clear any extra characters
// Check if weight is detected
if (weight > 0) {
// Rotate servo motor 360 degrees
for (int angle = 0; angle < 360; angle += 5) {
servo1.write(angle % 180); // Use modulo 180 to loop the angles for continuous rotation
delay(50); // Adjust delay as necessary for speed control
}
// Reset the position
servo1.write(0);
delay(1000); // Delay before the next rotation
}
delay(1000); // Adjust delay as needed
}