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.
The demos below are written in C++17, take a look at demo/c++11 to view the versions with support to C++11.
#include <hx711.hpp>
int main() {
hx711::adc adc{avr::io::pb4, avr::io::pb3};
auto code = adc.read();
}
#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();
}
}
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
The adc can be powered down to save energy through a call off()
and it can be powered up again using the method on()
.
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:
- Check the requirements section.
- Add the
include
directory to your include path as also the include directory related to the dependency avrIO if this isn’t already done. - Ensure that the macro
F_CPU
is defined. [1] - Include the header
hx711.hpp
(#include <hx711.hpp>
) to your source and enjoy it!
- 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 todemo/c++17
. - Adjust at least the variables
MCU
andAVRDUDE_DEVICE
atdemo/common.mk
to the appropriate values related to your microcontroller. Note, the demos are already ready to ATtiny85. - Check the value of the macro
F_CPU
atdemo/common.mk
to reflect the clock speed used by the microcontroller. The default value is1000000
, which means a clock of 1Mhz. make
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.
- ATtiny13A
- ATtiny85
- ATmega328P
- avrIO
- avr-libc 2.0
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.
An experiment to measure the intrinsic noise of HX711 @ 10SPS & Gain=128.