[SOLVED] Unable to display the output of a load cell on OLED

Hello everyone and thank you in advance for any advice you can offer.

As part of a larger project, I’m trying to display the output from a load cell on an OLED display. I’m using a HX711 amplifier, a SH1106 OLED, and Arduino Uno.

I have very simple sketches for both the scale and OLED that work perfectly. Sadly, when I combine them, the program does not seem to run.

From my research it seems that this could be a memory issue and so I’ve stripped the code down as far as my current skills allow. I’ve also implemented a memory reporting function I found here and ,assuming the output is correct, can tell you that the HX711 sketch is using 193kb of RAM, and the OLED sketch 1497kb. Combined, this is 1690kb.

Sketch Goal:
Take readings from a calibrated load cell/HX711 combo and display that reading on an OLED display in conjunction with some static text.

Combined OLED and HX711 Sketch - Not working:

// Scale Libraries and setup

#include <HX711.h> 

#define DOUT  3
#define CLK  4
#define calibration_factor -970 // Calibration factor of scale

HX711 scale(DOUT, CLK);

// End scale libraries and setup


// OLED Libraries and setup

#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

// End OLED libraries and setup


// Variables

int loadCellValue = 1;
  
  
void setup() {   
  
  Serial.begin(9600);
  scale.set_scale(calibration_factor); 
  scale.tare();

}

void loop() {
  
  Serial.println(F("Entering Loop")); // Used to show loop has been entered
  
  loadCellValue = scale.get_units(); // Take reading from scale
  
  //Some Example Text 
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK, WHITE);
  display.setCursor(3,5);
  display.println(" Some Example Text ");
  display.println();
  display.setTextColor(WHITE); 
  display.setTextSize(3);
  display.setCursor(15,21);
  display.print(loadCellValue);
  display.print("/");
  display.print("20");
  display.display();
  delay(2000);

}

Working OLED Test Sketch

#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

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

  display.begin(SH1106_SWITCHCAPVCC, 0x3C); 
  
}

void loop() {
  
  int loadCellVal = 17; 
  
  Serial.println("Entering Loop"); // prove that loop has begun

// Some example text
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK, WHITE);
  display.setCursor(3,5);
  display.println(" Some Example Text ");
  display.println();
  display.setTextColor(WHITE); 
  display.setTextSize(3);
  display.setCursor(15,21);
  display.print(loadCellVal);
  display.print("/");
  display.print("20");
  display.display();
  delay(2000);
  
  Serial.println(freeRam());
}

