olkal/HX711_ADC

Significant slowdown in data reading speed. HX711_ADC.cpp#L347

lonelymyp opened this issue · 5 comments

dout = digitalRead(doutPin);

The slow digitalRead() function is used 24 times in a row.
This is important for using the sensor in dynamic systems, for example, to disable the mechanism when the load is exceeded.
It would be great to use a fast alternative or bit operations

olkal commented

Hi!
Yes, digitalRead() is quite slow and digitalWrite() is even slower compared to direct port manipulation. However, I have no plans to implement fast IO read/write functions for several reasons:

  • It's much more convenient to use the original Arduino functions for simplicity and best possible compatibility across different board/mcu platforms.
  • Keep in mind that we are only clocking out 24 bits at the time, it's not like e.g. writing to a graphical display with several thousand bits for each transmission.
  • Reading out the conversion value with the 16mhz Arduino Uno takes about 410us. At 10SPS rate that's only 0.4% of your total processing time. With a faster mcu it's much less.
  • There are limits for how fast we can go; the HX711 data sheet specifies SCK timing of minimum 0.2us high/low. Using a fast MCU like the ESP32 the digitalRead() and digitalWrite() functions are already too fast for the HX711 timing and we have to use a delay in between each read/write operation.

Olav

Well, I have just such a case, so I decided to write about this problem :)
I used the sensor to track changes in pressure force in real time and noticed that the sensor reacts sluggishly. I had to change the library code to make it work faster.
By the way, 10 sps is very small, the sensor can output up to 140 sps with an external clock =)

little funny picture:
image

olkal commented

Yes sure, you can optimize a bit by using fast IO, but even with high rate and external clock the conversion itself will still take a lot more time than the readout, so it's only usefull for very time critical projects.

If you run into HX711 timing limit and wish to optimize further you could also replace the optional delayMicroseconds() code with an ASM NOP delay, more fine tuned for your mcu speed.

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.