【发布时间】:2021-12-02 22:03:53
【问题描述】:
我目前正在从事一个物联网项目,我正在尝试将我的 Raspberry Pi 3 连接到 HX711,以便我可以读取来自 200 公斤范围的称重传感器的重量读数。
对于 Python 代码,我尝试了this Python library from github
根据这个存储库的描述,我首先使用 5 公斤的已知重量校准了 HX711 (calibration.py),给出了偏移量和比例。之后我将它们复制并在example_python3.py 中使用。
但我不断从称重传感器获得可变读数,如以下来自 Raspberry Pi 窗口的屏幕截图所示:
我通过放置 5 公斤负载来获得此输出。我尝试了这个校准循环并多次检查输出,但我的输出仍然是可变的。
这是我使用的代码:
import RPi.GPIO as GPIO
import time
import sys
from hx711 import HX711
# Force Python 3 ###########################################################
if sys.version_info[0] != 3:
raise Exception("Python 3 is required.")
############################################################################
GPIO.setwarnings(False)
hx = HX711(5, 6)
def cleanAndExit():
print("Cleaning...")
GPIO.cleanup()
print("Bye!")
sys.exit()
def setup():
"""
code run once
"""
#Pasted Offset and Scale I got from calibration..
hx.set_offset(8608276.3125)
hx.set_scale(19.828315054835493)
def loop():
"""
code run continuosly
"""
try:
val = hx.get_grams()
print(val)
hx.power_down()
time.sleep(0.001)
hx.power_up()
except (KeyboardInterrupt, SystemExit):
cleanAndExit()
##################################
if __name__ == "__main__":
setup()
while True:
loop()
【问题讨论】:
-
欢迎来到 Stack Overflow。 Please do not upload images of code/errors when asking a question.。你能edit你的问题,用文字代替图片吗?
-
以下信息有用吗?
标签: python raspberry-pi3 sensors