jbit / hx711_spi

[Development Fork] This is a library for the hx711 chip. It uses SPI instead of bit banging for more reliability.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hx711_spi

Crate API

License

Maintained dependency status GitHub Repo stars Crates.io

This is a platform agnostic driver to interface with the HX711 load cell IC. It uses SPI instead of bit banging. This driver is built using embedded-hal traits.

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.

What works

(tested on Raspberry Pi)

  • Reading results
  • Setting the mode (gain and channel)

No scales functions (like tare weight and calibration) are implemented because I feel that's not part of a device driver.

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

Usage

Use an embedded-hal implementation (e. g. rppal) to get SPI. HX711 does not use CS and SCLK. Make sure that it is the only device on the bus. 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.

// 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(())
}

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

    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!

References

License

Licensed under either of

at your option.

About

[Development Fork] This is a library for the hx711 chip. It uses SPI instead of bit banging for more reliability.

License:MIT License


Languages

Language:Rust 100.0%