├── .github
└── FUNDING.yml
├── LICENSE.TXT
├── README.md
├── hx711.c
├── hx711.h
└── hx711Config.h
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [nimaltd]
4 | patreon: # Replace with a single Patreon username
5 | open_collective: # Replace with a single Open Collective username
6 | ko_fi: nimaltd
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: #nimaltd
10 | issuehunt: #nimaltd
11 | otechie: # Replace with a single Otechie username
12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
13 |
--------------------------------------------------------------------------------
/LICENSE.TXT:
--------------------------------------------------------------------------------
1 | Software License Terms
2 |
3 | This software is dual-licensed:
4 |
5 | 1. GNU General Public License v2 (GPLv2):
6 | - You may redistribute and/or modify this software under the terms of GPLv2 as published by the Free Software Foundation.
7 | - This license does not provide any warranty of any kind, including but not limited to MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 | - See for details.
9 |
10 | 2. Commercial License:
11 | - The commercial license removes all GPLv2 restrictions and allows you to redistribute your closed-source products with the Library embedded.
12 | - This license includes access to software maintenance, such as updates and upgrades. Customers who purchase this license are eligible to receive updates whenever
13 | they are released. However, the software provider is not obligated to release updates on a specific schedule.
14 | - The commercial license is available in two models:
15 | - Single-Product License: Allows usage in one specific product.
16 | - Company-Wide License: Allows usage across all products of the company.
17 | - No Warranty: The software is provided "AS IS" without any warranties, express or implied, including but not limited to the implied warranties of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, or NON-INFRINGEMENT.
18 | - Liability Disclaimer: In no event shall the copyright holder or contributors be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
19 |
20 | By using this software under the commercial license, you agree to these terms and acknowledge that no additional guarantees or obligations are provided beyond what is explicitly stated in this document.
21 |
22 | ---
23 |
24 | Copyright © Nima Askari, 2025. All rights reserved.
25 | For inquiries, contact: nima.askari@gmail.com All rights reserved
26 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## HX711 Library for STM32
2 |
3 | * http://www.github.com/NimaLTD
4 | * https://www.instagram.com/github.nimaltd/
5 | * https://www.youtube.com/channel/UCUhY7qY1klJm1d2kulr9ckw
6 |
7 | * Select 2 pins on CubeMX for clock and data pins.
8 | * Config `hx711Config.h`.
9 | * Call `hx711_init()`.
10 | * After init, you can read value.
11 |
12 | Example
13 | ``` c
14 | #include "hx711.h"
15 |
16 | hx711_t loadcell;
17 | float weight;
18 | .
19 | .
20 | int main()
21 | {
22 | hx711_init(&loadcell, HX711_CLK_GPIO_Port, HX711_CLK_Pin, HX711_DATA_GPIO_Port, HX711_DATA_Pin);
23 | hx711_coef_set(&loadcell, 354.5); // read afer calibration
24 | hx711_tare(&loadcell, 10);
25 | while (1)
26 | {
27 | HAL_Delay(500);
28 | weight = hx711_weight(&loadcell, 10);
29 | }
30 | }
31 |
32 | ```
33 |
--------------------------------------------------------------------------------
/hx711.c:
--------------------------------------------------------------------------------
1 | #include "hx711.h"
2 | #include "hx711Config.h"
3 | #if (_HX711_USE_FREERTOS == 1)
4 | #include "cmsis_os.h"
5 | #define hx711_delay(x) osDelay(x)
6 | #else
7 | #define hx711_delay(x) HAL_Delay(x)
8 | #endif
9 |
10 | //#############################################################################################
11 | void hx711_delay_us(void)
12 | {
13 | uint32_t delay = _HX711_DELAY_US_LOOP;
14 | while (delay > 0)
15 | {
16 | delay--;
17 | __nop(); __nop(); __nop(); __nop();
18 | }
19 | }
20 | //#############################################################################################
21 | void hx711_lock(hx711_t *hx711)
22 | {
23 | while (hx711->lock)
24 | hx711_delay(1);
25 | hx711->lock = 1;
26 | }
27 | //#############################################################################################
28 | void hx711_unlock(hx711_t *hx711)
29 | {
30 | hx711->lock = 0;
31 | }
32 | //#############################################################################################
33 | void hx711_init(hx711_t *hx711, GPIO_TypeDef *clk_gpio, uint16_t clk_pin, GPIO_TypeDef *dat_gpio, uint16_t dat_pin)
34 | {
35 | hx711_lock(hx711);
36 | hx711->clk_gpio = clk_gpio;
37 | hx711->clk_pin = clk_pin;
38 | hx711->dat_gpio = dat_gpio;
39 | hx711->dat_pin = dat_pin;
40 |
41 | GPIO_InitTypeDef gpio = {0};
42 | gpio.Mode = GPIO_MODE_OUTPUT_PP;
43 | gpio.Pull = GPIO_NOPULL;
44 | gpio.Speed = GPIO_SPEED_FREQ_HIGH;
45 | gpio.Pin = clk_pin;
46 | HAL_GPIO_Init(clk_gpio, &gpio);
47 | gpio.Mode = GPIO_MODE_INPUT;
48 | gpio.Pull = GPIO_PULLUP;
49 | gpio.Speed = GPIO_SPEED_FREQ_HIGH;
50 | gpio.Pin = dat_pin;
51 | HAL_GPIO_Init(dat_gpio, &gpio);
52 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_SET);
53 | hx711_delay(10);
54 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_RESET);
55 | hx711_delay(10);
56 | hx711_value(hx711);
57 | hx711_value(hx711);
58 | hx711_unlock(hx711);
59 | }
60 | //#############################################################################################
61 | int32_t hx711_value(hx711_t *hx711)
62 | {
63 | uint32_t data = 0;
64 | uint32_t startTime = HAL_GetTick();
65 | while(HAL_GPIO_ReadPin(hx711->dat_gpio, hx711->dat_pin) == GPIO_PIN_SET)
66 | {
67 | hx711_delay(1);
68 | if(HAL_GetTick() - startTime > 150)
69 | return 0;
70 | }
71 | for(int8_t i=0; i<24 ; i++)
72 | {
73 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_SET);
74 | hx711_delay_us();
75 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_RESET);
76 | hx711_delay_us();
77 | data = data << 1;
78 | if(HAL_GPIO_ReadPin(hx711->dat_gpio, hx711->dat_pin) == GPIO_PIN_SET)
79 | data ++;
80 | }
81 | data = data ^ 0x800000;
82 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_SET);
83 | hx711_delay_us();
84 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_RESET);
85 | hx711_delay_us();
86 | return data;
87 | }
88 | //#############################################################################################
89 | int32_t hx711_value_ave(hx711_t *hx711, uint16_t sample)
90 | {
91 | hx711_lock(hx711);
92 | int64_t ave = 0;
93 | for(uint16_t i=0 ; ioffset = (int32_t)(ave / sample);
113 | hx711_unlock(hx711);
114 | }
115 | //#############################################################################################
116 | void hx711_calibration(hx711_t *hx711, int32_t noload_raw, int32_t load_raw, float scale)
117 | {
118 | hx711_lock(hx711);
119 | hx711->offset = noload_raw;
120 | hx711->coef = (load_raw - noload_raw) / scale;
121 | hx711_unlock(hx711);
122 | }
123 | //#############################################################################################
124 | float hx711_weight(hx711_t *hx711, uint16_t sample)
125 | {
126 | hx711_lock(hx711);
127 | int64_t ave = 0;
128 | for(uint16_t i=0 ; ioffset) / hx711->coef;
135 | hx711_unlock(hx711);
136 | return answer;
137 | }
138 | //#############################################################################################
139 | void hx711_coef_set(hx711_t *hx711, float coef)
140 | {
141 | hx711->coef = coef;
142 | }
143 | //#############################################################################################
144 | float hx711_coef_get(hx711_t *hx711)
145 | {
146 | return hx711->coef;
147 | }
148 | //#############################################################################################
149 | void hx711_power_down(hx711_t *hx711)
150 | {
151 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_RESET);
152 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_SET);
153 | hx711_delay(1);
154 | }
155 | //#############################################################################################
156 | void hx711_power_up(hx711_t *hx711)
157 | {
158 | HAL_GPIO_WritePin(hx711->clk_gpio, hx711->clk_pin, GPIO_PIN_RESET);
159 | }
160 | //#############################################################################################
161 |
--------------------------------------------------------------------------------
/hx711.h:
--------------------------------------------------------------------------------
1 | #ifndef HX711_H_
2 | #define HX711_H_
3 |
4 |
5 | /*
6 | Author: Nima Askari
7 | WebSite: http://www.github.com/NimaLTD
8 | Instagram: http://instagram.com/github.NimaLTD
9 | Youtube: https://www.youtube.com/channel/UCUhY7qY1klJm1d2kulr9ckw
10 |
11 | Version: 1.1.1
12 |
13 |
14 | Reversion History:
15 |
16 | (1.1.1):
17 | Add power down/up.
18 | (1.1.0):
19 | Add structure, Add calibration, Add weight, change names, ...
20 | (1.0.0):
21 | First Release.
22 | */
23 |
24 | #ifdef __cplusplus
25 | extern "C" {
26 | #endif
27 |
28 | #include "main.h"
29 |
30 | //####################################################################################################################
31 |
32 | typedef struct
33 | {
34 | GPIO_TypeDef *clk_gpio;
35 | GPIO_TypeDef *dat_gpio;
36 | uint16_t clk_pin;
37 | uint16_t dat_pin;
38 | int32_t offset;
39 | float coef;
40 | uint8_t lock;
41 |
42 | }hx711_t;
43 |
44 | //####################################################################################################################
45 |
46 | void hx711_init(hx711_t *hx711, GPIO_TypeDef *clk_gpio, uint16_t clk_pin, GPIO_TypeDef *dat_gpio, uint16_t dat_pin);
47 | int32_t hx711_value(hx711_t *hx711);
48 | int32_t hx711_value_ave(hx711_t *hx711, uint16_t sample);
49 |
50 | void hx711_coef_set(hx711_t *hx711, float coef);
51 | float hx711_coef_get(hx711_t *hx711);
52 | void hx711_calibration(hx711_t *hx711, int32_t value_noload, int32_t value_load, float scale);
53 | void hx711_tare(hx711_t *hx711, uint16_t sample);
54 | float hx711_weight(hx711_t *hx711, uint16_t sample);
55 | void hx711_power_down(hx711_t *hx711);
56 | void hx711_power_up(hx711_t *hx711);
57 |
58 | //####################################################################################################################
59 |
60 | #ifdef __cplusplus
61 | }
62 | #endif
63 |
64 | #endif
65 |
--------------------------------------------------------------------------------
/hx711Config.h:
--------------------------------------------------------------------------------
1 | #ifndef _HX711CONFIG_H_
2 | #define _HX711CONFIG_H_
3 |
4 | #define _HX711_USE_FREERTOS 0
5 | #define _HX711_DELAY_US_LOOP 4
6 | #endif
7 |
--------------------------------------------------------------------------------