Hx711 and esp32 issues

I am using the Adafruit ESP32-S3 Reverse TFT Feather (product 5691), with Adafruit HX711 break out (product 5974), and Adafruit Strain Gauge Load Call 5kg (product 4541).

I am trying to build a scale and have the output displayed on the Feather's TFT, using the Arduino 1.8.9 IDE. I have been able to read the load cell without problems when the results are limited to the serial monitor. See code below. I have also been able to display test numbers on the TFT without problems as long as I don't call on the HX711. See code below. The problem is that when I try to both read the HX711 and then display the output on the TFT, something is compromising the results and I get nothing but random numbers. Can anyone help me understand what the problem is? Thank you!

This code works as expected, with valid load scale results shown in the serial monitor:

#include "Adafruit_HX711.h"
#include "Arduino.h"
const uint8_t DATA_PIN = A4; 
const uint8_t CLOCK_PIN = A5; 
Adafruit_HX711 hx711(DATA_PIN, CLOCK_PIN);
float BU=0;

void setup() {
  Serial.begin(115200);
  while (!Serial) {delay(10);}
  
  Serial.println("Adafruit HX711 Test!");
  hx711.begin();
  Serial.println("Tareing....");
  for (uint8_t t=0; t<3; t++) 
  { hx711.tareA(hx711.readChannelRaw(CHAN_A_GAIN_128));
    hx711.tareA(hx711.readChannelRaw(CHAN_A_GAIN_128));
    hx711.tareB(hx711.readChannelRaw(CHAN_B_GAIN_32));
    hx711.tareB(hx711.readChannelRaw(CHAN_B_GAIN_32));}}
             
void loop() {

  int32_t weightA128 = hx711.readChannelBlocking(CHAN_A_GAIN_128);
  BU=weightA128/100000.0;
  Serial.print("Channel A (Gain 128): ");
  Serial.println(abs(BU),1);}

This code also works just fine, displaying the test number 2.7 on the screen as expected:

#include <Adafruit_GFX.h>    
#include <Adafruit_ST7789.h> 
#include <SPI.h>
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

void setup(void) {
   pinMode(TFT_BACKLITE, OUTPUT);
  digitalWrite(TFT_BACKLITE, HIGH);
  pinMode(TFT_I2C_POWER, OUTPUT);
  digitalWrite(TFT_I2C_POWER, HIGH);
  delay(10);
  tft.init(135, 240); // Init ST7789 240x135
  tft.setRotation(3);
  tft.fillScreen(ST77XX_BLACK);}

void loop() {
  tft.setTextWrap(false);
  tft.setCursor(30, 30);
  tft.setTextColor(ST77XX_WHITE);
  tft.setTextSize(10);
  tft.println("2.7");
  delay(10000);}

When I try to combine the code above and display the HX711 results on the TFT, however, I get random numbers for output. Here is the code for that:

#include <Adafruit_GFX.h>    
#include <Adafruit_ST7789.h> 
#include <SPI.h>
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

#include "Adafruit_HX711.h"
#include "Arduino.h"
const uint8_t DATA_PIN = A4; 
const uint8_t CLOCK_PIN = A5; 
Adafruit_HX711 hx711(DATA_PIN, CLOCK_PIN);
float BU=0;

void setup(void) {

Serial.begin(115200);
  while (!Serial) {delay(10);}
  
  Serial.println("Adafruit HX711 Test!");
  hx711.begin();
  Serial.println("Tareing....");
  for (uint8_t t=0; t<3; t++) 
  { hx711.tareA(hx711.readChannelRaw(CHAN_A_GAIN_128));
    hx711.tareA(hx711.readChannelRaw(CHAN_A_GAIN_128));
    hx711.tareB(hx711.readChannelRaw(CHAN_B_GAIN_32));
    hx711.tareB(hx711.readChannelRaw(CHAN_B_GAIN_32));}

  
  pinMode(TFT_BACKLITE, OUTPUT);
  digitalWrite(TFT_BACKLITE, HIGH);
  pinMode(TFT_I2C_POWER, OUTPUT);
  digitalWrite(TFT_I2C_POWER, HIGH);
  delay(10);
  tft.init(135, 240); // Init ST7789 240x135
  tft.setRotation(3);
  tft.fillScreen(ST77XX_BLACK);}

void loop() {
  int32_t weightA128 = hx711.readChannelBlocking(CHAN_A_GAIN_128);
  BU=weightA128/100000.0;
  Serial.print("Channel A (Gain 128): ");
  Serial.println(abs(BU),1);
  tft.setTextWrap(false);
  tft.fillScreen(ST77XX_BLACK);
  tft.setCursor(30, 30);
  tft.setTextColor(ST77XX_WHITE);
  tft.setTextSize(10);
  tft.println(BU,1);
  delay(100);}

I'm not familiar with that library/syntax, but if you are neither, try without ,1

It inherits println() from the Stream class, and prints a float with 1 decimal point.

1 Like

Well, in case helpful to anyone else going forward, I was able to fix the issue by switching HX711 libraries to the one done by bodge on github.

GitHub - bogde/HX711: An Arduino library to interface the Avia Semiconductor HX711 24-Bit Analog-to-Digital Converter (ADC) for Weight Scales.

The ESP32 is apparently too fast for the HX711 and can pulse the clock signal faster than the HX711 can handle. This library fixes that. I had read about this issue elsewhere, but thought it was inapplicable to me since my set up was working when the TFT was not involved. Not sure why using the TFT triggers the issue, but I can confirm everything works as expected when using the HX711.h library linked above.

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