Use SPI.transfer to communicate with HX711

Hi All,

I am currently looking into an issue where the HX711 can go into sleep mode, resulting in bad data being received by the boron. See thread below:

@Joel suggested using the actually SPI and SPI.transfer to attempt to communicate to the HX711 via the hardware SPI. I have done some research but am at a bit of a loss as to where to start.

Will I need to make use of the:

SPI.setDataMode(SPI_MODE2);

command and is SPI_MODE2 the mode that I should use. Also in the actual SPI.transfer command:

SPI.transfer(tx_buffer, rx_buffer, length, myFunction);

Am I correct in saying that tx and rx are byte arrays. Do I just fill the outgoing array with dummy data and read the incoming array after calling the .transfer function?

From what I have gathered the myFunction callback can be used to ensure that the comms process has been completed, for this to work do I just return a bool from myFunction and wait for that value?

Thanks.

I'd have to look into the library to brush up on the inner workings of the sensor but ...

You are correct for the fist part, but you don't need to preset a TX buffer you can just use NULL for that if you don't have any data to send to an SPI client.
That's also stated in the docs :wink:
https://docs.particle.io/reference/device-os/firmware/boron/#transfer-void-void-size_t-std-function-

The docs also explain that.
When you provide NULL as callback function the call will be synchronous - this means the function will not return unless length bytes have been received.
If you want the call to be asynchronous the callback will be called when all data has been received and therein you can deal with the just received data however you wish (including just setting a flag which may then "instruct" loop() to deal with the data).

1 Like

Perfect I’ll give this a go. Thanks for the pointers @ScruffR, and not just in this thread. I’ll post again with either a solution, further findings or the next time I get stuck.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.