add links

This commit is contained in:
Clém'S
2016-05-14 08:55:24 +02:00
parent 06fe5d85a1
commit b5126afe2d
9 changed files with 272 additions and 0 deletions
Binary file not shown.
@@ -0,0 +1,44 @@
/* sample for digital weight scale of hx711, display with a HD44780 liquid crtstal monitor
*
* hardware design: syyyd
* available at http://syyyd.taobao.com
*
* library design: Weihong Guan (@aguegu)
* http://aguegu.net
*
* library host on
* https://github.com/aguegu/Arduino
*/
// Hx711.DOUT - pin #A1
// Hx711.SCK - pin #A0
// LCD.RS - pin 12
// LCD.En - pin 11
// LCD.D4 - pin 5
// LCD.D5 - pin 4
// LCD.D6 - pin 3
// LCD.D7 - pin 2
#include <LiquidCrystal.h>
#include <hx711.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Hx711 scale(A1, A0);
void setup() {
lcd.begin(16, 2);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print(scale.getGram(), 1);
lcd.print(" g");
lcd.print(" ");
delay(200);
}
+33
View File
@@ -0,0 +1,33 @@
/* sample for digital weight scale of hx711, display with a HD44780 liquid crtstal monitor
*
* hardware design: syyyd
* available at http://syyyd.taobao.com
*
* library design: Weihong Guan (@aguegu)
* http://aguegu.net
*
* library host on
* https://github.com/aguegu/Arduino
*/
// Hx711.DOUT - pin #A1
// Hx711.SCK - pin #A0
#include "hx711.h"
Hx711 scale(A1, A0);
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(scale.getGram(), 1);
Serial.println(" g");
delay(200);
}
+81
View File
@@ -0,0 +1,81 @@
/*
* Hx711.cpp
*
* Created on: Oct 31, 2012
* Author: agu
*/
#include "hx711.h"
Hx711::Hx711(uint8_t pin_dout, uint8_t pin_slk) :
_pin_dout(pin_dout), _pin_slk(pin_slk)
{
pinMode(_pin_slk, OUTPUT);
pinMode(_pin_dout, INPUT);
digitalWrite(_pin_slk, HIGH);
delayMicroseconds(100);
digitalWrite(_pin_slk, LOW);
averageValue();
this->setOffset(averageValue());
this->setScale();
}
Hx711::~Hx711()
{
}
long Hx711::averageValue(byte times)
{
long sum = 0;
for (byte i = 0; i < times; i++)
{
sum += getValue();
}
return sum / times;
}
long Hx711::getValue()
{
byte data[3];
while (digitalRead(_pin_dout))
;
for (byte j = 3; j--;)
{
for (char i = 8; i--;)
{
digitalWrite(_pin_slk, HIGH);
bitWrite(data[j], i, digitalRead(_pin_dout));
digitalWrite(_pin_slk, LOW);
}
}
digitalWrite(_pin_slk, HIGH);
digitalWrite(_pin_slk, LOW);
data[2] ^= 0x80;
return ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8)
| (uint32_t) data[0];
}
void Hx711::setOffset(long offset)
{
_offset = offset;
}
void Hx711::setScale(float scale)
{
_scale = scale;
}
float Hx711::getGram()
{
long val = (averageValue() - _offset);
return (float) val / _scale;
}
+38
View File
@@ -0,0 +1,38 @@
/* Arduino library for digital weight scale of hx711
*
* hardware design: syyyd
* available at http://syyyd.taobao.com
*
* library design: Weihong Guan (@aguegu)
* http://aguegu.net
*
* library host on
* https://github.com/aguegu/Arduino
*
* Created on: Oct 31, 2012
*/
#ifndef HX711_H_
#define HX711_H_
#include "Arduino.h"
class Hx711
{
public:
Hx711(uint8_t pin_din, uint8_t pin_slk);
virtual ~Hx711();
long getValue();
long averageValue(byte times = 32);
void setOffset(long offset);
void setScale(float scale = 742.f);
float getGram();
private:
const uint8_t _pin_dout;
const uint8_t _pin_slk;
long _offset;
float _scale;
};
#endif /* HX711_H_ */
+76
View File
@@ -0,0 +1,76 @@
Thanks for trying my lib. It has been quite a while, and there are still people email me about how to use it. I really appreciate that.
I have written [an blog about hx711](http://aguegu.net/?p=1327), well, in Chiese...
To config the module right, you need to config the offset and the scale. The `setScale` function should be named `setRatio` to make its function more clear. But for now, just leave it the way it is.
The offset and scale value are got by experiments, which means they vary according to different setups.
To do the test right:
```cpp
#include "hx711.h"
Hx711 scale(A1, A0);
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(scale.averageValue());
delay(200);
}
```
1. place nothing on the scale, then run the example code, which means reset your arduino that runs the example code above.
record the output. that is the `offset`.
2. place a stand weight, like 1kg(1000g), record the output as `w`.
3. Do the math
ratio = (`w` - `offset`) / 1000
4. modify your application code to
```cpp
#include "hx711.h"
Hx711 scale(A1, A0);
void setup() {
Serial.begin(9600);
scale.setOffset(fill in the `offset` value here);
scale.setScale(fill in the `ratio` value here);
}
void loop() {
Serial.println(scale.getGram());
delay(200);
}
```
Then arduino should give you the right result in unit gram.
The init program has set the average value for the first few runs to the offset, with the function `setOffset`. It is written in the lib.
My example code is supposed to work with scale that got a few seconds of zero weight when the program start running. But we still need to do the test to calculate the ratio. It is just happen to be 742 in my setup, but it dose not fit for other setups.
If the setup got weight already when the program start running, and you do not want its getGram() output to be zero. Then you have to setOffset() again manually with the tested value to override the auto offset, just like the example code above.
the averageValue() basically is the raw output. It is not affected by the offest or the scale(ratio). The offset determind your zero point. while the scale(ratio) determind what unit you want the getGram() output to be. With different ratio value, the output can be in g, kg, oz, pound, whatever, as long as the value in the range of the setup and makes sense to you.
```
0g = 0kg = 0oz = 0pound
1000g = 1kg = 35.274oz = 2.20462 pound
```
Just remember that the raw output and the calculated (`getGram()`) output, are linear related.
```
getGram() = (averageValue() - offset) / ratio;
```
Just middle school math. :)