# Interfacing weight sensor with pic microcontroller

#### James E

Joined Nov 2, 2019
3
Hello,

I'm trying to interface weight sensor and of 20th and weight module HX711 to pic16f690

Using 5v as Vref and 2mV is what the 20Kg is said to give out

I don't really know how to go about the calculation in my code to give me Kg.
Any help is appreciated

#### djsfantasi

Joined Apr 11, 2010
6,504
Ok, I’m assuming that by “2mV is what 20Kg is said to give out” that the output of the sensor with a weight of 20Kg is 0.002V.

However, to calculate Kg based on the voltage output by the sensor, we need at least one more point (if sensor output is linear). Then, Kg is a linear function.
Kg = mE + b
Where E is the voltage
and m is (W2-W1)/(E2-E1)
and b is the weight that produces 0V
point 1 is (2mV, 20Kg) and
point 2 is (E2, W2)​

#### ericgibbs

Joined Jan 29, 2010
10,190
hi James.
Look thru these two items.
Change the Arduino .txt to .ino

E
Update:
This is the section ref the Calibration.

// this value is obtained by calibrating the scale with known weights

scale.set_scale(358.f); // edit factor to suit scale

Serial.println(); Serial.println("Scale Tared to Zero:");
scale.tare(); // reset the scale to Zero

// Serial.println(); Serial.println("HX711 LC Calibrated");

#### Attachments

• 3.9 KB Views: 29
• 46.2 KB Views: 34
Last edited:
• James E

#### James E

Joined Nov 2, 2019
3
hi James.
Look thru these two items.
Change the Arduino .txt to .ino

E
Update:
This is the section ref the Calibration.

// this value is obtained by calibrating the scale with known weights

scale.set_scale(358.f); // edit factor to suit scale

Serial.println(); Serial.println("Scale Tared to Zero:");
scale.tare(); // reset the scale to Zero

// Serial.println(); Serial.println("HX711 LC Calibrated");

Thanks so much for your response I really appreciate

I'm actually using PIC not arduino, I haven't used arduino before , I couldn't understand most part of the code

#### James E

Joined Nov 2, 2019
3
Ok, I’m assuming that by “2mV is what 20Kg is said to give out” that the output of the sensor with a weight of 20Kg is 0.002V.

However, to calculate Kg based on the voltage output by the sensor, we need at least one more point (if sensor output is linear). Then, Kg is a linear function.
Kg = mE + b
Where E is the voltage
and m is (W2-W1)/(E2-E1)
and b is the weight that produces 0V
point 1 is (2mV, 20Kg) and
point 2 is (E2, W2)​

Pls can you go about the explanation deeply with details

Because what I did was

1024/5 =204
Then 204*5= 1020
And 1020/1024 = 0.997
0.997*20 = 19.9

This was the calculation I did in order to get the real life value

Using the 10 bit microchip and 5V power supply

Pls I'm I wrong
Materials I'm reading are not breaking it down into simple terms

Pls you can help further

The whole essence of the project is to read tare weight and store it and after that minus it when the container has content to give just the content alone

Thanks

#### MrChips

Joined Oct 2, 2009
21,325
@djsfantasi showed how it is done.

Let's go over this again. I am going to use different notation.
Your weight sensor and PIC will give you a number for every weight on the scale.
Let us say
W = weight
N = number
The mathematical relation between W and N is the equation
W = a x N + b
You need to determine the values of a and b. In order to find a and b you need two measurements using two different weights. You will do this to calibrate the scale.

W1 gives the number N1
W2 gives the number N2.

Now you can calculate

a = (W2 - W1) / (N2 - N1)

Now that you have a, calculate b

b = W1 - a x N1
or
b = W2 - a x N2

Now, if you wish, you can use the tare weight where W1 = 0.
Then
a = (W2 - 0 ) / (N2 - N1)
b = - a x N1

Once you have calculated a and b your job is done.
Check your answers by plugging in N1 and N2 into the equation and check that the results are W1 and W2 respectively.
W = a x N + b

• atferrari and djsfantasi

#### djsfantasi

Joined Apr 11, 2010
6,504
Thanks @MrChips

I sometimes get brain freeze when I supply an answer step by step and a TS asks me to explain it in detail. It takes me a while to understand it could be that s/he doesn’t understand... And this momentary pause in brain function makes it difficult for me to look at it in another way, which you did so eloquently.

• atferrari

#### John P

Joined Oct 14, 2008
1,808
I looked up HX711 and it seems to be a strain gauge amplifier combined with a 24-bit A/D converter and SPI interface. So the precision of the PIC A/D isn't going to be relevant! If you use the input range with a gain of 128, you'd have +/- 20mV input swing available, so you're not using much of the chip's range, but with 24 bits of precision, it should be OK.

That seems to say, if you're using 10% of the full range with a 10Kg load, allowing for half the range to be for negative inputs which you won't use, you should expect to see 838861 counts full scale, or 84 per gram. In theory. I bet it's not really that good!

#### petehand

Joined Jun 2, 2017
1
The sensitivity of a strain gauge is given in millivolts per volt at full scale. So if this 20kg strain gauge is specified at 2mV, that means the output at 20kg is 2mv times the applied excitation voltage. If you supply it with 5V, the output is 10mV. The DC level of the output will be half the supplied excitation, so with 5V applied the DC output level is 2.5V.

If you feed that into an HX711 with gain set to 128 and a reference voltage of 5V, the maximum output will be (1.28/5) = 0.256 of full scale, or 2,147,483 (0x20C49B) - half what you might expect because the HX711 goes both plus and minus, so full scale is +/- 2.5V, not 5V. That's 22 bits of resolution. You'll probably want to truncate it to 16 bits, because (trust me) your strain gauge isn't linear to more than 16 bits, and you'll still have a resolution of 305 milligrams.

To convert to engineering units, put a test weight on and divide the reading you get by the test weight in grams. That gives you a conversion constant - X = reading per gram. Divide an unknown by X to get the answer in grams.

#### swr999

Joined Mar 30, 2011
27
Hello @James E,

As pointed out by @John P, the HX711 is an integrated analog-to-digital (ADC) module with a 24-bit delta-sigma converter and digital SPI serial output. It is designed to read analog voltages from load cells, strain gauges, and similar Wheatstone-bridge type sensors. Sparkfun sells a breakout board for the HX711 and has a tutorial for it. You might have a look at: