#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HX711.h>
// HX711 pins for Load Cell 1
const int LOADCELL_DT_PIN_1 = 3;
const int LOADCELL_SCK_PIN_1 = 2;
HX711 scale1;
// HX711 pins for Load Cell 2
const int LOADCELL_DT_PIN_2 = 13;
const int LOADCELL_SCK_PIN_2 = 12;
HX711 scale2;
// Calibration factors (these will be determined during calibration)
// IMPORTANT: THESE ARE EXAMPLE VALUES. YOU MUST CALIBRATE YOUR OWN!
float calibration_factor_1 = 22000.0; // Adjust this value during calibration for Load Cell 1
float calibration_factor_2 = 22000.0; // Adjust this value during calibration for Load Cell 2
// OLED Display parameters
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
Serial.println("Dual Load Cell Weight Scale");
// Initialize OLED display
// Initialize HX711 for Load Cell 1
scale1.begin(LOADCELL_DT_PIN_1, LOADCELL_SCK_PIN_1);
scale1.set_scale(); // Reset to initial values
scale1.tare(); // Reset the scale to 0
Serial.println("Load Cell 1 ready.");
// Initialize HX711 for Load Cell 2
scale2.begin(LOADCELL_DT_PIN_2, LOADCELL_SCK_PIN_2);
scale2.set_scale(); // Reset to initial values
scale2.tare(); // Reset the scale to 0
Serial.println("Load Cell 2 ready.");
// Set initial calibration factors
scale1.set_scale(calibration_factor_1);
scale2.set_scale(calibration_factor_2);
Serial.println("Place known weights on the load cells for calibration if needed.");
Serial.println("Send 'c' to recalibrate. Send 't' to tare.");
}
void loop() {
if (Serial.available()) {
char key = Serial.read();
if (key == 't') {
Serial.println("Tare command received.");
scale1.tare(); // Tare both scales
scale2.tare();
Serial.println("Scales tared.");
} else if (key == 'c') {
// Calibration routine (simplified)
Serial.println("Calibration mode initiated. Put nothing on Load Cell 1 and press any key.");
while (!Serial.available()); Serial.read(); // Wait for user input
long zero_factor1 = scale1.read_average();
Serial.print("Zero factor 1: ");
Serial.println(zero_factor1);
Serial.println("Put a known weight (e.g., 100g) on Load Cell 1 and enter its value (in grams) then press enter.");
while (!Serial.available());
float known_weight_1 = Serial.parseFloat();
while (Serial.available()) Serial.read(); // Clear buffer
long reading1 = scale1.read_average();
calibration_factor_1 = (float)(reading1 - zero_factor1) / known_weight_1;
scale1.set_scale(calibration_factor_1);
Serial.print("New calibration factor 1: ");
Serial.println(calibration_factor_1, 5);
Serial.println("Calibration for Load Cell 1 complete.");
Serial.println("Put nothing on Load Cell 2 and press any key.");
while (!Serial.available()); Serial.read(); // Wait for user input
long zero_factor2 = scale2.read_average();
Serial.print("Zero factor 2: ");
Serial.println(zero_factor2);
Serial.println("Put a known weight (e.g., 100g) on Load Cell 2 and enter its value (in grams) then press enter.");
while (!Serial.available());
float known_weight_2 = Serial.parseFloat();
while (Serial.available()) Serial.read(); // Clear buffer
long reading2 = scale2.read_average();
calibration_factor_2 = (float)(reading2 - zero_factor2) / known_weight_2;
scale2.set_scale(calibration_factor_2);
Serial.print("New calibration factor 2: ");
Serial.println(calibration_factor_2, 5);
Serial.println("Calibration for Load Cell 2 complete.");
Serial.println("You can now read weights. Send 'c' to recalibrate. Send 't' to tare.");
}
}
// Read weights
float weight1 = scale1.get_units(5); // Read average of 5 readings
float weight2 = scale2.get_units(5); // Read average of 5 readings
// Print to Serial Monitor (for debugging and calibration)
Serial.print("Load Cell 1: ");
Serial.print(weight1, 2);
Serial.print(" g \t");
Serial.print("Load Cell 2: ");
Serial.print(weight2, 2);
Serial.println(" g");
delay(500); // Adjust delay as needed
}