fivesixzero / CircuitPython_HX711

A CircuitPython driver for the HX711 load cell amplifer and ADC
MIT License
5 stars 1 forks source link

PIO constructor type mismatch #5

Open utdrmac opened 1 year ago

utdrmac commented 1 year ago

Per suggestions in #4, I'm trying to use the PIO version with my PicoW:

# HX711 Setup
gpio_data = digitalio.DigitalInOut(board.GP18)
gpio_clk = digitalio.DigitalInOut(board.GP19)

hx = HX711_PIO(gpio_data, gpio_clk, tare=False, offset=-155118, scalar=8949.0)

Error when running:

Traceback (most recent call last):
  File "<stdin>", line 50, in <module>
  File "hx711/hx711_pio.py", line 87, in __init__
  File "hx711/hx711_pio.py", line 102, in sm_init
TypeError: first_in_pin must be of type Pin, not DigitalInOut

The constructor for HX711_PIO wants a DigitalInOut but rp2pio.StateMachine wants a Pin (https://docs.circuitpython.org/en/latest/shared-bindings/rp2pio/index.html)

fivesixzero commented 1 year ago

Oof, looks like I added type hints without double-checking and re-testing the PIO lib back in the day. 🤦

I should be able to get this changed, tested, and committed to the repo within the next 24 hours or so.

fivesixzero commented 1 year ago

Although the constructor's type hints are incorrect (whoops), I did just run a test that worked fine as long as the board's Pin objects themselves are just passed through without using DigitalInOut at all, as in the PIO-mode example:

https://github.com/fivesixzero/CircuitPython_HX711/blob/4efc13f5e69f482d9b43936077e19e5fbeb7f285/examples/hx711_pio_simpletest.py#L8-L11

Give that a shot and let me know how it goes. I tested on a PicoW running the latest stable CircuitPython release (8.0.5) using board.GP18 and board.GP19. Give that a shot and let me know if it works out.

Those type hints definitely need fixing though, so I'll be getting that addressed ASAP.

utdrmac commented 1 year ago

Hey @fivesixzero I finally got a chance to test this and switching my code as follows results in all 0 for data:

**
** GPIO
from hx711.hx711_gpio import HX711_GPIO
# HX711 Setup
gpio_data = digitalio.DigitalInOut(board.GP19)
gpio_clk = digitalio.DigitalInOut(board.GP18)

# Initialize the HX711 ADC
# Don't tare on startup as the offset and scalar values are already calibrated
hx = HX711_GPIO(gpio_data, gpio_clk, tare=False, offset=-155118, scalar=8949.0)

**
** PIO
from hx711.hx711_pio import HX711_PIO
# HX711 Setup
gpio_data = board.GP19
gpio_clk = board.GP18

# Initialize the HX711 ADC
# Don't tare on startup as the offset and scalar values are already calibrated
hx = HX711_PIO(gpio_data, gpio_clk, tare=False, offset=-155118, scalar=8949.0)

Both are used as such:

    # Take 20 ADC readings, which are averaged
    reading = hx.read(20)
    reading_raw = hx.read_raw()

Both variables are then pushed to mqtt into influxdb. The GPIO version gives values whereas the PIO version gives 0s for both.