Skip to content

Commit 8dd2657

Browse files
authored
fix #54, calibration sketch (#55)
- fix #54, calibration sketch should output int32_t for offset. - update examples - update readme.md - minor edits
1 parent fbd4b79 commit 8dd2657

File tree

22 files changed

+205
-159
lines changed

22 files changed

+205
-159
lines changed

CHANGELOG..md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77

8+
## [0.5.1] - 2024-11-08
9+
- fix #54, calibration sketch should output int32_t for offset.
10+
- update examples
11+
- update readme.md
12+
- minor edits
13+
814
## [0.5.0] - 2024-06-17
915
- fix #50, bug in constructor (thanks Mathieu!)
1016
- refactor constructor

HX711.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: HX711.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.5.0
4+
// VERSION: 0.5.1
55
// PURPOSE: Library for load cells for UNO
66
// URL: https://github.com/RobTillaart/HX711_MP
77
// URL: https://github.com/RobTillaart/HX711
@@ -108,7 +108,7 @@ float HX711::read()
108108

109109
union
110110
{
111-
long value = 0;
111+
int32_t value = 0;
112112
uint8_t data[4];
113113
} v;
114114

@@ -375,13 +375,13 @@ float HX711::get_scale()
375375
}
376376

377377

378-
void HX711::set_offset(long offset)
378+
void HX711::set_offset(int32_t offset)
379379
{
380380
_offset = offset;
381381
}
382382

383383

384-
long HX711::get_offset()
384+
int32_t HX711::get_offset()
385385
{
386386
return _offset;
387387
}

HX711.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: HX711.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.5.0
5+
// VERSION: 0.5.1
66
// PURPOSE: Library for load cells for Arduino
77
// URL: https://github.com/RobTillaart/HX711_MP
88
// URL: https://github.com/RobTillaart/HX711
@@ -15,7 +15,7 @@
1515

1616
#include "Arduino.h"
1717

18-
#define HX711_LIB_VERSION (F("0.5.0"))
18+
#define HX711_LIB_VERSION (F("0.5.1"))
1919

2020

2121
const uint8_t HX711_AVERAGE_MODE = 0x00;
@@ -143,8 +143,8 @@ class HX711
143143
float get_scale();
144144

145145
// OFFSET > 0
146-
void set_offset(long offset = 0);
147-
long get_offset();
146+
void set_offset(int32_t offset = 0);
147+
int32_t get_offset();
148148

149149
// clear the scale
150150
// call tare() to set the zero offset
@@ -177,7 +177,7 @@ class HX711
177177
uint8_t _clockPin;
178178

179179
uint8_t _gain;
180-
long _offset;
180+
int32_t _offset;
181181
float _scale;
182182
uint32_t _lastRead;
183183
float _price;

README.md

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This translates roughly to 4 or max 5 significant digits in a single measurement
2929
That's why multiple measurements are advised to average and reduce the noise.
3030

3131

32-
#### Breaking change 0.3.0
32+
### Breaking change 0.3.0
3333

3434
In issue #11 it became clear that the timing of the default **shiftIn()** function to
3535
read the value of the internal ADC was too fast on some processor boards for the HX711.
@@ -44,7 +44,7 @@ Drawback is that reading the HX711 takes an extra 50-55 microseconds.
4444
How much this affects performance is to be investigated.
4545

4646

47-
#### HX711_MP
47+
### HX711_MP
4848

4949
- https://github.com/RobTillaart/HX711_MP
5050

@@ -61,7 +61,7 @@ This multi-point calibration allows to compensate for non-linear behaviour
6161
in the sensor readings.
6262

6363

64-
#### 10 or 80 SPS
64+
### 10 or 80 SPS
6565

6666
The datasheet mentions that the HX711 can run at 80 samples per second SPS.
6767
To select this mode connect the **RATE** pin(15) of the chip to VCC (HIGH).
@@ -75,12 +75,17 @@ If there is a need (issue) I will implement this in the library.
7575
For now one can add an IOpin for this and use **digitalWrite()**.
7676

7777

78-
#### Related
78+
### Related
7979

8080
- https://github.com/bogde/HX711
8181
- https://github.com/RobTillaart/weight (conversions kg <> stone etc.)
8282
- https://github.com/RobTillaart/HX711
83-
- https://github.com/RobTillaart/HX711_MP
83+
- https://github.com/RobTillaart/HX711_MP multipoint calibration version.
84+
85+
86+
### Faulty boards
87+
88+
- https://forum.arduino.cc/t/load-cell-amplifier-hx711-wrong-ground/1046075
8489

8590

8691
## Main flow
@@ -107,14 +112,18 @@ Steps to take for calibration
107112
#include "HX711.h"
108113
```
109114

110-
#### Base
115+
### Constructor
111116

112117
- **HX711()** constructor.
113118
- **~HX711()**
114119
- **void begin(uint8_t dataPin, uint8_t clockPin, bool fastProcessor)** sets a fixed gain 128 for now.
115120
The fastProcessor option adds a 1 uS delay for each clock half-cycle to keep the time greater than 200 nS.
116121
- **void reset()** set internal state to start condition.
117122
Since 0.3.4 reset also does a power down / up cycle.
123+
124+
125+
### Read
126+
118127
- **bool is_ready()** checks if load cell is ready to read.
119128
- **void wait_ready(uint32_t ms = 0)** wait until ready, check every ms.
120129
- **bool wait_ready_retry(uint8_t retries = 3, uint32_t ms = 0)** wait max retries.
@@ -130,7 +139,7 @@ The weight alpha can be set to any value between 0 and 1, times >= 1.
130139
- **uint32_t last_read()** returns timestamp in milliseconds of last read.
131140

132141

133-
#### Gain + channel
142+
### Gain + channel
134143

135144
Use with care as it is not 100% reliable - see issue #27. (solutions welcome).
136145

@@ -169,13 +178,13 @@ as the datasheet states it should be. So use with care. (feedback welcome)
169178
See discussion #27.
170179

171180

172-
#### Mode
181+
### Read mode
173182

174183
Get and set the operational mode for **get_value()** and indirect **get_units()**.
175184

176185
Constants (see .h file)
177186

178-
- **HX711_RAW_MODE** new in 0.3.3
187+
- **HX711_RAW_MODE**
179188
- **HX711_AVERAGE_MODE**
180189
- **HX711_MEDIAN_MODE**
181190
- **HX711_MEDAVG_MODE**
@@ -193,7 +202,7 @@ to keep memory footprint relative low.
193202
- **uint8_t get_mode()** returns current set mode. Default is **HX711_AVERAGE_MODE**.
194203

195204

196-
#### Get values
205+
### Get values
197206

198207
Get values from the HX711 corrected for offset and scale.
199208
Note that in **HX711_RAW_MODE** the times parameter will be ignored => just call **read()** once.
@@ -203,11 +212,11 @@ Note that in **HX711_RAW_MODE** the times parameter will be ignored => just call
203212
- **bool set_scale(float scale = 1.0)** set scale factor which is normally a positive number larger than 50. Depends on load-cell used.
204213
Returns false if scale == 0.
205214
- **float get_scale()** returns set scale factor.
206-
- **void set_offset(long offset = 0)** idem.
207-
- **long get_offset()** idem.
215+
- **void set_offset(int32_t offset = 0)** idem.
216+
- **int32_t get_offset()** idem.
208217

209218

210-
#### Tare & calibration I
219+
### Tare & calibration I
211220

212221
Steps to take for calibration
213222
1. clear the scale.
@@ -227,7 +236,7 @@ Assumes offset is not zero, which is true for all load cells tested.
227236
- **void calibrate_scale(uint16_t weight, uint8_t times = 10)** idem.
228237

229238

230-
#### Tare & calibration II
239+
### Tare & calibration II
231240

232241
A load cell + HX711 module without weight gives a raw value, mostly not equal to zero.
233242
The function **get_tare()** is used to measure this raw value and allows the user
@@ -248,7 +257,7 @@ Furthermore it is also important to do the calibration at the temperature you
248257
expect to do the weight measurements. See temperature section below.
249258

250259

251-
#### Inner formula
260+
### Inner formula
252261

253262
Weight = **get_scale()** x raw + **get_tare()**.
254263

@@ -258,7 +267,7 @@ This can be compared with actual values to get an indication
258267
of the accuracy of the load cell.
259268

260269

261-
#### Power management
270+
### Power management
262271

263272
- **void power_down()** idem. Explicitly blocks for 64 microseconds.
264273
(See Page 5 datasheet).
@@ -267,7 +276,7 @@ It should reset the HX711 to defaults but this is not always seen.
267276
See discussion issue #27 GitHub. Needs more testing.
268277

269278

270-
#### Pricing
279+
### Pricing
271280

272281
Some price functions were added to make it easy to use this library
273282
for pricing goods or for educational purposes.
@@ -279,10 +288,31 @@ For weight conversion functions see https://github.com/RobTillaart/weight
279288
- **float get_unit_price()** idem.
280289

281290

291+
### Millivolts
292+
293+
**Experimental**
294+
295+
To be verified in a test setup.
296+
297+
In issue #53, a question was asked to convert the input of the HX711 to milivolts.
298+
Thinking about this question resulted in a simple and imho an elegant idea:
299+
300+
- Apply 0.000 mV to the system.
301+
- Call **tare(times)** to calibrate the zero point.
302+
- Then apply 20.000 mV to the system.
303+
- Call **calibrate_scale(20000)** to map the raw reading to 20000 µV = 20 mV.
304+
305+
Assuming the scale is linear, the HX711 now works like a millivolt meter.
306+
And the **float get_units(uint8_t times = 1)** will return microvolts.
307+
308+
In fact, one could map any linear unit this way, e.g. if the voltage applied
309+
is linear with temperature, humidity or windspeed one can map this directly.
310+
311+
282312
## Notes
283313

284314

285-
#### Scale values for load cells
315+
### Scale values for load cells
286316

287317
These scale values worked pretty well with a set of load cells I have,
288318
Use calibrate to find your favourite values.
@@ -291,7 +321,7 @@ Use calibrate to find your favourite values.
291321
- 20 KG load cell scale.set_scale(127.15);
292322

293323

294-
#### Connections HX711
324+
### Connections HX711
295325

296326
- A+/A- uses gain of 128 or 64
297327
- B+/B- uses gain of 32
@@ -308,7 +338,7 @@ Colour scheme wires of two devices.
308338
| B+ | not connected | not connected |
309339

310340

311-
#### Temperature
341+
### Temperature
312342

313343
Load cells do have a temperature related error. (see datasheet load cell)
314344
This can be reduced by doing the calibration and take the tare
@@ -322,20 +352,20 @@ differences in your code.
322352
## Multiple HX711
323353

324354

325-
#### Separate lines
355+
### Separate lines
326356

327357
Simplest way to control multiple HX711's is to have a separate **DOUT** and **CLK**
328358
line for every HX711 connected.
329359

330360

331-
#### Multiplexer
361+
### Multiplexer
332362

333363
Alternative one could use a multiplexer like the https://github.com/RobTillaart/HC4052
334364
or possibly an https://github.com/RobTillaart/TCA9548.
335365
Although to control the multiplexer one need some extra lines and code.
336366

337367

338-
#### Share CLOCK line
368+
### Share CLOCK line
339369

340370
See **HX_loadcell_array.ino**
341371

@@ -368,7 +398,6 @@ See https://github.com/RobTillaart/HX711/issues/40
368398
- update documentation HX711
369399
- keep in sync with HX711_MP
370400

371-
372401
#### Should
373402

374403
- test B channel explicitly.
@@ -377,7 +406,6 @@ See https://github.com/RobTillaart/HX711/issues/40
377406
- investigate the need of yield after interrupts
378407
- investigate blocking loop at begin => less yield() calls ?
379408

380-
381409
#### Could
382410

383411
- test different load cells
@@ -389,7 +417,6 @@ See https://github.com/RobTillaart/HX711/issues/40
389417
- add **setRate()** and **getRate()** - sample rate 10/80 SPS
390418
- optional?
391419

392-
393420
#### Wont
394421

395422
- why store the gain as \_gain while the iterations m = 1..3 is used most

examples/HX_calibration/HX_calibration.ino

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
//
22
// FILE: HX_calibration.ino
33
// AUTHOR: Rob Tillaart
4-
// PURPOSE: HX711 demo
4+
// PURPOSE: HX711 calibration finder for offset and scale
55
// URL: https://github.com/RobTillaart/HX711
66

77

88
#include "HX711.h"
99

1010
HX711 myScale;
1111

12+
// adjust pins if needed.
1213
uint8_t dataPin = 6;
1314
uint8_t clockPin = 7;
1415

15-
uint32_t start, stop;
16-
volatile float f;
17-
1816

1917
void setup()
2018
{
2119
Serial.begin(115200);
2220
Serial.println(__FILE__);
23-
Serial.print("LIBRARY VERSION: ");
21+
Serial.print("HX711_LIB_VERSION: ");
2422
Serial.println(HX711_LIB_VERSION);
2523
Serial.println();
2624

@@ -45,8 +43,9 @@ void calibrate()
4543
while (Serial.available() == 0);
4644

4745
Serial.println("Determine zero weight offset");
48-
myScale.tare(20); // average 20 measurements.
49-
uint32_t offset = myScale.get_offset();
46+
// average 20 measurements.
47+
myScale.tare(20);
48+
int32_t offset = myScale.get_offset();
5049

5150
Serial.print("OFFSET: ");
5251
Serial.println(offset);

0 commit comments

Comments
 (0)