#embedded-hal-driver #embedded-hal #driver #hx711 #amplifier

hx711_spi

A platform agnostic driver to interface with the HX711 using SPI

4 releases (2 breaking)

Uses new Rust 2021

0.4.0 Apr 24, 2022
0.3.2 Apr 12, 2022
0.3.1 Jul 17, 2021
0.2.1 Apr 14, 2021

#145 in Embedded development

Download history 12/week @ 2022-06-16 12/week @ 2022-06-23 6/week @ 2022-06-30 14/week @ 2022-07-07 15/week @ 2022-07-14 18/week @ 2022-07-21 9/week @ 2022-07-28 27/week @ 2022-08-04 23/week @ 2022-08-11 9/week @ 2022-08-18 11/week @ 2022-08-25 9/week @ 2022-09-01 27/week @ 2022-09-08 11/week @ 2022-09-15 14/week @ 2022-09-22 17/week @ 2022-09-29

69 downloads per month

MIT/Apache

180KB
114 lines

hx711_spi

Crate API License Docs LOC Maintained dependency status GitHub Repo stars Crates.io crev reviews

This is a platform agnostic driver to interface with the HX711 load cell IC. It uses SPI instead of bit banging. This [no_std] driver is built using embedded-hal traits. It is recommended to always use cargo-crev to verify the trustworthiness of each of your dependencies, including this one.

Why did I write another HX711 driver?

In multi-user / multi-tasking environments bit banging is not reliable. SPI on the other hand handles the timing with hardware support and is not influenced by other processes.

Usage

Note: I'm using the reddefined SPI signal names ('see sparkfun's Resolution').

Use an embedded-hal implementation to get SPI and Delay. HX711 does not use SCLK, instead it is provided by the driver using SDI. Make sure that HX711 is the only device on the bus since it does not implemnt CS (Chip Select). Connect the SDO to the PD_SCK and SDI to DOUT of the HX711. SPI clock frequency has to be between 20 kHz and 5 MHz. Since the SPI clock is not used either SPI mode 0 or mode 1 should work. You need test which one gives the best results for you.

No scales functions (like tare weight and calibration) are implemented because I feel that's not part of a device driver. Power down functions exist just for compatibility. Implementation is not possible with this (ab-) use of SPI since the CPU / MPU would need to constantly send on the bus to power donwn the HX711. This would totally defy the purpose.

TODO

  • Test on more platforms
  • Power down (functions exist just for compatibility. Implementation is not possible with SPI)
  • Reset
  • [no_std]
  • make it re-entrant / thread safe

Examples

Raspberry PI

// embedded_hal implementation
use rppal::{spi::{Spi, Bus, SlaveSelect, Mode, Error},hal::Delay};

use hx711_spi::Hx711;
use nb::block;

// minimal example
fn main() -> Result<(), Error>
{
    let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 1_000_000, Mode::Mode0)?;
    let mut hx711 = Hx711::new(spi, Delay::new());

	  hx711.reset()?;
    let v = block!(hx711.read())?;
 	  println!("value = {}", v);

    Ok(())
}

STM32F1

An example stm32f103 (blue pill) initialization (note mode 1).

    use stm32f1xx_hal::time::U32Ext;
    use cortex_m_rt::entry;
    use stm32f1xx_hal::{pac, prelude::*,
      spi::{Mode, Phase, Polarity, Spi}, };

    let dp = pac::Peripherals::take().unwrap();
    let mut rcc = dp.RCC.constrain();
    let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
    let clocks = rcc.cfgr.freeze(&mut flash.acr);

    let hx711_spi_pins = (
        gpiob.pb13.into_alternate_push_pull(&mut gpiob.crh),
        gpiob.pb14.into_floating_input(&mut gpiob.crh),
        gpiob.pb15.into_alternate_push_pull(&mut gpiob.crh),
    );
    let hx711_spi = spi::Spi::spi2(device.SPI2, hx711_spi_pins, spi::MODE_1, 1.mhz(), clocks);
    let tim_delay = device.TIM1.delay::<1_000_000>(&clocks);
    let mut hx711_sensor = Hx711::new(hx711_spi, tim_delay);
    hx711_sensor.reset().unwrap();
    hx711_sensor.set_mode(hx711_spi::Mode::ChAGain128).unwrap(); // x128 works up to +-20mV

Feedback

All kind of feedback is welcome. If you have questions or problems, please post them on the issue tracker This is literally the first code I ever wrote in rust. I am still learning. So please be patient, it might take me some time to fix a bug. I may have to break my knowledge sound-barrier. If you have tested on another platform I'd like to hear about that, too!

Big thanx to 'jbit' for clearing the question about thread safety and 'anddreyk0' for testing on STM32 and both for debugging!

References

License

Licensed under either of

at your option.

Dependencies

~0.8–1.2MB
~26K SLoC