Skip to content

Instantly share code, notes, and snippets.

@EDISON-SCIENCE-CORNER
Created August 15, 2021 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EDISON-SCIENCE-CORNER/eb6b1a9834f4f77759d5680cd902dab8 to your computer and use it in GitHub Desktop.
Save EDISON-SCIENCE-CORNER/eb6b1a9834f4f77759d5680cd902dab8 to your computer and use it in GitHub Desktop.
#include <HX711_ADC.h>
#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
HX711_ADC LoadCell(HX711_dout, HX711_sck);
int tpin = 3;
const int calVal_eepromAdress = 0;
long t;
void setup() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 8);
display.println(" BALANCE");
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 25);
display.println("EDISON SCIENCE CORNER");
display.display();
delay(4000);
display.clearDisplay();
Serial.begin(57600);
delay(10);
Serial.println();
Serial.println("Starting...");
pinMode(tpin, INPUT_PULLUP);
LoadCell.begin();
float calibrationValue; // calibration value (see example file "Calibration.ino")
calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
//EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag())
{
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else
{
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
Serial.println("Startup is complete");
}
}
void loop() {
static boolean newDataReady = 0;
const int serialPrintInterval = 0; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
// get smoothed value from the dataset:
if (newDataReady)
{
if (millis() > t + serialPrintInterval) {
float i = LoadCell.getData();
Serial.print("Load_cell output val: ");
Serial.println(i);
newDataReady = 0;
t = millis();
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(WHITE);
display.setCursor(10, 10);
display.println(i);
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(115,20);
display.println("gm");
display.display();
delay(10);
}
if (digitalRead(tpin) == LOW) {
LoadCell.tareNoDelay();
}
// check if last tare operation is complete:
if (LoadCell.getTareStatus() == true) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Tare complete");
display.display();
delay(1000);
display.clearDisplay();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment