Load Cell Code
Load Cell Code
h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#define DOUT 3
#define CLK 2
float weight=0;
const int cal=6;
const int up=5;
const int down=4;
const int x100=1;
int readingup,readingdown,readingcal,readingx100,cf=0,a[2]={100,1000},i=0;
float calibration_factor =-204100; //this value is calculated according to
instructions in report
HX711 scale(DOUT, CLK); //using the Hx711 in 128 gain mode by default
void setup()
{
lcd.begin(16,2);
pinMode(cal,INPUT);
pinMode(up,INPUT);
pinMode(down,INPUT);
pinMode(x100,INPUT);
scale.set_scale(); // initially calibrating the scale with zero value
scale.tare(); //resets the scale to 0
long zero_factor = scale.read_average(10); //stores the average of 10 readings
from the ADC when there is no weight on pan
}
void loop()
{
readingcal=digitalRead(cal);
if(readingcal==HIGH) //this section gets activated when CAL button is ON for
calibration
{
if(cf==0)//in this section we will give some instructions for calibration once
{
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Calibration ");
lcd.setCursor(4,1);
lcd.print("Starts! ");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Place 1kg weight");
lcd.setCursor(0,1);
lcd.print("or known weight.");
delay(4000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press '+' when ");
lcd.setCursor(0,1);
lcd.print("weight is small.");
delay(2500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press '-' when ");
lcd.setCursor(0,1);
lcd.print("weight is big. ");
delay(2500);
cf=1; // this variable helps us to read the instructions once and for the
rest of time calibration() function is called
}
calibration();
}
else //otherwise to measure the weights
{
display2();
cf=0; //this variable helps us to read the instructions ONCE when the CAL
button is switched ON
}
}
void calibration() //this is the general function of Calibration
{
readingup=digitalRead(up); //reads the logic at pin 5 to increase the calibration
factor
readingdown=digitalRead(down); //reads the logic at pin 4 to decrease the
calibration factor
readingx100=digitalRead(x100); //reads the logic at pin 1 to inc/dec the
calibration factor by 100 times
if(readingx100==HIGH)
{
i=1; //i is the index of array used at top that contains only two values 10 and
100,
//it provides the factor by which we are inc/dec the calibration factor
}
else {i=0;} //if x100 is off then by default we increase or decrease the
calibration factor by 10 times
if(readingup==HIGH)
{
calibration_factor+=a[i];//increasing the calibration factor by 10/100 times
delay(80);
}
if(readingdown==HIGH)
{
calibration_factor-=a[i];//decreasing the calibration factor by 10/100 times
delay(80);
}
display1();//as we used two different displays for two modes. NOrmal mode and
Calibration mode..
//this is for calibration mode
}
void display1()//calibration mode display
{
scale.set_scale(calibration_factor); //the value in calibration factor is
obtained by calibration it with known weights
weight=scale.get_units(); //stores the average of weight readings from the ADC
according to the calibration factor
if(weight<0)
{weight=0;}
lcd.setCursor(0,0);
lcd.print("Weight: ");
lcd.setCursor(8,0);
lcd.print(weight,4); //displays the weight in 4 decimal places only for
calibration
lcd.setCursor(14,0);
lcd.print("kg");
lcd.setCursor(0,1);
lcd.print("Value: "); //display the calibration factor so that it is easy to
inc/dec it and reading it at the same time
lcd.setCursor(7,1);
lcd.print(calibration_factor);
}
void display2()//display of normal mode
{
scale.set_scale(calibration_factor);
weight=scale.get_units();
if(weight<0)
{weight=0;}//if the weight is negative then show it zero
lcd.setCursor(0,0);
lcd.print("**Weight Scale:*");
lcd.setCursor(0,1);
lcd.print("Weight: ");
lcd.setCursor(8,1);
lcd.print(weight,2);//displaying the weight in two decimal places. As 0.01
accuracy is required
lcd.setCursor(12,1);
lcd.print("kg ");
}