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]))