feature-requests icon indicating copy to clipboard operation
feature-requests copied to clipboard

HX711 via I2C (m5stack)

Open PineappleEmperor opened this issue 1 year ago • 7 comments

Describe the problem you have/What new integration you would like Support HX711 sensors via I2C.

Please describe your use case for this integration and alternatives you've tried: I currently use a lot of m5stack ESP32 gear, and would like to use the m5stack HX711 (along with other sensors) which uses I2C. At present, the HX711 sensor requires dout_pin and clk_pin. I've looked to see if I can directly use arduino code, but this is a messy workaround and I'd prefer to keep to native ESPHome if possible.

Additional context I'm new (and very amateur) with ESPHome, so if I'm missing something obvious please let me know!

PineappleEmperor avatar Sep 28 '24 12:09 PineappleEmperor

same here

Eyal51 avatar Oct 14 '24 23:10 Eyal51

Bump. Just got this thing and am getting into making it work.

HeathersZen avatar Oct 19 '24 00:10 HeathersZen

FWIW, I managed to get the M5 Atom stack to return a value by following this video: https://www.youtube.com/watch?v=GlIS2HsSdWY

The only difference I can tell from my original attempt was that I flashed ESP Home to the device using the https://web.esphome.io/ website instead of the HAOS add-in.

I haven't calibrated it yet, so I may yet run into issues, but here's the YAML that is currently working. I haven't bothered to clean it up yet.

substitutions: name: esphome-web-a03858 friendly_name: Aeroponica-Scale1

esphome: name: ${name} friendly_name: ${friendly_name} min_version: 2024.6.0 name_add_mac_suffix: false project: name: esphome.web version: dev

esp32: board: esp32dev framework: type: arduino

Enable logging

logger:

Enable Home Assistant API

api:

Allow Over-The-Air updates

ota:

  • platform: esphome

Allow provisioning Wi-Fi via serial

improv_serial:

wifi:

Set up a wifi access point

ap: {}

In combination with the ap this allows the user

to provision wifi credentials to the device via WiFi AP.

captive_portal:

dashboard_import: package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main import_full_config: true

Sets up Bluetooth LE (Only on ESP32) to allow the user

to provision wifi credentials to the device.

esp32_improv: authorizer: none

To have a "next url" for improv serial

web_server: port: 80

sensor:

  • platform: hx711 name: "HX711 Value" dout_pin: GPIO32 clk_pin: GPIO26 gain: 128 update_interval: 60s

HeathersZen avatar Oct 19 '24 01:10 HeathersZen

If that's working for you then you're not using a i2c device.

Eyal51 avatar Oct 19 '24 16:10 Eyal51

I’m using the M5Stack unit. Perhaps it isn’t the one OP is referring to.Scale Kit with Weight Unitshop.m5stack.comOn Oct 19, 2024, at 9:09 AM, Eyal51 @.***> wrote: If that's working for you then you're not using a i2c device.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

HeathersZen avatar Oct 19 '24 18:10 HeathersZen

I believe we're referring to this one: https://shop.m5stack.com/products/mini-scales-unit-hx711

From chatting with the devs, this would require code changes to ESPHome.

ScottG489 avatar Oct 21 '24 16:10 ScottG489

I believe we're referring to this one:

https://shop.m5stack.com/products/mini-scales-unit-hx711

From chatting with the devs, this would require code changes to ESPHome.

Yeah that's the one I was referring to. I had assumed the hx711 component would need changing, but similar to other components that can be used with I2C OR direct pins?

PineappleEmperor avatar Oct 21 '24 16:10 PineappleEmperor

protocol-d35ec78e-c48e-44ae-93b5-ddb10418ce88.png

Is this it?

DjordjeMandic avatar Jan 23 '25 01:01 DjordjeMandic

If you cut a grove cable and put the grove side into your HX711 and the wires into the right side of an Atom Lite (= i2c), it will work. At least, it does with me. :) Yellow=G(PIO)21, white=G(PIO)25, red=5V(olt), black=G(round).

Use the following code (see and also):

# i2c communication
i2c:
  - id: bus_a
    sda: GPIO25
    scl: GPIO21
    scan: true

i2c_device:
   id: i2cdev
   address: 0x26

# Returns actual sensor data (sends data every second)
sensor:
- platform: template
  name: "HX711 weight Sensor"
  update_interval: 1s
  unit_of_measurement: "g"  
  device_class: "weight"
  state_class: "measurement"
  lambda: |-
    uint8_t data[4];
    float c;
    uint8_t *p;
    id(i2cdev).read_bytes(0x10,data,4);
    p = (uint8_t *)&c;
    memcpy(p, data, 4);    
    return c;

