Hello everyone!
I'm currently building a Arduino scale from the YouTube Channel Electronoobs.
Video Link:
How STRAIN GAUGE Works | Precision SCALE With Arduino - YouTube
These are the materials I'm using:
- Arduino Uno R3
- 1 - 5kg Load Cell with HX711
- HD44780 16x2 LCD with I2C Module
- Potentiometer, Push Buttons...
This is the Code:
#include <Q2HX711.h>
//LCD config
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,20,4);
//Pins
const byte hx711_data_pin = 3;
const byte hx711_clock_pin = 2;
int tara_button = 8;
int mode_button = 11;
Q2HX711 hx711(hx711_data_pin, hx711_clock_pin);
float y1 = 2831.0;
long x1 = 0L;
long x0 = 0L;
float avg_size = 10.0;
float tara = 0;
bool tara_pushed = false;
bool mode_pushed = false;
int mode = 0;
float oz_conversion = 0.035274;
void setup() {
Serial.begin(9600);
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0);
PCMSK0 |= (1 << PCINT3);
pinMode(tara_button, INPUT_PULLUP);
pinMode(mode_button, INPUT_PULLUP);
lcd.init();
lcd.backlight();
delay(1000);
for (int ii=0;ii<int(avg_size);ii++){
delay(10);
x0+=hx711.read();
}
x0/=long(avg_size);
Serial.println("Add Calibrated Mass");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Add Calibrated ");
lcd.setCursor(0,1);
lcd.print(" Mass ");
int ii = 1;
while(true){
if (hx711.read()<x0+10000)
{
}
else
{
ii++;
delay(2000);
for (int jj=0;jj<int(avg_size);jj++){
x1+=hx711.read();
}
x1/=long(avg_size);
break;
}
}
Serial.println("Calibration Complete");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Calibration ");
lcd.setCursor(0,1);
lcd.print(" Complete ");
}
void loop() {
long reading = 0;
for (int jj=0;jj<int(avg_size);jj++)
{
reading+=hx711.read();
}
reading/=long(avg_size);
float ratio_1 = (float) (reading-x0);
float ratio_2 = (float) (x1-x0);
float ratio = ratio_1/ratio_2;
float mass = y1*ratio;
if(tara_pushed)
{
tara = mass;
tara_pushed = false;
Serial.print("TARA");
Serial.print(".");
lcd.setCursor(0,0);
lcd.print(" TARA ");
lcd.setCursor(0,1);
lcd.print(" . ");
delay(300);
Serial.print(".");
lcd.setCursor(0,0);
lcd.print(" TARA ");
lcd.setCursor(0,1);
lcd.print(" .. ");
delay(300);
Serial.println(".");
lcd.setCursor(0,0);
lcd.print(" TARA ");
lcd.setCursor(0,1);
lcd.print(" ... ");
delay(300);
}
if(mode_pushed)
{
mode = mode + 1;
mode_pushed = false;
if(mode > 2)
{
mode = 0;
}
}
if(mode == 0)
{
Serial.print(mass - tara);
Serial.println(" g");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" SCALE! ");
lcd.setCursor(0,1);
lcd.print(mass - tara);
lcd.print(" g");
}
else if(mode == 1)
{
Serial.print(mass - tara);
Serial.println(" ml");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" SCALE! ");
lcd.setCursor(0,1);
lcd.print(mass - tara);
lcd.print(" ml");
}
else
{
Serial.print((mass - tara)*oz_conversion);
Serial.println(" oz");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" SCALE! ");
lcd.setCursor(0,1);
lcd.print((mass - tara)*oz_conversion);
lcd.print(" oz");
}
}
ISR(PCINT0_vect)
{
if (!(PINB & B00000001))
{
tara_pushed = true;
}
if (!(PINB & B00001000))
{
mode_pushed = true;
}
}
The Wiring:
Load Cell to HX711:
Red to E+
Black to E-
White to A-
Green to A+
HX711 to Arduino Uno:
GND to GND
DT to Digital Pin 3
SCK to Digital Pin 2
VCC to VCC
HD44780 16x2 LCD (with I2C) to Arduino Uno:
GND to GND
VCC to VCC
SDA to A4
SCL to A5
(Pushbuttons connections aren't necessary)
A User had a simillar Problem and he had been suggested to solder again his LCD. So I'm also posting some photos of my "scale":






