|
| 1 | +// |
| 2 | +// FILE: HX_calibration.ino |
| 3 | +// AUTHOR: Rob Tillaart |
| 4 | +// PURPOSE: HX711 demo |
| 5 | +// URL: https://github.com/RobTillaart/HX711 |
| 6 | + |
| 7 | + |
| 8 | +#include "HX711.h" |
| 9 | + |
| 10 | +HX711 myScale; |
| 11 | + |
| 12 | +uint8_t dataPin = 6; |
| 13 | +uint8_t clockPin = 7; |
| 14 | + |
| 15 | +uint32_t start, stop; |
| 16 | +volatile float f; |
| 17 | + |
| 18 | + |
| 19 | +void setup() |
| 20 | +{ |
| 21 | + Serial.begin(115200); |
| 22 | + Serial.println(__FILE__); |
| 23 | + Serial.print("LIBRARY VERSION: "); |
| 24 | + Serial.println(HX711_LIB_VERSION); |
| 25 | + Serial.println(); |
| 26 | + |
| 27 | + myScale.begin(dataPin, clockPin); |
| 28 | +} |
| 29 | + |
| 30 | +void loop() |
| 31 | +{ |
| 32 | + calibrate(); |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +void calibrate() |
| 38 | +{ |
| 39 | + Serial.println("\n\nCALIBRATION\n==========="); |
| 40 | + Serial.println("remove all weight from the loadcell"); |
| 41 | + // flush Serial input |
| 42 | + while (Serial.available()) Serial.read(); |
| 43 | + |
| 44 | + Serial.println("and press enter\n"); |
| 45 | + while (Serial.available() == 0); |
| 46 | + |
| 47 | + Serial.println("Determine zero weight offset"); |
| 48 | + myScale.tare(20); // average 20 measurements. |
| 49 | + uint32_t offset = myScale.get_offset(); |
| 50 | + |
| 51 | + Serial.print("OFFSET: "); |
| 52 | + Serial.println(offset); |
| 53 | + Serial.println(); |
| 54 | + |
| 55 | + |
| 56 | + Serial.println("place a weight on the loadcell"); |
| 57 | + // flush Serial input |
| 58 | + while (Serial.available()) Serial.read(); |
| 59 | + |
| 60 | + Serial.println("enter the weight in (whole) grams and press enter"); |
| 61 | + uint32_t weight = 0; |
| 62 | + while (Serial.peek() != '\n') |
| 63 | + { |
| 64 | + if (Serial.available()) |
| 65 | + { |
| 66 | + char ch = Serial.read(); |
| 67 | + if (isdigit(ch)) |
| 68 | + { |
| 69 | + weight *= 10; |
| 70 | + weight = weight + (ch - '0'); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + Serial.print("WEIGHT: "); |
| 75 | + Serial.println(weight); |
| 76 | + myScale.calibrate_scale(weight, 20); |
| 77 | + float scale = myScale.get_scale(); |
| 78 | + |
| 79 | + Serial.print("SCALE: "); |
| 80 | + Serial.println(scale, 6); |
| 81 | + |
| 82 | + Serial.print("\nuse scale.set_offset("); |
| 83 | + Serial.print(offset); |
| 84 | + Serial.print("); and scale.set_scale("); |
| 85 | + Serial.print(scale, 6); |
| 86 | + Serial.print(");\n"); |
| 87 | + Serial.println("in the setup of your project"); |
| 88 | + |
| 89 | + Serial.println("\n\n"); |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +// -- END OF FILE -- |
0 commit comments