Help needed with ESPHome config for M5Stack Mini Scale

Hi Guys,

I picked up one of these mini scales recently, Ive been trying to add it to one of my ESP32 devices but I’m really struggling to understand how to achieve this.

The device communicates over the i2c bus, my code is compiling and it identifies the scale on the address it is expected to be on.

My question is how do I then turn this into a sensor.

I did find an article on the ESPHome website suggesting that the hx711 device can be used as a sensor however there is no reference how I can make it communicate with the i2c bus.

esphome:
  name: bedroom-home-status-light
  friendly_name: Bedroom Home Status Light

esp32:
  board: m5stack-atom
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "*="

ota:
  - platform: esphome
    password: "*"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bedroom-Home-Status-Light"
    password: "*"
    
light:
  - platform: fastled_clockless # 5x5 RGB Matrix (WS2812C)
    name: "Light"
    pin: 27
    chipset: WS2812
    num_leds: 25
    rgb_order: GRB
    effects:
      - pulse:
          name: "Fast Pulse"
          transition_length: 0.5s
          update_interval: 0.5s
          min_brightness: 0%
          max_brightness: 100%

i2c:
  - id: bus_a
    sda: 25
    scl: 21
    scan: true

I did attempt the following but this failed to compile:

sensor:
  - platform: hx711
    name: "HX711 Value"
    update_interval: 60s
    i2c_id: bus_a
    address: 0x26

Any ideas?

Hi!

were you able to solve this? I have an hx711 connected to a m5Stack poesp32. The scale gets detected on address 0x26 but I can’t read it.

If you are using those mini scales, even if hx711 is one component of that scale, connection is done by STM32 microcontroller through I2C and it has nothing to do with hx711 serial protocol.

Thanks, you’re right.

So I want to use the i2c read_register functionality to get the weight from the scale. Any ideas on how to make this work?

i2c:
  - id: bus_a
    sda: GPIO16
    scl: GPIO17
    scan: true

i2c_device:
   id: i2cdev
   address: 0x26

sensor:
  - platform: custom
    lambda: |-
        uint8_t* weight=0;
        if (id(i2cdev).read_register(0x60,weight,4,false)) {
        return {weight};
        }
    sensors:
      - name: weight
        id: weight
        

Because now I get this error when compiling:

Compiling .pioenvs/poesp32/src/main.cpp.o
/config/esphome/poesp32.yaml: In lambda function:
/config/esphome/poesp32.yaml:43:21: error: could not convert '{weight}' from '<brace-enclosed initializer list>' to 'std::vector<esphome::sensor::Sensor*>'
         return {weight};
                     ^
*** [.pioenvs/poesp32/src/main.cpp.o] Error 1

No, but you can study this:

1 Like

Well, I figured it out! (And added the Tare Button)

This reads the weight in grams every second.

i2c:
  - id: bus_a
    sda: GPIO16
    scl: GPIO17
    scan: true

i2c_device:
   id: i2cdev
   address: 0x26

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;

button:
  - platform: template
    id: SetOffset
    name: "Tare"
    on_press:
      then:
        - lambda: |-
            id(i2cdev).write_byte(0x50, 1);

I got the code from the M5Stack sample here , from the getWeight function:

image

2 Likes

Nice job !

1 Like