1
+ /*
2
+ *******************************************************************************
3
+ * Copyright (c) 2022 by M5Stack
4
+ * Equipped with M5Core sample source code
5
+ * 配套 M5Core 示例源代码
6
+ * Visit the website for more information:https://docs.m5stack.com/en/app/scales_kit
7
+ * 获取更多资料请访问:https://docs.m5stack.com/zh_CN/app/scales_kit
8
+ *
9
+ * describe: SCALES KIT WEIGHT UNIT EXAMPLE.
10
+ * date:2022/02/23
11
+ *******************************************************************************
12
+ Connect WEIGHT UNIT to port B (G26/36), calibration instructions: press button A to remove the tare weight when there is no load,
13
+ press button B, switch the standard weight value left and right and put down the corresponding weight, confirm for calibration.
14
+ 将WEIGHT UNIT连接至端口B(G26/36), 校准说明:无负重情况下按下按键A去处皮重, 按下按键B,左右切换标准重量值并放下对应重量砝码,comfirm进行校准。
15
+ Libraries:
16
+ - [HX711](https://github.com/bogde/HX711)
17
+
18
+ */
19
+
20
+ #include < M5Stack.h>
21
+ #include < M5GFX.h>
22
+ #include " HX711.h"
23
+
24
+ M5GFX display;
25
+ M5Canvas canvas (&display);
26
+
27
+ // HX711 related pin Settings. HX711 相关引脚设置
28
+ #define LOADCELL_DOUT_PIN 36
29
+ #define LOADCELL_SCK_PIN 26
30
+
31
+ HX711 scale;
32
+
33
+ void setup () {
34
+ M5.begin (); // Init M5Stack. 初始化M5Stack
35
+ M5.Power .begin (); // Init power 初始化电源模块
36
+ display.begin ();
37
+ canvas.setColorDepth (1 ); // mono color
38
+ canvas.createSprite (display.width (), display.height ());
39
+ canvas.setTextDatum (MC_DATUM);
40
+ canvas.setPaletteColor (1 , GREEN);
41
+
42
+ canvas.drawString (" Calibration sensor...." , 160 , 80 );
43
+ canvas.pushSprite (0 , 0 );
44
+ scale.begin (LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
45
+
46
+
47
+ scale.set_gain ();
48
+ // The scale value is the adc value corresponding to 1g
49
+ scale.set_scale (27 .61f ); // set scale
50
+ scale.tare (); // auto set offset
51
+ }
52
+
53
+ char info[100 ];
54
+
55
+ void loop () {
56
+ canvas.fillSprite (BLACK);
57
+ canvas.setTextSize (1 );
58
+ canvas.drawString (" Connect the Weight Unit to PortB(G26,G36)" , 160 , 40 );
59
+ canvas.drawString (" Click Btn A for Tare deduction" , 160 , 55 );
60
+ canvas.drawString (" Click Btn B Switch to Calibration mode" , 160 , 70 );
61
+ float weight = scale.get_units (10 )/1000.0 ;
62
+ // float weight = scale.get_units(10) / 1.0;
63
+ canvas.setTextSize (3 );
64
+ if (weight >= 0 ) {
65
+ Serial.printf (" Weight: %.2f" , weight);
66
+ sprintf (info, " Weight: %.2f" , weight);
67
+ canvas.drawString (String (info) + " kg" , 160 , 150 );
68
+ // canvas.drawString(String(info) + "g", 160, 150);
69
+ }else {
70
+ canvas.drawString (" Weight: 0kg" , 160 , 150 );
71
+ // canvas.drawString("Weight: 0g", 160, 150);
72
+ }
73
+ M5.update ();
74
+ if (M5.BtnA .wasPressed ()) {
75
+ scale.tare ();
76
+ canvas.drawString (" 0g Calibration!" , 160 , 180 );
77
+ }
78
+ if (M5.BtnB .wasPressed ()) {
79
+ long kg = 5 ;
80
+ while (1 )
81
+ {
82
+ M5.update ();
83
+ canvas.fillSprite (BLACK);
84
+ canvas.setTextSize (1 );
85
+ canvas.drawString (" Connect the Weight Unit to PortB(G26,G36)" , 160 , 40 );
86
+ canvas.drawString (" Click Btn A/C to change kg value" , 160 , 55 );
87
+ canvas.drawString (" Click Btn B Calibration Comfirm" , 160 , 70 );
88
+ canvas.setTextSize (3 );
89
+ canvas.drawString (" Calibration:" + String (kg) + " kg" , 160 , 150 );
90
+ canvas.drawString (" comfirm" , 160 , 200 );
91
+ canvas.fillTriangle (40 , 200 , 60 , 220 , 60 , 180 , 1 );
92
+ canvas.fillTriangle (280 , 200 , 260 , 220 , 260 , 180 , 1 );
93
+ canvas.pushSprite (0 , 0 );
94
+ if (M5.BtnA .isPressed ()){
95
+ kg--;
96
+ }
97
+ if (M5.BtnC .isPressed ()){
98
+ kg++;
99
+ }
100
+ if (M5.BtnB .wasPressed ()){
101
+ break ;
102
+ }
103
+ delay (10 );
104
+ }
105
+ long kg_adc = scale.read_average (20 );
106
+ kg_adc = kg_adc - scale.get_offset ();
107
+ scale.set_scale ( kg_adc / (kg * 1000.0 ));
108
+ // canvas.drawString(String(kg) + "kg Calibration: " + String(kg_adc / (kg * 1000.0)), 160, 180);
109
+ canvas.drawString (" Set Scale: " + String (kg_adc / (kg * 1000.0 )), 160 , 180 );
110
+ canvas.pushSprite (0 , 0 );
111
+ delay (1000 );
112
+ }
113
+ canvas.pushSprite (0 , 0 );
114
+ }
0 commit comments