🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    M5Stack MiniScale - can it work with Python using I2C?

    Units
    2
    2
    579
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • X
      xpackers
      last edited by

      I have been trying for some time to get the M5 MiniScale unit working with MicroPython using the I2C protocol as published with the product listing. I am using Raspberry Pico microcontrollers.

      The process seems quite straightforward. I am resetting the offset and calculating the GAP as described. I have an accurate 100g calibration weight and have experimented with all the filter settings.

      I have three identical M5Stack MiniScale units, all bought new, but I have never been able to get any of them to zero correctly which as it stands makes them all pretty much useless for my intended purpose.

      The Unit-Mini Scales product page (SKU:U177) says that the unit is suitable for scientific experimentation (as such 0.01g resolution would not be unreasonable) and it also states Python as a programming platform.

      I can't however find any information or Pyhton examples regarding this unit having searched high and low and before I conclude I may be wasting my time thought I'd post here to see if anyone has ever managed to get this unit working accurately, has experienced similar frustrations or may be able to point me in the right direction?

      A 1 Reply Last reply Reply Quote 0
      • A
        alien @xpackers
        last edited by

        Hi @xpackers,
        i can help you with the micropython code for the miniScale unit.
        regarding the accuracy of the unit i have the same problem, its drifts a lot over time with or without calibration, i try to use the set_offset() function before each use but its useless over time.
        tried to take it a part and tighten the screws a little but also the same problem

        this is the miniUnit.py

        from machine import I2C, Pin
        import time
        import struct

        Constants

        DEVICE_DEFAULT_ADDR = 0x26

        Scale Registers

        UNIT_SCALES_RAW_ADC_REG = 0x00
        UNIT_SCALES_CAL_DATA_REG = 0x10
        UNIT_SCALES_BUTTON_REG = 0x20
        UNIT_SCALES_RGB_LED_REG = 0x30
        UNIT_SCALES_SET_GAP_REG = 0x40
        UNIT_SCALES_SET_OFFSET_REG = 0x50
        UNIT_SCALES_CAL_DATA_INT_REG = 0x60
        UNIT_SCALES_CAL_DATA_STRING_REG = 0x70
        UNIT_SCALES_FILTER_REG = 0x80
        JUMP_TO_BOOTLOADER_REG = 0xFD
        FIRMWARE_VERSION_REG = 0xFE
        I2C_ADDRESS_REG = 0xFF

        class UnitScales:
        def init(self, sda=21, scl=22, addr=DEVICE_DEFAULT_ADDR):
        self._i2c = I2C(0, scl=Pin(scl), sda=Pin(sda), freq=400000)
        self._addr = addr

        def write_bytes(self, reg, data):
            self._i2c.writeto(self._addr, bytes([reg]) + data)
        
        def read_bytes(self, reg, length):
            self._i2c.writeto(self._addr, bytes([reg]))
            return self._i2c.readfrom(self._addr, length)
        
        def begin(self):
            try:
                self._i2c.readfrom(self._addr, 1)
                return True
            except OSError:
                return False
        
        def get_btn_status(self):
            data = self.read_bytes(UNIT_SCALES_BUTTON_REG, 1)
            return data[0]
        
        def set_led_color(self, color_hex):
            color = bytearray(3)
            color[0] = (color_hex >> 16) & 0xff  # RED
            color[1] = (color_hex >> 8) & 0xff   # GREEN
            color[2] = color_hex & 0xff           # BLUE
            self.write_bytes(UNIT_SCALES_RGB_LED_REG, color)
        
        def get_led_color(self):
            color = self.read_bytes(UNIT_SCALES_RGB_LED_REG, 3)
            return (color[0] << 16) | (color[1] << 8) | color[2]
        
        def get_weight(self):
            data = self.read_bytes(UNIT_SCALES_CAL_DATA_REG, 4)
            return struct.unpack('<f', data)[0]
        
        def get_weight_int(self):
            data = self.read_bytes(UNIT_SCALES_CAL_DATA_INT_REG, 4)
            return int.from_bytes(data, 'little')
        
        def get_weight_string(self):
            data = self.read_bytes(UNIT_SCALES_CAL_DATA_STRING_REG, 16)
            return ''.join([chr(b) for b in data if b != 0])
        
        def get_gap_value(self):
            data = self.read_bytes(UNIT_SCALES_SET_GAP_REG, 4)
            return struct.unpack('<f', data)[0]
        
        def set_gap_value(self, offset):
            data = struct.pack('<f', offset)
            self.write_bytes(UNIT_SCALES_SET_GAP_REG, data)
        
        def set_offset(self):
            self.write_bytes(UNIT_SCALES_SET_OFFSET_REG, bytearray([1]))
        
        def get_raw_adc(self):
            data = self.read_bytes(UNIT_SCALES_RAW_ADC_REG, 4)
            return int.from_bytes(data, 'little')
        
        def set_i2c_address(self, addr):
            self.write_bytes(I2C_ADDRESS_REG, bytearray([addr]))
            self._addr = addr
            return self._addr
        
        def get_i2c_address(self):
            data = self.read_bytes(I2C_ADDRESS_REG, 1)
            return data[0]
        
        def get_firmware_version(self):
            data = self.read_bytes(FIRMWARE_VERSION_REG, 1)
            return data[0]
        
        def jump_bootloader(self):
            self.write_bytes(JUMP_TO_BOOTLOADER_REG, bytearray([1]))
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post