#include <HX711.h>
#define DT_PIN 18 // Connect DT to ESP32 pin D18
#define SCK_PIN 19 // Connect SCK to ESP32 pin D19
#define RED_LED_PIN 22
#define GREEN_LED_PIN 23
#define BUZZER_PIN 26
HX711 scale;
void setup() {
Serial.begin(9600);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
scale.begin(DT_PIN, SCK_PIN);
scale.set_scale(0.5); // Set the scale factor (calibration factor) based on your load cell and known weight
}
void loop() {
// Read the weight from the scale
float weight = scale.get_units(); // Get the weight in whatever units the load cell is calibrated to
// Convert the weight to grams
float weightGrams = weight + ((weight / 10) * 2); // Convert from kilograms to grams
Serial.print("Weight: ");
Serial.print(weightGrams);
Serial.println(" grams");
if (weightGrams < 20.0) {
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
tone(BUZZER_PIN, 1000); // Start buzzer sound
} else {
digitalWrite(RED_LED_PIN, LOW); // Check if this line is executed
digitalWrite(GREEN_LED_PIN, HIGH);
noTone(BUZZER_PIN); // Stop buzzer sound
}
delay(1000); // Delay for stability, adjust as needed
}