| 
 Raspberry pico+HX711 电子秤   【前言】 我做过多款电子秤,这次移植到pico上,显示屏采用复古的lcd-1602,均在micopython下完成。Hx711模块采用m5stack出品的模块,封装比较精致。 知识点:(1)lcd-1602(i2c)的micropython驱动 (2)hx711的micropython驱动。 难度系数:★☆☆☆☆ 有趣系数:★★☆☆☆  
  【硬件准备】 【软件准备】 序号  |  |  |  |   | Raspberry pico micropython固件  |  |  |   |  |  | https://Github.com/devonschafer/Raspberry_Pi_Pico_I2C_1602_LCD_16x2  |   |  |  | https://Github.com/SergeyPiskunov/micropython-hx711  |  
 【接线】   【代码及注释】  
 
			
			
			- #date:2021-04-16
 - #lcd1602 +hx711
 - #https://Github.com/devonschafer/Raspberry_Pi_Pico_I2C_1602_LCD_16x2
 - #https://Github.com/SergeyPiskunov/micropython-hx711
 - #It works
 - 
 - from hx711 import HX711
 - from utime import sleep_us,sleep
 - import machine, onewire, ds18x20, utime
 - from esp8266_i2c_lcd import I2cLcd
 - from machine import I2C, Pin
 - 
 - DEFAULT_I2C_ADDR = 0x27
 - # i2c init
 - i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
 - lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
 - # say hello and clear screen
 - lcd.putstr("Hello World !")
 - sleep(3)
 - lcd.clear()
 - lcd.move_to(1, 0)
 - 
 - print("scales begin...")
 - 
 -     
 - class Scales(HX711):
 -     def __init__(self, d_out, pd_sck):
 -         super(Scales, self).__init__(d_out, pd_sck)
 -         self.offset = 0
 - 
 -     def reset(self):
 -         self.power_off()
 -         self.power_on()
 - 
 -     def tare(self):
 -         self.offset = self.read()
 - 
 -     def raw_value(self):
 -         return self.read() - self.offset
 - 
 -     def stable_value(self, reads=10, delay_us=500):
 -         values = []
 -         for _ in range(reads):
 -             values.append(self.raw_value())
 -             sleep_us(delay_us)
 -         return self._stabilizer(values)
 -     
 -     @staticmethod
 -     def _stabilizer(values, deviation=10):
 -         weights = []
 -         for prev in values:
 -             weights.append(sum([1 for current in values if abs(prev - current) / (prev / 100) <= deviation]))
 -         return sorted(zip(values, weights), key=lambda x: x[1]).pop()[0]
 - 
 - #scales init
 - scales = Scales(d_out=5, pd_sck=4)
 - scales.tare() #setoff
 - 
 - while True:
 -     lcd.move_to(1, 0)
 -     val =int(scales.stable_value()*(-0.000482462)*100)/100
 -     lcd.putstr("Weight = ")
 -     lcd.putstr(str(val)) 
 -     print(val)
 -     sleep(1)
 
  复制代码
 【小结】 这是一个小玩具、也是实用器,面包板搭建,后续可以做一个外壳。 计算折算系数: 系数=100/(100g对应raw值-0g对应raw值) 本例题中,系数为-0.000482462。我用了一个100g砝码做了一个简单计算。玩家也可以用更好的方法标定,实际上形变传感器与温度也有关系,在不同季节应该做温度补偿,玩家感兴趣的话也可以进行尝试。 沧海抱拳。  
 代码和需要的库分享如下,库来自github.com,如果使用了别忘记给作者点个星星。谢谢。  
 |