Ks0087 keyestudio DIY Electronic Scale

From Keyestudio Wiki
Jump to navigation Jump to search

keyestudio DIY Electronic Scale


thumb

Introduction

Electronic scale is commonly seen in our daily life, with function of weighting. Professional scales on market have more functions with higher precision. Is there any possibility for enthusiasts to DIY their own electronic scale with what they have learned? Absolutely you can! It is easy to make an open source electronic scale. You just need a weighting module, an AD chip (hx711) specialized in weighting and an Arduino with corresponding library. Everything becomes so easy!

Principle

Electronic scale weigh force according to slight deformation causing by pressing metal. It uses the most commonly used strain gauge to measure deformation. In a typical application, utilize the strain gauge adhered on face of this sensor, to measure the force bore by the sensor. HX711 is a chip of 24 bit A\D converter, designed for high precision weighing sensor. Compared with other same-type chips, this chip is featured with high integration, quick response and strong anti-interference, integrated external circuit needed by other chips including stabilized voltage supply and on-chip clock oscillator. It decreases cost of complete machine of the scale, and in the same time improve its performance and reliability. The interface and programs of this chip and MCU chip are very simple. All control signals are driven by pins, without programming register of inner chip. Input and select switch to choose arbitrarily channel A or B, connecting it to a low-noise programmable amplifier. The gain in channel A is 128 or 64 bit, and its corresponding amplitude of different input signals in full loaded is respectively ±20mV or ±40mV. The gain in channel B is fixed 32, used to detect system parameters. Stabilized voltage supply offered by chip can directly supply power for external sensor and A\D converter, there no need for additional analog power on main board. Clock oscillator doesn't need any external device. Automatic reset simplifies initialization when opening up.

Features

Channel A or B can choose different input.
The gain is 64 or 128 in the low-noise programmable amplifier.
Stabilized voltage supply offered by chip can directly supply power for external sensor and A\D converter.
Clock oscillator doesn't need any external device. If necessary, it can use external crystal oscillator or clock.
Automatic reset function
All control signals are driven by pins, without programming register of inner chip.
Alternative 10Hz or 80Hz of output data rate

Kit List

No. Product Name Quantity Picture
1 keyestudio UNO Board 1
thumb
2 I2C 1602 LCD 1
thumb
3 keyestudio Digital Push Button 1
thumb
4 keyestudio HX711 Weighing Module 1
thumb
5 5KG Rheostat Sensor 1
thumb
6 Tabula Board(Upper Board) 1
thumb
7 Tabula Board(Bottom Board) 1
thumb
8 keyestudio R=120mm Bearing Acrylic Board 1
thumb
9 keyestudio 15*11*4 Acrylic Board 4mm Spacer 1 thumbthumb
10 50g Weight 1
thumb
11 100g Weight 1
thumb
12 Self-adhesion 3M Crashproof Transparent Colloidal Particle∮10*5(4 Granules) 1
thumb
13 M3*6MM Round Head Screw 23
thumb
14 M3 Nickeling Nut 2 thumbthumb
15 M4*10MM Round Head Screw 2 thumbthumb
16 Hexagonal Copper Fistular M3*10MM 8
thumb
17 Hexagonal Copper Fistular M3*40MM 1 thumbthumbthumb
18 Female to Female Dupont Line15CM 20
thumb
19 USB Cable 1
thumb
20 18650 Battery Case 1
thumb

Connection Method

1)VCC can be any value among 2.6-5.5 . Because we use Arduino, just connect to 5V, and to GND.
2)Connect SCK to Pin 9, DT to Pin 10 on Arduino; you can change the pins in program.
3)Connect E+ to positive driving voltage, E- to negative, A+ to positive output voltage and A- to negative on bridge sensor.(E+ to red wire, E- to black wire, A+ to green or blue wire , A- to white wire )
4)Connect B+ and B- to sensor of channel B, or connect to power supply through partial pressure circuit, in order to detect supply voltage. If not, you had better connect to GND. But disconnect GND is OK.

Connection Diagram


thumb

Debugging Step

The following is a specific applied sample:

Parameters of sensor shown as following:
Full-range Output Voltage=Excitation x Sensitivity 1.0mV/V
For example: 5V supply voltage multiplied by 1.0mV/V sensitivity equals to 5mV full-range.
In fact when supply voltage of the module is 5V, supply voltage of sensor is 4V, so full-range voltage of the sensor is 4mV. or you can choose 128 bit, the highest gain factor in channel A , to get highest precision.

Figure of a Complete Electronic Scale
thumb

