// For: https://forum.arduino.cc/t/hx711-together-with-i2c-devices-on-same-pins/1068062/
// Using: https://randomnerdtutorials.com/esp32-load-cell-hx711/
// With library: "HX711 Arduino Library", https://github.com/bogde/HX711
//
// Not working !
// The HX711 with the library and the ESP32 does not work in Wokwi.
// Test here: https://wokwi.com/projects/351948174652342863
#include <Wire.h>
#include <HX711.h>
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = SDA;
const int LOADCELL_SCK_PIN = SCL;
HX711 scale;
enum
{
NONE,
I2C,
HX711
} mode;
void setup()
{
Serial.begin(115200);
Serial.print("Pin SDA = ");
Serial.print( SDA);
Serial.print(", Pin SCL = ");
Serial.print( SCL);
Serial.println();
mode = NONE;
// -----------------------------------------------------
// HX711
// The HX711 library uses pinMode, digitalRead, digitalWrite.
// It does not change something else for the ESP32 or the pins.
// -----------------------------------------------------
if( mode == NONE)
{
Serial.print("Testing HX711 ...");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); // data (DT), clock (SCK)
bool ready = scale.wait_ready_timeout(200,10); // 200ms, check every 10ms
if( ready)
{
Serial.println("... HX711 found.");
mode = HX711;
}
else
{
Serial.println("... nope, nothing found.");
pinMode( SDA, INPUT);
pinMode( SCL, INPUT);
delay( 100);
}
}
// -----------------------------------------------------
// I2C
// -----------------------------------------------------
if( mode == NONE)
{
Serial.print("Testing I2C ...");
Wire.begin(); // use default, SDA = 21, SCL = 22
// Run I2C Scanner, I'm using a few short lines instead here.
Wire.beginTransmission(0x68);
int error = Wire.endTransmission();
if( error == 0)
{
Serial.println("... Something found at 0x68.");
mode = I2C;
}
else
{
Serial.println("... nothing found on the I2C bus.");
Wire.end(); // Stop the I2C mode on the pins
}
}
}
void loop()
{
switch( mode)
{
case NONE:
delay(10);
break;
case I2C:
delay(10);
break;
case HX711:
if (scale.is_ready())
{
long reading = scale.read();
Serial.print("HX711 reading: ");
Serial.println(reading);
}
else
{
Serial.println("HX711 not found.");
}
break;
default:
break;
}
delay(800); // slow down the sketch
}