#include <HX711.h>
// Pin definitions
const int loadCellDoutPin = 2;
const int loadCellSckPin = 3;
const int ledPin = 9; // LED connected to pin 9
// Create HX711 object
HX711 scale;
// Calibration factor (change this as needed)
float calibrationFactor = 1100; // Adjust this value for accurate readings
void setup() {
Serial.begin(9600);
scale.begin(loadCellDoutPin, loadCellSckPin);
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.println("HX711 Calibration");
Serial.println("Place a known weight on the load cell and press Enter.");
while (!Serial.available()) {
// Wait for user to press Enter
}
Serial.read(); // Clear the buffer
// Tare the scale
scale.tare();
Serial.println("Scale is tared. Please place the known weight on the load cell.");
delay(2000); // Wait for a bit
long reading = scale.get_units(10); // Average of 10 readings
Serial.print("Reading with weight: ");
Serial.println(reading);
Serial.print("Enter the calibration factor: ");
while (!Serial.available()) {
// Wait for user input
}
calibrationFactor = Serial.parseFloat();
Serial.println("Calibration factor set.");
scale.set_scale(calibrationFactor); // Set the calibration factor
scale.tare(); // Reset the scale to 0
Serial.println("Calibration complete. You can now use the scale.");
}
void loop() {
float weight = scale.get_units();
// Map the weight to a PWM value (0-255)
int pwmValue = map(weight, 0, 1000, 0, 255); // Adjust the range as needed
// Ensure PWM value is within bounds
pwmValue = constrain(pwmValue, 0, 255);
analogWrite(ledPin, pwmValue); // Set LED brightness
Serial.print("Weight: ");
Serial.print(weight, 2); // Print weight in grams
Serial.print(" g, LED Brightness: ");
Serial.println(pwmValue);
delay(1000);
}