# đọc thanh ghi
b = bus.read_word_data(addr, CONVERSIONREG)
# hoán đổi hai byte
b = ((b & 0xFF) << 8) | ((b >> 8) & 0xFF)
# trừ một nửa phạm vi để đặt tiếp đất về 0
b -= 0x8000
# Chia kết quả theo phạm vi để có giá trị nằm giữa 0 và 1
b /= TOP
# giới hạn ở 1
b = min(b, 1)
# phía dưới cùng là 0
b = max(b, 0)
# hai vị trí thập phân
b = round(b, 2) print(b) time.sleep(.01)
You're almost done. Map the range of values you're getting to the value you like, then truncate the desired number of decimal places. You can adjust the print function to only print a new value when it differs from the last value.
This is the inherent disadvantage of using 16 bits instead of just 10 bits: a little bit of noise is more noticeable.
By connecting the adjacent input (input 1) to ground and switching the mode so that you can compare inputs one and two, you can get much more stable results. You can also swap out those noise-picking interconnect cables for small cables and add a few capacitors while you're at it. Your potentiometer value can also make a difference.
Additionally, you have software options. You can create a rolling average, or simply ignore small changes. The disadvantage is that the additional code will cause computational overhead. If you're writing conditional statements in a high-level language like Python and taking thousands of samples per second, these costs add up quickly.
It's done! Hope this article is useful to you!