#include <HX711.h>
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 5;
const int LOADCELL_SCK_PIN = 18;
HX711 scale;
// initialize OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#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);
//buzzer setup
#define freq 2000
#define channel 0
#define resolution 8
void setup() {
Serial.begin(115200);
//buzzer ------------------->
ledcSetup(channel, freq, resolution);
ledcAttachPin(12, channel);
//OLED --------------------->
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(1000);
//Load Cell -------------------------------->
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
Serial.println("HX711 LOAD CELL");
Serial.println("Initialize...");
loadcell_start();
}
void loop() {
//calibration_setup(); //kalibrasi load cell
loadcell_read();
}
void calibration_setup (){ //untuk mencari faktor kalibrasi
if (scale.is_ready()) {
scale.set_scale();
Serial.println("Tare... remove any weights from the scale.");
delay(4000);
scale.tare();
Serial.println("Tare done...");
Serial.print("Place a known weight on the scale...");
delay(6000);
long reading = scale.get_units(10);
Serial.print("Result: ");
Serial.println(reading);
Serial.println(reading/43);//calibration factor = result/berat sesungguhnya
delay(1000);
}
else {
Serial.println("HX711 not found.");
}
delay(1000);
}
void loadcell_start(){ //meng setup load cell sebelum digunakan
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(419);
//scale.set_scale(-9334); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void loadcell_read(){ //membaca nilai dari load cell
Serial.print("Weight:\t");
Serial.println(scale.get_units(), 1);
display.clearDisplay();
display.setTextSize(1.95);
display.setTextColor(WHITE);
display.setCursor(18, 25);
// Display static text
display.print("WEIGHT: ");
display.setCursor(63, 25);
display.print(scale.get_units(), 1);
display.setCursor(98, 25);
display.println("Kg");
display.display();
if(int(scale.get_units(1)) > 29){ //buzz ketika berat lebih dari 29
ledcWriteTone(channel, 2000);
ledcWrite(channel,125);
}else{
ledcWrite(channel,0);
}
delay(2000);
}