/*
Standart I2C pins! Use these to connect the OLED SSD1306 to the MCU
========================
ESP8266
SDA: D2 (I2C -> Data)
SCL: D1 (I2C -> Clock)
ESP32
SDA: 21
SCL: 22
*/
#define VERSIONNO 0.1
#if defined(ESP32) // For real hardware I use a ESP8266, so if it's an ESP32, then we are in WOKWI
#define WOKWI
#endif
#include <Arduino.h> // includes freeRTOS
#include "HX711.h" // Library HX711 by Bogdan Necula: https://github.com/bogde/HX711
#include <Adafruit_SSD1306.h>
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
// HX711 circuit
HX711 scale;
#ifdef ESP8266
#define LOADCELL_DOUT_PIN D6
#define LOADCELL_SCK_PIN D5
#define BTN_TARE_PIN D7
#define MEASURING_INTERVAL 1000
#elif defined(ESP32)
#define LOADCELL_DOUT_PIN 19
#define LOADCELL_SCK_PIN 18
#define BTN_TARE_PIN 12
#else
#error no valid MCU
#endif
#ifdef WOKWI
#define MEASURING_INTERVAL 200
const float CALIBRATION_VALUE = 0.42; // This is the value for WOKWI
#else
const float CALIBRATION_VALUE = 392.5;
#endif
//OLED Display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT);
// Button(uint8_t pin, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
Button btnTare(BTN_TARE_PIN, 20, true);
void displayWeight(const float weight){
const int u = (int)(weight + 0.5 - (weight < 0));
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.println("Weight:");
oled.setCursor(0, 10);
oled.setTextSize(2);
oled.printf("%i g", u);
displayStatusbar();
}
void displayText(const char *msg) {
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(0, 10);
oled.print(msg);
displayStatusbar();
}
void displayStatusbar() {
oled.setTextSize(1);
oled.setCursor(0, SCREEN_HEIGHT - 10);
oled.printf("V-%0.1f", VERSIONNO);
oled.display();
}
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.printf("\n----- Scale testprogram for HX711 version %1.1f ------\n", VERSIONNO);
// Setup Buttons
btnTare.begin();
// Setup OLED
if(!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("ERROR: SSD1306 allocation failed, execution stoped!"));
for(;;); // Don't proceed, loop forever
}
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
// Setup Scale (Loadcell HX711)
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
//scale.wait_for_ready_timeout(200);
scale.set_scale(CALIBRATION_VALUE);
delay(200);
scale.tare(10);
}
void loop() {
static uint32_t scale_ms = 0;
btnTare.read();
if (btnTare.isPressed()) {
Serial.println("--- Taring ---");
displayText("TARE");
while (btnTare.read() && btnTare.isPressed());
scale.tare();
Serial.println("--- Tare done ---");
scale_ms = 0;
}
if (millis() > scale_ms) {
const float f = scale.get_units();
displayWeight(f);
Serial.printf("weight = %0.5f\n", f);
scale_ms = millis() + MEASURING_INTERVAL;
#ifdef WOKWI
delay(20);
#endif
}
}