HX711 Weighing Sensor
This module uses the 24-bit high-precision A/D converter chip HX711. It is designed specifically for high-precision electronic scales, featuring two analog input channels and an integrated programmable 128x gain amplifier. The input circuit can be configured as a bridge-type sensor mode (such as pressure or weighing) that provides bridge voltage. It is an ideal high-precision, low-cost sampling front-end module.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a21n57.1.0.0.19a3523cagvNPv&id=611254073645&ns=1&abbucket=0#detail Materials download link: https://pan.baidu.com/s/1V2NdHCmvusPDhBp00VvIvQ Password: j2sh
Specifications
Operating voltage: 2.6V-5.5V Operating current: 100~1500uA ADC precision: 24-bit Output method: Serial output Number of pins: 4 Pin
File Download
File 18.2-1 Product Datasheet
Sensor Wiring Diagram
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. However, here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_hx711.c and bsp_hx711.h, and rename the folder to HX711.
Write Code
In the file bsp_hx711.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-02 LCKFB-lp first version
*/
#include "bsp_hx711.h"
#include "stdio.h"
unsigned int HX711_Buffer;
unsigned int Weight_Maopi;
int Weight_Shiwu;
unsigned char Flag_Error = 0;
// Calibration parameter
// Because the characteristic curves of different sensors are not very consistent, each sensor needs to calibrate this parameter here to make the measured value very accurate.
// When the tested weight is too large, increase this value.
// If the tested weight is too small, decrease this value.
// This value can be a decimal
#define GapValue 208.05
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: HX711_GPIO_Init
* Function Description: Pin initialization for HX711
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void HX711_GPIO_Init(void)
{
gpio_config_t SCK_config = {
.pin_bit_mask = (1ULL<<HX711_SCK_PIN), // Configure pin
.mode =GPIO_MODE_OUTPUT, // Output mode
.pull_up_en = GPIO_PULLUP_ENABLE, // Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&SCK_config);
gpio_config_t DT_config = {
.pin_bit_mask = (1ULL<<HX711_DT_PIN), // Configure pin
.mode =GPIO_MODE_INPUT, // Input mode
.pull_up_en = GPIO_PULLUP_ENABLE, // Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&DT_config);
}
/******************************************************************
* Function Name: HX711_Read
* Function Description: Read HX711
* Function Parameters: None
* Function Return: The read value
* Author: LC
* Notes: None
******************************************************************/
unsigned int HX711_Read(void) // Gain 128
{
unsigned long count;
unsigned char i;
DT(1);
delay_us(1);
SCK(0);
count=0;
while(DT_GET());
for(i=0;i<24;i++)
{
SCK(1);
count=count<<1;
delay_us(1);
SCK(0);
if(DT_GET())
count++;
delay_us(1);
}
SCK(1);
count=count^0x800000;// When the 25th pulse falling edge arrives, convert data
delay_us(1);
SCK(0);
return(count);
}
/******************************************************************
* Function Name: Get_Maopi
* Function Description: Measure the initial weight (tare)
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: Subsequent weights are based on this initial weight as zero, so do not place anything on the scale during initialization
******************************************************************/
void Get_Maopi(void)
{
Weight_Maopi = HX711_Read();
}
/******************************************************************
* Function Name: Get_Weight
* Function Description: Weighing
* Function Parameters: None
* Function Return: Weight value, unit g
* Author: LC
* Notes: None
******************************************************************/
float Get_Weight(void)
{
float Weight=0;
HX711_Buffer = HX711_Read();
if(HX711_Buffer > Weight_Maopi)
{
Weight_Shiwu = HX711_Buffer - Weight_Maopi; // Get the AD sampling value of the object.
Weight = (float)Weight_Shiwu / GapValue;// Calculate the actual weight of the object
// Because the characteristic curves of different sensors are not the same, each sensor needs to calibrate the GapValue divisor here.
// When the tested weight is too large, increase this value.
// If the tested weight is too small, decrease this value.
}
return Weight;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
In the file bsp_hx711.h, write the following code.
#ifndef _BSP_HX711_H_
#define _BSP_HX711_H_
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gpio.h"
// Port porting
#define HX711_SCK_PIN 1
#define HX711_DT_PIN 2
// Set DT output mode
#define DT_OUT() gpio_set_direction(HX711_DT_PIN,GPIO_MODE_OUTPUT)
// Set DT input mode
#define DT_IN() gpio_set_direction(HX711_DT_PIN,GPIO_MODE_INPUT)
// Get the level change of DT pin
#define DT_GET() gpio_get_level(HX711_DT_PIN)
// DT and SCK output
#define DT(x) gpio_set_level(HX711_DT_PIN, (x?1:0))
#define SCK(x) gpio_set_level(HX711_SCK_PIN, (x?1:0))
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void HX711_GPIO_Init(void);
float Get_Weight(void);
void Get_Maopi(void);
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Porting Verification
In the main function of your own project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-02 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_hx711.h"
void app_main(void)
{
HX711_GPIO_Init();
Get_Maopi(); // Weigh the tare
delay_ms(500);
Get_Maopi(); // Re-obtain the tare weight
printf("start..........\r\n");
while(1)
{
printf("w = %.2fg\r\n",Get_Weight());
delay_ms(500);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Power-on effect (with a 200g weight placed):
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.