# Button in Hassio to "calibrate" (sensor is set ~0)
button:
  - platform: template
    id: SetOffset
    name: "HX711 calibrate"
    on_press:
      then:
        - lambda: |-
            id(i2cdev).write_byte(0x50, 1);

# HX711 button in Hassio (only returns on/off)
binary_sensor:
  - platform: template
    name: "HX711 button"
    icon: "mdi:radiobox-blank"
    lambda: |-
      uint8_t data = 0;
      id(i2cdev).read_bytes(0x20,&data,1);
      return data==0;

regencape avatar Jan 24 '25 09:01 regencape

Thanks, this worked great for me! Only difference was the GPIO for the i2c was 26 and 32 respectively for me. I'm using a grove cable plugged into the grove connector on a Atom Lite and the device.

ScottG489 avatar Feb 19 '25 23:02 ScottG489

@regencape This is the only article I can find anywhere relating to using I2C with the M5stack scales. I was wondering if you had any advice on this? I have currently tried your code and I get no errors but I only get a reading of 0 grams.

I am using the M5stack Scales Kit but I have replaced the transmitter with the Unit Weight-I2C which is slightly different to what everyone else on this thread is using. Worth a shot I guess.

Unit Weight-I2C: https://docs.m5stack.com/en/unit/Unit-Weight%20I2C

Scales Kit: https://docs.m5stack.com/en/app/scales_kit

Thanks in advance

Tirccir avatar May 22 '25 21:05 Tirccir

Is there a reason you did that replacement? I think the HX711 that comes with the scales kit is already supported: https://esphome.io/components/sensor/hx711.html

ScottG489 avatar May 23 '25 22:05 ScottG489

@ScottG489 You're correct, it is already supported. I have changed it because I wanted to connect via I2C as I have multiple scales connected to my ESP32 and I need to free up some GPIO pins. I have it working fine with the HX711 using the SDA/SCL

Tirccir avatar May 24 '25 00:05 Tirccir

@Tirccir I'm not really the expert. Key thing is to have the ports and addresses right. The only thing I can think of that might cause the issue is the following: Unit Weight-I2C SCL (white) SDA (yellow) Scales Kit SCL (yellow) SDA (white) Mini Scales Unit (HX711) SCL (white) SDA (yellow)

But this hardly seems the issue since the SCL and SDA in the Unit Weight-I2C is similar to Mini Scales Unit. But maybe something happens I don't understand either. The schematics on https://docs.m5stack.com/en/app/scales_kit and https://docs.m5stack.com/en/unit/Unit-Weight%20I2C might help, but that's way over my head. :)

regencape avatar May 24 '25 12:05 regencape

@regencape How did I not see that?! You are right, the SDA/SCL are opposite. I am now getting a reading, thank you very much! Now the next problem... It's reading 450g for a 2.5kg weight haha. I had noticed that in your code, you didn't calibrate it? Something I'll have to read further into

Tirccir avatar May 24 '25 13:05 Tirccir

That should be possible to fix by playing around with some lamba code. We already have the code shown down here. Actually I've no idea what all this lamba code means, but I do know that it returns "c". Apparently "c" should be 2500/450 times bigger, right? That's 5,5555555556.

sensor:
- platform: template
  name: "Weight Sensor"
  update_interval: 1s
  unit_of_measurement: "g"  
  device_class: "weight"
  state_class: "measurement"
  lambda: |-
    uint8_t data[4];
    float c;
    uint8_t *p;
    id(i2cdev).read_bytes(0x10,data,4);
    p = (uint8_t *)&c;
    memcpy(p, data, 4); 
    return c;

As said, I'm not the expert, but according to o3-mini LLM (chatbot) we should replace it with the following:

return c * 5,5555555556;

Hope it works. :)

regencape avatar May 24 '25 13:05 regencape

Ohh by the way, there is a calibrate button, but that button only sets it zero If I remember correctly. I hope the difference the actual weight and the reading is lineair. Otherwise you'll have to make up some nice formula and ask some LLM how to incorporate it with lamba. :)

regencape avatar May 24 '25 13:05 regencape

Yeah I added all your code (nicely done btw!) and yes it does calibrate to zero which does work. Your suggestion of adding "return c * X" also works but the fluctuations are also exaggerated. There seems to be some serious drift going on. So I may need to look into the "nice formula" xD.

I followed an example here: https://github.com/markusressel/ESPHome-Smart-Scale/tree/master which directly interfaces with the hx711 and it does combat the drift quite well. I am no expert either so no worries, so thank you for all your suggestions, it's been really helpful :)

Tirccir avatar May 24 '25 16:05 Tirccir