Skip to content

ricardocosme/hx711

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HX711 C++11/17/20 driver for AVR-8

The HX711 is a high-resolution 24-bit ADC designed for weight scales and other applications that interface with a bridge sensor. This is a header only C++11/17/20 driver to interface with HX711 using an ATtiny85/13A or ATmega328P.

Demo

The demos below are written in C++17, take a look at demo/c++11 to view the versions with support to C++11.

Synchronous reading

#include <hx711.hpp>

int main() {
  hx711::adc adc{avr::io::pb4, avr::io::pb3};
  auto code = adc.read();
}

demo/c++17/read.cpp

Asynchronous reading

#include <hx711.hpp>

int main() {
  hx711::adc adc{avr::io::pb4, avr::io::pb3};

  //event loop
  while(true) {
    if(auto code = adc.async_read())
      auto value = code.value();
  }
}

demo/c++17/async_read.cpp

Gain 128/64/32

The default gain used to read is 128 at channel A, but the gain 64(ch. A) or gain 32(ch. B) can be selected:

auto code_64 = adc.read(hx711::gain::_64); // gain=64
auto code_32 = adc.read(hx711::gain::_32); // gain=32

Power-down

The adc can be powered down to save energy through a call off() and it can be powered up again using the method on().

How to use it?

This is a header only library. It should be enough to add the path to the include directory as also the path to the include directory of avrIO to your project:

  1. Check the requirements section.
  2. Add the include directory to your include path as also the include directory related to the dependency avrIO if this isn’t already done.
  3. Ensure that the macro F_CPU is defined. [1]
  4. Include the header hx711.hpp (#include <hx711.hpp>) to your source and enjoy it!

How to build the demos?

  1. If you want to build the demos with support to C++11, then go to the directory demo/c++11, if you want C++17 then go to demo/c++17.
  2. Adjust at least the variables MCU and AVRDUDE_DEVICE at demo/common.mk to the appropriate values related to your microcontroller. Note, the demos are already ready to ATtiny85.
  3. Check the value of the macro F_CPU at demo/common.mk to reflect the clock speed used by the microcontroller. The default value is 1000000, which means a clock of 1Mhz.
  4. make

I’m still lost, Is there something simpler than that?

Yes, go to the directory demo/c++11 and execute the following minimal command:

avr-g++ -std=c++11 -Os -mmcu=attiny85 -DF_CPU=1000000 -I../../include -I$AVR_IO_INCLUDE read.cpp

Replace $AVR_IO_INCLUDE by the path to the include directory of avrIO and check the value of the macro F_CPU, it must reflect the clock speed used by the microcontroller, the value above is related to a clock of 1Mhz.

The command above should generate a binary a.out in the current directory. Note, you should replace the argument attiny85 to the approriate value related to your microcontroller.

[1] Why this? The library uses delay functions of avr-libc and that functions, or better saying macros, they need to know what is the clock of your microcontroller.

Supported microcontrollers

  1. ATtiny13A
  2. ATtiny85
  3. ATmega328P

Dependencies

  1. avrIO
  2. avr-libc 2.0

Requirements

  1. avr-gcc with at least -std=c++11. Note: If C++14, C++17 or C++20 is used then some additional features can be offered.

[Extra] HX711 noise

An experiment to measure the intrinsic noise of HX711 @ 10SPS & Gain=128.

Releases

No releases published

Packages

No packages published

Languages