├── library.properties ├── README.md ├── .github └── FUNDING.yml ├── src ├── HX711_asukiaaa.h └── HX711_asukiaaa.cpp ├── LICENSE └── examples └── readMultiple └── readMultiple.ino /library.properties: -------------------------------------------------------------------------------- 1 | name=HX711_asukiaaa 2 | version=1.0.0 3 | author=Asuki Kono 4 | maintainer=Asuki Kono 5 | sentence=It read values from HX711 6 | paragraph=It can read vales from muiltiple HX711 modules with same clock. 7 | category=Sensors 8 | url=https://github.com/asukiaaa/HX711_asukiaaa 9 | architectures=* 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # arduino-HX711 2 | 3 | A library to use multiple HX711 amplifier modules for load cells. 4 | 5 | # Usage 6 | 7 | See [examples](./examples). 8 | 9 | # License 10 | 11 | MIT 12 | 13 | # References 14 | 15 | - [HX711 Module | Akizuki](https://akizukidenshi.com/catalog/g/gK-12370/) 16 | - [Load cell 120kg](https://akizukidenshi.com/catalog/g/gP-12035/) 17 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: asukiaaa 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 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: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /src/HX711_asukiaaa.h: -------------------------------------------------------------------------------- 1 | #ifndef _HX711_ASUKIAAA_H_ 2 | #define _HX711_ASUKIAAA_H_ 3 | 4 | #include 5 | 6 | namespace HX711_asukiaaa { 7 | enum Error { 8 | timeout = 1, 9 | }; 10 | 11 | class Parser { 12 | public: 13 | Parser(float ratedVoltage, float ratedGram, float r1, float r2); 14 | float parseToGram(int32_t raw); 15 | // float offsetGram; 16 | private: 17 | // const float ratedVoltage; 18 | // const float ratedGram; 19 | // const float r1; 20 | // const float r2; 21 | const float avdd; 22 | const float adc1bit; 23 | const float scale; 24 | }; 25 | 26 | class Reader { 27 | public: 28 | Reader(int* pinsDout, int doutLen, int pinSlk); 29 | ~Reader(); 30 | void begin(); 31 | void reset(); 32 | int read(int sumNumber = 10); 33 | 34 | int32_t* values; 35 | const int doutLen; 36 | private: 37 | const int* pinsDout; 38 | const int pinSlk; 39 | 40 | int readRawOnce(int32_t* readValues, unsigned long timeout = 50UL); 41 | bool pinsAreReady(); 42 | }; 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /examples/readMultiple/readMultiple.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int pinsDout[] = {5, 6, 7, 8}; 4 | const int numPins = sizeof(pinsDout) / sizeof(pinsDout[0]); 5 | int pinSlk = 4; 6 | HX711_asukiaaa::Reader reader(pinsDout, numPins, pinSlk); 7 | 8 | //---------------------------------------------------// 9 | // load cell SC301A 100kg 10 | // https://akizukidenshi.com/catalog/g/gP-12036/ 11 | //---------------------------------------------------// 12 | // #define LOAD_CELL_RATED_VOLT 0.002f 13 | // #define LOAD_CELL_RATED_GRAM 100000.0f 14 | 15 | //---------------------------------------------------// 16 | // load cell SC601 120kg 17 | // https://akizukidenshi.com/catalog/g/gP-12035/ 18 | //---------------------------------------------------// 19 | #define LOAD_CELL_RATED_VOLT 0.002f 20 | #define LOAD_CELL_RATED_GRAM 120000.0f 21 | 22 | //---------------------------------------------------// 23 | // load cell SC133 20kg 24 | // https://akizukidenshi.com/catalog/g/gP-12034/ 25 | //---------------------------------------------------// 26 | // #define LOAD_CELL_RATED_VOLT 0.002f 27 | // #define LOAD_CELL_RATED_GRAM 20000.0f 28 | 29 | //---------------------------------------------------// 30 | // load cell SC616C 500g 31 | // https://akizukidenshi.com/catalog/g/gP-12532/ 32 | //---------------------------------------------------// 33 | // #define LOAD_CELL_RATED_VOLT 0.0007f 34 | // #define LOAD_CELL_RATED_GRAM 500.0f 35 | 36 | //---------------------------------------------------// 37 | // Resistor value for HX711 mofule sold on Akizuki 38 | // https://akizukidenshi.com/catalog/g/gK-12370/ 39 | //---------------------------------------------------// 40 | #define HX711_R1 20000.0 41 | #define HX711_R2 8200.0 42 | 43 | HX711_asukiaaa::Parser parser(LOAD_CELL_RATED_VOLT, LOAD_CELL_RATED_GRAM, HX711_R1, HX711_R2); 44 | float offsetGrams[numPins]; 45 | 46 | void setup() { 47 | Serial.begin(115200); 48 | Serial.println("start"); 49 | reader.begin(); 50 | reader.read(); 51 | for (int i = 0; i < reader.doutLen; ++i) { 52 | offsetGrams[i] = parser.parseToGram(reader.values[i]); 53 | } 54 | } 55 | 56 | void loop() { 57 | reader.read(); 58 | for (int i = 0; i < reader.doutLen; ++i) { 59 | float gram = parser.parseToGram(reader.values[i]) - offsetGrams[i]; 60 | Serial.println("sensor" + String(i) + ": " + String(gram/1000) + " kg"); 61 | } 62 | Serial.println("at " + String(millis())); 63 | Serial.println(""); 64 | delay(1000); 65 | } 66 | -------------------------------------------------------------------------------- /src/HX711_asukiaaa.cpp: -------------------------------------------------------------------------------- 1 | #include "HX711_asukiaaa.h" 2 | 3 | #define HX711_VBG 1.25f 4 | #define HX711_AVDD 4.2987f // (HX711_VBG*((HX711_R1+HX711_R2)/HX711_R2)) 5 | #define HX711_ADC1BIT (HX711_AVDD / 16777216) // 16777216=(2^24) 6 | #define HX711_PGA 128 7 | 8 | namespace HX711_asukiaaa { 9 | Parser::Parser(float ratedVoltage, float ratedGram, float r1, float r2): 10 | // ratedVoltage(ratedVoltage), 11 | // ratedGram(ratedGram), 12 | // r1(r1), 13 | // r2(r2), 14 | avdd(HX711_VBG * ((r1 + r2) / r2)), 15 | adc1bit(avdd / 16777216), 16 | scale(ratedVoltage * HX711_AVDD / ratedGram * HX711_PGA) { 17 | // offsetGram = 0; 18 | } 19 | 20 | float Parser::parseToGram(int32_t raw) { 21 | return (float) raw * adc1bit / scale; // - offsetGram; 22 | } 23 | 24 | Reader::Reader(int* pinsDout, int doutLen, int pinSlk): 25 | doutLen(doutLen), 26 | pinsDout(pinsDout), 27 | pinSlk(pinSlk) { 28 | values = new int32_t[doutLen]; 29 | } 30 | 31 | Reader::~Reader() { 32 | delete[] values; 33 | } 34 | 35 | void Reader::begin() { 36 | pinMode(pinSlk, OUTPUT); 37 | for (int i = 0; i < doutLen; ++i) { 38 | pinMode(pinsDout[i], INPUT); 39 | } 40 | reset(); 41 | } 42 | 43 | void Reader::reset() { 44 | digitalWrite(pinSlk, HIGH); 45 | delayMicroseconds(100); 46 | digitalWrite(pinSlk, LOW); 47 | delayMicroseconds(100); 48 | } 49 | 50 | int Reader::read(int sumNumber) { 51 | uint64_t sumValues[doutLen]; 52 | for (int i = 0; i < doutLen; ++i) { 53 | sumValues[i] = 0; 54 | } 55 | int32_t data[doutLen]; 56 | // Serial.println("readLen " + String(readLen)); 57 | for (int sumCount = 0; sumCount < sumNumber; ++sumCount) { 58 | int result = readRawOnce(data); 59 | if (result != 0) { return result; } 60 | for (int i = 0; i < doutLen; ++i) { 61 | // Serial.println("data " + String(sumCount) + " " + String(i) + " " + String(data[i])); 62 | sumValues[i] += data[i]; 63 | } 64 | } 65 | for (int i = 0; i < doutLen; ++i) { 66 | values[i] = sumValues[i] / sumNumber; 67 | } 68 | return 0; 69 | } 70 | 71 | int Reader::readRawOnce(int32_t *readValues, unsigned long timeout) { 72 | int readLen = doutLen; 73 | for (int i = 0; i < readLen; ++i) { 74 | readValues[i] = 0; 75 | } 76 | unsigned long startedAt = millis(); 77 | while (!pinsAreReady()) { 78 | if (millis() - startedAt > timeout) { 79 | return Error::timeout; 80 | } 81 | }; 82 | delayMicroseconds(10); 83 | for (int clockCount = 0; clockCount < 24; clockCount++) { 84 | digitalWrite(pinSlk, 1); 85 | delayMicroseconds(5); 86 | digitalWrite(pinSlk, 0); 87 | delayMicroseconds(5); 88 | for (int i = 0; i < readLen; ++i) { 89 | readValues[i] = (readValues[i] << 1) | (digitalRead(pinsDout[i])); 90 | } 91 | } 92 | digitalWrite(pinSlk, 1); 93 | delayMicroseconds(10); 94 | digitalWrite(pinSlk, 0); 95 | delayMicroseconds(10); 96 | // Serial.println("read"); 97 | // Serial.println(readLen); 98 | for (int i = 0; i < readLen; ++i) { 99 | // Serial.println(readValues[i]); 100 | readValues[i] ^= 0x800000; 101 | } 102 | return 0; 103 | } 104 | 105 | bool Reader::pinsAreReady() { 106 | bool allReady = true; 107 | for (int i = 0; i < doutLen; ++i) { 108 | if (digitalRead(pinsDout[i]) == HIGH) { 109 | // Serial.println("notReady " + String(pinsDout[i])); 110 | allReady = false; 111 | } 112 | } 113 | return allReady; 114 | } 115 | } 116 | --------------------------------------------------------------------------------