Getting weight from load cell on to oled

Hi there,

so first off all, still new to Arduino...
second, I have a load cell which works with the following code:

*
   -------------------------------------------------------------------------------------
   HX711_ADC
   Arduino library for HX711 24-Bit Analog-to-Digital Converter for Weight Scales
   Olav Kallhovd sept2017
   -------------------------------------------------------------------------------------
*/

/*
   Settling time (number of samples) and data filtering can be adjusted in the config.h file
   For calibration and storing the calibration value in eeprom, see example file "Calibration.ino"

   The update() function checks for new data and starts the next conversion. In order to acheive maximum effective
   sample rate, update() should be called at least as often as the HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS.
   If you have other time consuming code running (i.e. a graphical LCD), consider calling update() from an interrupt routine,
   see example file "Read_1x_load_cell_interrupt_driven.ino".

   This is an example sketch on how to use this library
*/

#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif

//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin

//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);

const int calVal_eepromAdress = 0;
unsigned long t = 0;

void setup() {
  Serial.begin(57600); delay(10);
  Serial.println();
  Serial.println("Starting...");

  LoadCell.begin();
  //LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
  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

  unsigned 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();
    }
  }

  // receive command from serial terminal, send 't' to initiate tare operation:
  if (Serial.available() > 0) {
    char inByte = Serial.read();
    if (inByte == 't') LoadCell.tareNoDelay();
  }

  // check if last tare operation is complete:
  if (LoadCell.getTareStatus() == true) {
    Serial.println("Tare complete");
  }

}

so, that works fine, now for the tricky part.
I want these values, coming out onto my Oled display.
the Oled is connecting as it should to pin A4 & A5.

I have tried to use different codes on different sites but it just doesn't want to work.
or I get rubbish or I get nothing, so.... please help in how to integrate this ...

THX,

Which library are you using to control the OLED and have you tried the examples that come with it ?

Which Arduino board are you using ?

hi, I am using the adafruit 1306 for the oled, although the manual of the oled says to use the U8g2 library.
the board being used is: AZDelivery Microcontroller Board with ATmega328P ATmega16U2 Y

I can have something on the screen if I use the examples installed.
that is working fine, but I want the output of the load cell to be presented on the screen...
Oled used: AZDelivery 1.3" OLED Screen I2C SSH1106 Chip 128 x 64 pixels I2C White Character Display

grts

Why did you choose to do that? Normally what we do is follow the manual and get the thing working, then start tweaking and changing things.

Ok, post the code where you tried and it didn't work and let's see if we can figure out where you went wrong.

I haven’t been able to put the 2 together just yet. I have been experimenting with code online but this results in my screen not really working and the output of the cell not being correct. I am not home right now but I will post the code and other’s tomorrow so we can figure this out!!!

This is a working sketch using UNO-(1.1"x1.1")OLED-Adafruit_SSD1306.h. You can interface your Load Cell data easily with this sketch.

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) 
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  testdrawstyles();    // Draw 'stylized' characters
}

void loop() 
{
}

void testdrawstyles(void) 
{
  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0, 0);            // Start at top-left corner
  display.println(F("Hello, world!"));

  display.setTextColor(SSD1306_WHITE);
  //(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  display.println(3.141592, 6); //6-digit after decimal point

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.print(F("0x")); display.println(0xDEADBEEF, HEX);

  display.display();
  delay(2000);
}

ok, so I figured something out and used this code:

#include "HX711.h"
#include <U8g2lib.h>

#define DOUT_PIN  4
#define CLK_PIN   5

HX711 scale;

// OLED configuration
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);

void setup() {
  Serial.begin(9600);
  scale.begin(DOUT_PIN, CLK_PIN);

  u8g2.begin();
  u8g2.setFont(u8g2_font_ncenB14_tr);
}

void loop() {
  // Read the weight from the load cell
  float weight = scale.get_units(10); // Adjust the argument for your specific setup

  // Display weight on OLED
  displayWeight(weight);

  delay(1000); // Update every 1 second
}

void displayWeight(float weight) {
  u8g2.clearBuffer();
  u8g2.setCursor(0, 20);
  u8g2.print("Weight: ");
  u8g2.print(weight, 2); // Display weight with two decimal places
  u8g2.print(" kg");
  u8g2.sendBuffer();
}

but now the 0 weigh is shown as 25160 ...
I have recalibrated the weight and written the rate to the EEPROM.
so, how can I get this to 0 and have it shown on 2 lines with just gram or kg added (don't need the "weight")

so, I went a step further and now I have the following code:

#include "HX711.h"
#include <U8g2lib.h>
#include <EEPROM.h>

#define DOUT_PIN  4
#define CLK_PIN   5

HX711 scale;

// OLED configuration
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);

// Calibration factor, adjust this based on your load cell and setup
float calibrationFactor = 10122.0;

void setup() {
  Serial.begin(9600);
  scale.begin(DOUT_PIN, CLK_PIN);

  scale.set_scale(calibrationFactor);   // this value is obtained by calibrating the scale with known weights

  u8g2.begin();
  u8g2.setFont(u8g2_font_ncenB14_tr);

  // Read calibration factor from EEPROM
  EEPROM.get(0, calibrationFactor);

  // Zero the scale
  scale.tare();
}

void loop() {
  // Read the weight from the load cell
  float weight = scale.get_units(10); // Adjust the argument for your specific setup

  // Apply calibration factor
  weight = weight * calibrationFactor;

  // Display weight on OLED
  displayWeight(weight);

  delay(1000); // Update every 1 second
}

void displayWeight(float weight) {
  u8g2.clearBuffer();
  u8g2.setCursor(0, 20);
  u8g2.print(weight, 2); // Display weight with two decimal places
  u8g2.print(" g");
  u8g2.sendBuffer();
}

which seems to work.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.