In the picture , the module connected to sensor is HX711, Arduino UNO is in the lower right corner, bridge sensor is hung on lazy arm(my connection mode is to use sling to measure tension value). It is better to choose the shorter wire between HX711 module and sensor. If it is too long, the module will get disturbed and it has better exceeding 30cm. If it is necessary to lengthen, you can use wire that can shield electromagnetism and signal amplifier.

1. Open sample codes in Arduino, and you can see that they are very easy:

 
#include <HX711.h> // including library
HX711 hx(9, 10); // digital pins definition
void setup() {
  Serial.begin(9600);
}
void loop()
{
 double sum = 0;    // in order to decrease error, taking the average of 10 values 
  for (int i = 0; i < 10; i++) // the more the cycling the higher the precision, and the more time    
sum += hx.read();  // accumulating
  Serial.println(sum/10); // averaging to get mean deviation
}

These sample codes are very simple, but there are a lot of sample without function:

 
HX711(byte sck, byte dout, byte amp = 128, double co = 1); // define SCK and DOUT pin, gain factor(default 128) and correction factor(default 1)
void set_amp(byte amp); // changing gain factor and corresponding channel, and invoking read() at least one time to work
bool is_ready(); // whether turning beck hx711 is useful, and it is invoked in read()
long read(); // turning beck sensor voltage value, if hx711 not working, the program stops this function
double bias_read(); // turning beck:(read() - deviant) * correction factor
void tare(int t = 10); // adding tare to deviant and influencing the invoking of read()
void set_co(double co = 1); // modifying correction factor(default 1)
void set_offset(long offset = 0); // modifying deviant (default 1) , you can see hx711 also can use 4 parameters mode to define, and stipulate gain factor and correction factor. You can change gain and correction factor at any time during program running , and utilize deviant to remove tare.
The only one needed to explain is the first function HX711 hx(9, 10); // only defining SCK and DOUT pin, the default of gain of AMP in channel A is 128 bit, the default of correction factor is 1;
HX711 hx(9, 10, 64); // defining SCK and DOUT pin, the default of gain of AMP in channel A is 64 bit, the default of correction factor is 1;
HX711 hx(9, 10, 32, 1.4); // defining SCK and DOUT pin, the default of gain of AMP in channel A is 32 bit, the default of correction factor is 1.4;

As for choosing channel and gain factor mentioned in materials, The gain in channel A is 128 or 64 bit, and its corresponding full-load voltage is respectively ±20mV or ±40mV. The gain in channel B is fixed 32 and full-load voltage is 80mV. Input voltage of each channel can’t be more than their full-load voltage. Of course, you can change channel and gain factor at any time, as long as you use function of set_amp(amp) during programming, but the value of amp only can be 128, 64 or 32.

Stressing again, if you choose gain factor of 32 bit, the data read out is of channel B.

Firstly, use samples coming with library to test, and you can see the value after hanging on 100g weight on a tray:

30545.50
30515.30
30495.20
30489.80
30469.70
30503.20
30420.90
30483.90
30505.20
30509.00
30531.80
30546.70
30542.90
30574.00
30540.20
30537.80
30468.10
30500.60
30525.90
30488.50

After a 50g weight is taken out, the value are:

7348.90
7386.10
7443.80
7426.40
7351.40
7385.70
7334.80
7401.60
7298.20
7316.10
7297.10
7327.40
7342.40
7319.70
7354.20
7360.10
7351.40
7286.40
7382.20
7276.70
7344.30
7292.20
7308.40

Calculating roughly:

30500 - 7300 = 23200 so the correction factor is about 50 / 23200= 0.002155172


2. Then the codes are (at the moment you can decrease properly reading rate, and add “delay” , like reading value at 30 seconds at one time):

#include <HX711.h>
HX711 hx(9, 10, 128, 0.002155172);
void setup() {
  Serial.begin(9600);
}
void loop() {
  delay(500);
  double sum = 0;
  for (int i = 0; i < 10; i++)
    sum += hx.read();
  Serial.println(sum/10);
}

After compiling codes into Arduino, take out all weights, and just keep the body, you can get:

-15966.10
-15881.10
-15959.40
-15979.20
-16027.20
-16018.50
-16014.00
-16011.80
-16050.60
-16008.20
-15996.40
-15999.60
-16011.00
-15992.10
-15936.60
-15984.50
-16027.30
-16068.00
-16014.70
-15981.80
-16060.70
-16019.40

3.That means the deviant approximates to -16000. So we can invoke the function of deviant in setup(using bias_read() to read value with correction factor and deviant, compared with read() as a reference)

