#include "HX711.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
HX711 scale;
int reading;
int diff;
int nxt;
//REPLACE WITH YOUR CALIBRATION FACTOR
#define CALIBRATION_FACTOR .42
//OLED Display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Button
void displayWeight(int weight){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Weight diff:");
display.display();
display.setCursor(0, 30);
display.setTextSize(2);
display.print(weight);
display.print(" ");
display.print("g");
display.display();
}
void setup() {
Serial.begin(57200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(CALIBRATION_FACTOR); // this value is obtained by calibrating the scale with known weights
scale.tare(); // reset the scale to 0
}
void loop() {
reading= scale.get_units();
diff = reading - nxt;
nxt = reading;
Serial.print("Weight: ");
Serial.println(diff);
displayWeight(diff);
delay(5000);
}