- Notifications
You must be signed in to change notification settings - Fork6
A scale API for a Raspberry Pi Pico (RP2040) using the hx711-pico-c library.
License
endail/pico-scale
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
NOTE: DOCUMENTATION IS OLD. REFER TOthe test code for an example
A scale API for a Raspberry Pi Pico (RP2040).
The .gif above illustrates thecurrent example code obtaining data from a HX711 operating at 80 samples per second. Each line shows the current weight calculated from all samples obtained within 250 milliseconds, along with the minimum and maximum weights of the scale since boot. I applied pressure to the load cell to show the change in weight.
If you want to use this repository as-is with the example code, clone the respository and initialise thehx711-pico-c
submodule.
git clone --recurse-submodules --remote-submodules https://github.com/endail/pico-scale
Then#include
as follows:
#include"../include/hx711_scale_adaptor.h"#include"../include/scale.h"
Run CTest to build the example and calibration programs. The.uf2
files you upload to your Pico will be found underbuild/tests/
.
Alternatively, if you want to use the scale functionality as an API in your own project, addpico-scale
as a submodule and then initialise it.
git submodule add https://github.com/endail/pico-scale extern/pico-scalegit submodule update --init --remote --recursive
Then, from your own code,#include
the relevant files as follows and initialise the hx711:
#include"extern/pico-scale/include/hx711_scale_adaptor.h"#include"extern/pico-scale/include/scale.h"
See the explanationhere for why you need to manually include the PIO program.
https://endail.github.io/pico-scale
You will always need to initialise the HX711 before using it as a scale. Seehere for how to do that.
scale_tsc;// the values obtained when calibrating the scale// if you don't know them, read the following section How to Calibratemass_unit_tscaleUnit=mass_g;int32_trefUnit=-432;int32_toffset=-367539;scale_init(&sc,&hx,// pass a pointer to the hx711_tscaleUnit,refUnit,offset);// 3. Set options for how the scale will read and interpret values// SCALE_DEFAULT_OPTIONS will give some default settings which you// do not have to usescale_options_topt=SCALE_DEFAULT_OPTIONS;// scale_options_t has the following options//// opt.strat, which defines how the scale will collect data. By default,// data is collected according to the number of samples. So opt.strat// is set to strategy_type_samples. opt.samples defines how many samples// to obtain. You can also set opt.strat to read_type_time which will// collect as many samples as possible within the timeout period. The// timeout period is defined by opt.timeout and is given in microseconds// (us). For example, 1 second is equal to 1,000,000 us.//// opt.read, which defines how the scale will interpret data. By default,// data is interpreted according to the median value. So opt.read is set// to read_type_median. You can also set opt.read to read_type_average// which will calculate the average value.//// Example://// opt.strat = strategy_type_time;// opt.read = read_type_average;// opt.timeout = 250000;//// These options mean... collect as many samples as possible within 250ms// and then use the average of all those samples.// 4. Zero the scale (OPTIONAL) (aka. tare)if(scale_zero(&sc,&opt)) {printf("Scale zeroed successfully\n");}else {printf("Scale failed to zero\n");}// 5. Obtain the weightmass_tmass;if(scale_weight(&sc,&mass,&opt)) {// mass will contain the weight on the scale obtanined and interpreted// according to the given options and be in the unit defined by the// mass_unit_t 'scaleUnit' variable above//// you can now:// get the weight as a numeric value according to the mass_unit_tdoubleval;mass_get_value(&mass,&val);// convert the mass to a stringcharbuff[MASS_TO_STRING_BUFF_SIZE];mass_to_string(&mass,buff);printf("%s\n",buff);// or do other operations (see: mass.h file)}else {printf("Failed to read weight\n");}
Modifythe calibration program and change the clock and data pins to those connected to the HX711. Also change the rate at which the HX711 operates if needed.
Build by running
CTest
.Copy
calibration.uf2
in thebuild/tests/
directory to the Raspberry Pi Pico.Open a serial connection to the Pico at a baud rate of 115200 and follow the prompts.
Q:"Which mass units are supported?"
A: The followingmass_unit_t
s are available.
mass_ug
: microgramsmass_mg
: milligramsmass_g
: gramsmass_kg
: kilogramsmass_ton
: metric tonsmass_imp_ton
: imperial tonsmass_us_ton
: US tonsmass_st
: stonesmass_lb
: poundsmass_oz
: ounces
Q:"How do I get the weight in pounds/ounces/tons/etc...?"
A: You can either: set thescale_t
or change themass_t
.
// 1. setting the scale_t// when you initialise the scalescale_init(&sc,&hx,mass_imp_ton,//change the scaleUnit to your chosen mass_unit_trefUnit,offset);// or, if you've already initialised the scalesc.unit=mass_imp_ton;// 2. change the mass_t// if, for whatever reason, you are initialising a mass_tmass_init(&m,mass_lb,//your desired mass_unit_tval);// or, if you've already initialised a mass_tm.unit=mass_lb;
Q:"How do I perform math on the weights?"
A: You can either: get the underlying value and operate on that, or use the in-built functions to operate on twomass_t
structs.
// 1. get the underlying valuedoubleval;mass_get_value(&m,&val);if(val >=10.5) {//do something if val is greater than or equal to 10.5}//2. built-in functionsif(mass_gteq(&m1,&m2)) {//do something if m1 is greater than m2}
The advantage of using the built-in functions is that themass_t
structs can be of different units. So you can check if, for example,m1
is greater than or equal tom2
, even ifm1
is is in pounds andm2
is in kilograms. The conversion is taken care of for you.
Q:"Which math functions are available?"
A:
mass_add(mass_t* lhs, mass_t* rhs, mass_t* res)
: addlhs
andrhs
and store result inres
mass_sub(mass_t* lhs, mass_t* rhs, mass_t* res)
: subtractrhs
fromlhs
and store result inres
mass_mul(mass_t* lhs, mass_t* rhs, mass_t* res)
: multiplylhs
andrhs
and store result inres
mass_div(mass_t* lhs, mass_t* rhs, mass_t* res)
: dividelhs
byrhs
and store result inres
, returns false ifrhs
is 0mass_addeq(mass_t* self, mass_t* rhs)
: addrhs
toself
mass_subeq(mass_t* self, mass_t* rhs)
: subtractrhs
fromself
mass_muleq(mass_t* self, mass_t* rhs)
: multiplyself
byrhs
mass_diveq(mass_t* self, mass_t* rhs)
: divideself
byrhs
, returns false ifrhs
is 0mass_eq(mass_t* lhs, mass_t* rhs)
: return true iflhs
equalsrhs
mass_neq(mass_t* lhs, mass_t* rhs)
: return true iflhs
does not equalrhs
mass_lt(mass_t* lhs, mass_t* rhs)
: return true iflhs
is less thanrhs
mass_gt(mass_t* lhs, mass_t* rhs)
: return true iflhs
is greater thanrhs
mass_lteq(mass_t* lhs, mass_t* rhs)
: return true iflhs
is less than or equal torhs
mass_gteq(mass_t* lhs, mass_t* rhs)
: return true iflhs
is greater than or equal torhs
About
A scale API for a Raspberry Pi Pico (RP2040) using the hx711-pico-c library.