#include <HX711.h>
HX711 hx(9, 10, 128, 0.002155172);
void setup() {
  Serial.begin(9600);
  hx.set_offset(-16000);

}
void loop() {
  delay(500);
  double sum0 = 0;
  double sum1 = 0;
  for (int i = 0; i < 10; i++) {
    sum0 += hx.read();
    sum1 += hx.bias_read();
  }
  Serial.print(sum0/10);
  Serial.print(" ");
  Serial.println(sum1/10);
}

The data read out is changed into:

-15971.20 0.07
-15992.20 0.08
-15983.10 0.03
-15960.10 0.06
-15963.70 0.08
-15983.00 0.03
-15922.40 0.14
-15979.50 0.06
-15967.60 0.10
-16054.70 -0.09
-15985.30 0.02
-15944.40 0.10
-15945.80 0.13
-15982.50 0.07
-16043.00 -0.07
-16003.20 -0.02
-16004.30 -0.02
-15981.40 0.02
-15968.00 0.01
-15976.80 0.04
-16002.70 -0.01

Check again the data after putting on a 50g weight:

7159.20 49.85
7127.00 49.87
7115.10 49.78
7119.90 49.77
7140.40 49.82
7170.70 49.96
7201.40 50.02
7195.10 49.97
7191.50 50.00
7208.10 50.04
7213.30 50.02
7233.00 50.09
7216.30 50.02
7254.00 50.09
7252.00 50.09
7207.40 50.06
7244.10 50.08
7246.60 50.13
7242.40 50.06
7210.80 50.04

Putting on another 100g weight:

30276.70 99.73
30311.80 99.80
30306.20 99.79
30293.50 99.80
30310.80 99.76
30291.70 99.74
30286.40 99.75
30313.20 99.82
30283.80 99.78
30333.50 99.83
30335.40 99.89
30333.40 99.83
30287.20 99.75
30268.10 99.76
30292.30 99.81
30348.50 99.86

You can see the first decimal has changed for 0.2. That means reading correction factor is not precise enough, but it fits completely precision at 1g.

4. Next, it is to perfect program and increase removing tare function. Install a button on Arduino. In order to prevent electromagnetic interference and mistakes, we use a button usually inputting high level. When pressing down the button, it inputs low level. Connect this button to D0 port:

#include <HX711.h>
HX711 hx(9, 10, 128, 0.002155172);
void setup() {
Serial.begin(9600);
  hx.set_offset(-16000);
}
void loop() {
  if(digitalRead(0) == LOW) hx.tare();
  double sum0 = 0;
  double sum1 = 0;
  for (int i = 0; i < 10; i++) {
    sum0 += hx.read();
    sum1 += hx.bias_read();
  }
  Serial.print(sum0/10);
  Serial.print(" ");
  Serial.println(sum1/10);
}

Every time you press the button, tare is removed.

5. One more code added 1602 liquid crystal:

#include <HX711.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
HX711 hx(9, 10, 128, 0.002155172);
LiquidCrystal_I2C lcd(0x27,16,2); 
void setup() {
  Serial.begin(9600);
  hx.set_offset(-16000);
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  lcd.begin(16,2);

}
void loop() {
  if(digitalRead(0) == LOW) hx.tare();
  double sum0 = 0;
  double sum1 = 0;
  for (int i = 0; i < 10; i++) {
    sum0 += hx.read();
    sum1 += hx.bias_read();
  }
  lcd.clear();  
  lcd.setCursor(2,0);
  lcd.print("keyes-scale");
  lcd.setCursor(5, 1);
  lcd.print(sum1/10);
  lcd.print("g");
  delay(10);
 } 

Due to influences of different factor ,such as temperature, environment, wires and more, actual error is about +-0.5.

Result

Put a 50g weight on tray, and the result is shown as below.
thumb

Resources

Video

http://www.keyestudio.com/wp/ks0087/

Datasheet

Get libraries of HX711, LiquidCrystal_I2C and Wire from:

https://drive.google.com/open?id=1HgQW1Nj-JnY6bU6qOFNVjpEVS4rYRjAL

PDF:

https://drive.google.com/open?id=1I612HMe8-Lfsidt58-SHoHcY7MWXbDop

Codes:

https://drive.google.com/open?id=1wV-mmjQs2EfQLrBuDc7OOUSIdSMAkn38

Buy from

Official Website

http://www.keyestudio.com/keyestudio-diy-electronic-scale.html

Amazon Store

https://www.amazon.com/Keyestudio-Electronic-Weighing-Scale-Arduino/dp/B01MFHGU6I/