Calibration of Loadcell
#include "HX711.h"
#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);
float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup
void setup() {
Serial.begin(9600);
Serial.println("HX711 calibration sketch");
Serial.println("Remove all weight from scale");
Serial.println("After readings begin, place known weight on scale");
Serial.println("Press + or a to increase calibration factor");
Serial.println("Press - or z to decrease calibration factor");
scale.set_scale();
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale.
Useful in permanent scale projects.
Serial.println(zero_factor);
}
void loop() {
scale.set_scale(calibration_factor); //Adjust to this calibration factor
Serial.print("Reading: ");
Serial.print(scale.get_units(), 1);
Serial.print(" lbs"); //Change this to kg and re-adjust the calibration factor if you
follow SI units like a sane person
Serial.print(" calibration_factor: ");
Serial.print(calibration_factor);
Serial.println();
if(Serial.available())
{
char temp = Serial.read();
if(temp == '+' || temp == 'a')
calibration_factor += 10;
else if(temp == '-' || temp == 'z')
calibration_factor -= 10;
}
}
HX711 (transmitter)
#include "HX711.h"
#include <Servo.h>
HX711 scale;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false;
int pos = 0;
int b ;
float currentmass;
float mass;
float outmass;
void setup() {
Serial.begin(9600);
Serial.println("HX711 Demo");
myservo.attach(9);
myservo.write(0);
inputString.reserve(200);
Serial.println("Initializing the scale");
scale.begin(A1, A0);
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from
the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the
ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the
ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(665650.f);//667373 665650 // this value is obtained by
calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from
the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the
ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the
ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
void loop()
{
mass = scale.get_units(10)*1000;
outmass = mass*100;
if (stringComplete) {
int b = inputString.toInt();
currentmass = mass - b;
// Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
while(currentmass<=mass){
mass = scale.get_units(10)*1000;
outmass = mass*100;
for (pos = 0; pos <= 30; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
Serial.println(outmass,2);
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 30; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
Serial.println(outmass,2);
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
;
// Serial.print("Mass : \t");
Serial.println(outmass,2);
scale.power_down(); // put the ADC in sleep mode
delay(100);
scale.power_up();
delay(1000);
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Keypad
#include <Keypad.h> //niload library for keypad..
#include <LiquidCrystal.h>
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 14, 5, 4, 3, 2);
float weight; //variable for mass
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 6, 7, 8, 9 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 10, 11, 12 };
String input;
int pos =0;// for servo 0degree nakaclose
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int counter;
void setup()
{ lcd.begin(16, 2);// initialized lcd for 16x2
// Print a message to the LCD.
lcd.print("INPUT :");
lcd.print(input);
lcd.setCursor(0, 1);
lcd.print("Total:");
// reserve 200 bytes for the inputString:
inputString.reserve(200);
Serial.begin(9600); //serial communication
}
int output;
void loop()
{
//output = input.toInt()*100;
if (stringComplete) {
// Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
//String value = Serial.readString();
lcd.clear();
lcd.print("INPUT:");
lcd.print(input);
lcd.setCursor(0, 1);
lcd.print("Total:");
if(weight<0){ lcd.print("0.00");}
else{ lcd.print(weight);}
//weight = value.toInt();
weight = weight/100;
lcd.setCursor(0, 1);
lcd.print("Total:");
if(weight<0){ lcd.print("0.00");}
else{ lcd.print(weight);}
}
char key = kpd.getKey();
lcd.setCursor(7,0);
if(key) // Check for a valid key.
{
switch (key)
{
case '*':
lcd.setCursor(0, 1);
counter =0;
input ="";
lcd.clear();
lcd.print("INPUT:");
lcd.setCursor(0, 1);
lcd.print("Total:");
if(weight<0){ lcd.print("0.00");}
else{ lcd.print(weight);}
break;
case '#':
Serial.println(input);// to send data
lcd.setCursor(0, 1);
counter =0;
input ="";
lcd.clear();
lcd.print("Send OK");
lcd.setCursor(0, 1);
lcd.print(input);
lcd.print("grams");
break;
default:
counter = counter+1;
if(counter<6){
input +=key;
// print the number of seconds since reset:
lcd.print(input);
lcd.setCursor(0, 1);
lcd.print("Total:");
if(weight<0){ lcd.print("0.00");}
else{ lcd.print(weight);}
}
else{
lcd.setCursor(0, 1);
counter =0;
input ="";
lcd.clear();
lcd.print("INPUT:");
lcd.setCursor(0, 1);
lcd.print("Total:");
lcd.print(weight);
}
}
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
weight = inputString.toInt();//string to integer conversion
}
}
}