int freeRam() {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

Working HX711/Load Cell Sketch

#include <HX711.h>

#define DOUT  3
#define CLK  4
#define calibration_factor -970 // Calibration factor of scale

HX711 scale(DOUT, CLK);

int loadCellValue = 1;

void setup() {   
  
  Serial.begin(9600);
  scale.set_scale(calibration_factor); 
  scale.tare();

}

void loop() {
  
  Serial.println("Entering Loop"); // prove that loop has begun
  
  Serial.println(loadCellValue);
  
  loadCellValue = scale.get_units();
  
  Serial.println(freeRam());
  
}

int freeRam() {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

I’d be grateful of any tips or advice as to how to further troubleshoot this problem, or at least a few keywords with which to continue googling!

Thanks in advance again,

Nick

wild arse guess but

(from example code)

This example is for a 128x64 size display using I2C to communicate
3 pins are required to interface (2 I2C and one reset)

(from your code)

#define DOUT 3
#define CLK 4

maybe pin 4 ??????

Thanks for the suggestion gpop1.

The display I'm using has an internal reset pin so only interfaces with two pins (plus voltage and ground).

That being said, in case it was causing conflict, I modified the sketch and circuit to use Pin 5 and tested the theory on my barebones scale sketch. The barebones sketch works great still, but the combined sketch does not...

ok lets try another wild guess

maybe this line is needed in set up to turn the display on

display.begin(SH1106_SWITCHCAPVCC, 0x3C);

WHOOP WHOOP!

Great catch! Looks like that’s all it was!

Thank you so much gpop1. I really appreciate it

For anyone else who’s curious, here is the newly working code:

// Scale Libraries and setup

#include <HX711.h> 

#define DOUT  3
#define CLK  5
#define calibration_factor -970 // Calibration factor of scale

HX711 scale(DOUT, CLK);

// End scale libraries and setup


// OLED Libraries and setup

#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

// End OLED libraries and setup


// Variables

int loadCellValue = 1;
  
  
void setup() {   
  
  Serial.begin(9600);
  scale.set_scale(calibration_factor); 
  scale.tare();
  
  display.begin(SH1106_SWITCHCAPVCC, 0x3C); // this was the missing line of code preventing this from working

}

void loop() {
  
  Serial.println(F("Entering Loop")); // Used to show loop has been entered
  
  loadCellValue = scale.get_units(); // Take reading from scale
  
  //Some Example Text 
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK, WHITE);
  display.setCursor(3,5);
  display.println(" Some Example Text ");
  display.println();
  display.setTextColor(WHITE); 
  display.setTextSize(3);
  display.setCursor(15,21);
  display.print(loadCellValue);
  display.print("/");
  display.print("20");
  display.display();
  delay(2000);

}

Hey! I am facing a similar problem if possible do help me out. I have three custom images for display and an I2C OLED display and I am using pins 2 & 3 for controlling the HX711 which works standalone but not with the OLED code integrated. I am using an arduino pro mini. The getunits() doesn’t seem to be working and i get only the word reading as output in the seial terminal

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

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

#define SSD1306_LCDHEIGHT 32
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

#define LOGO_HEIGHT 32
#define LOGO_WIDTH 128 
#define LEARN_HEIGHT 32
#define LEARN_WIDTH 128 
#define RUNA_HEIGHT 32
#define RUNA_WIDTH 128 

#define DOUT  3
#define CLK  2
#define calibration_factor -96530.00

const int runPin = 11;     // the number of the pushbutton pin
const int calibPin =  12;      // the number of the LED pin

int runState = 0;
int calibState = 0;

HX711 scale(DOUT,CLK);    // parameter "gain" is ommited; the default value 128 is used by the library

//Custom Images which work has been tested independently
static const unsigned char PROGMEM runa[] ={  matrix 1 };

static const unsigned char PROGMEM learn[] ={   matrix 2 };

static const unsigned char PROGMEM logo[] ={ matrix 3};

void setup()   
{                
   Serial.begin(38400);
   scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
   scale.tare();  //Assuming there is no weight on the scale at start up, reset the scale to 0
   pinMode(runPin, INPUT);
   pinMode(calibPin, INPUT);
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // init done
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  //display.display();
  //delay(2000);
  //Clear the buffer.
  //display.clearDisplay();
  }


void loop() 
{
  Serial.print("Reading: ");
  Serial.println(scale.get_units(5), 1); //scale.get_units() returns a float
  Serial.print("kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  
  runState = digitalRead(runPin);
  calibState = digitalRead(calibPin);
  display.clearDisplay();
  if(runState == 0) {
    display.clearDisplay();
    display.drawBitmap(0, 0,  runa, 128, 32, 1);  
    display.display();
  } 
  else if(calibState == 0)
  {
    display.clearDisplay();
    display.drawBitmap(0, 0,  learn, 128, 32, 1);  
    display.display();
  }
  else
  {
    display.clearDisplay();
    // below first two number equals stating pixel point,the three numbers next to logoname represent width, height and rotation
    display.drawBitmap(0, 0, logo, 128, 32, 1);
    display.display();
  }
  
  
}

Serial.println(scale.get_units(5), 1);What data type does get_units return?
If it returns an integer type, printing it in base 1 seems a bit pointless.

It returns a float value

I removed the extra definitions and it worked seems like the pin 2 was having a conflict. Thanks a lot

#define SSD1306_LCDHEIGHT 32
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

Why would you need to edit Adafruit_SSD1306.h, when the problem is in your sketch?

const int runPin = 11;     // the number of the pushbutton pin
const int calibPin =  12;      // the number of the LED pin

The comments are nonsense.

  pinMode(runPin, INPUT);
How ARE your switches wired? INPUT_PULLUP makes it so much easier to properly wire switches.