Hi, I am doing an IOT weight scale using Mega and ESP-01 to measure weight of objects and identify whether it is within the acceptable range or not. I referred to this website Weighing Machine using Arduino Load Cell & HX711 Module for the code and tried to modify according to my needs. I tried out his code and the value of the load cell is quite consistent (-1 to 1) after calibration which is acceptable for me. However, when I uploaded my code to the device, the values flutter too much and is not consistent. Does someone have any idea why is it like this?
Here is my code:
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#define DT A0
#define SCK A1
#define sw 29 //input push button
#define buzzer 31 //output buzzer
#define led_red 30 //output led red
#define led_green 28 //output led green
LiquidCrystal lcd(7,6,5,4,3,2);
const byte numRows= 4;//Rows and columns of the keypad
const byte numCols= 4;
char keymap[numRows][numCols]= //Keypad map
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {38,39,40,41}; //Keypad 8 pins
byte colPins[numCols]= {42,43,44,45};
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
//Authorization token, WiFi ID and password for Blynk
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
#define EspSerial Serial1
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
int w;
float range;
long sample=0;
float val=0;
long count=0;
int reject=0;
int blinkTime = 500;
bool newWeight = false;
//reading data from HX711 module and return its output
unsigned long readCount(void)
{
unsigned long Count;
unsigned char i;
pinMode(DT, OUTPUT);
digitalWrite(DT,HIGH);
digitalWrite(SCK,LOW);
Count=0;
pinMode(DT, INPUT);
while(digitalRead(DT));
for (i=0;i<24;i++)
{
digitalWrite(SCK,HIGH);
Count=Count<<1;
digitalWrite(SCK,LOW);
if(digitalRead(DT))
Count++;
}
digitalWrite(SCK,HIGH);
Count=Count^0x800000;
digitalWrite(SCK,LOW);
return(Count);
}
//calibrate the load sensor with 100g load
void calibrate()
{
lcd.clear();
lcd.print("Calibrating...");
lcd.setCursor(0,1);
lcd.print("Please Wait...");
for(int i=0;i<100;i++)
{
count=readCount();
sample+=count;
}
sample/=100;
lcd.clear();
lcd.print("Put 100g & wait");
count=0;
while(count<1000)
{
count=readCount();
count=sample-count;
}
lcd.clear();
lcd.print("Please Wait....");
delay(2000);
for(int i=0;i<100;i++)
{
count=readCount();
val+=sample-count;
}
val=val/100.0;
val=val/100.0; // put here your calibrating weight
lcd.clear();
}
void setup()
{
Serial.begin(9600);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
pinMode(buzzer, OUTPUT);
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(SCK, OUTPUT);
pinMode(sw, INPUT);
lcd.begin(16, 2);
lcd.print(" Weight ");
lcd.setCursor(0,1);
lcd.print(" Measurement ");
delay(1000);
lcd.clear();
calibrate();
//call selectType() function every second
timer.setInterval(1000L, selectType);
}
void sendSensor(){
count= readCount();
w=(((count-sample)/val)-2*((count-sample)/val));
newWeight = true;
}
void selectType(){
lcd.clear();
lcd.print("Type:");
char customKey = myKeypad.getKey();
if(customKey!=NO_KEY){
if(customKey=='1')
{
range=252;
}
else if(customKey=='2')
{
range=319;
}
else if(customKey=='3')
{
range=272.5;
}
else if(customKey=='4')
{
range=310;
}
else if(customKey=='5')
{
range=285.5;
}
else if(customKey=='6')
{
range=280;
}
else if(customKey=='7')
{
range=280;
}
else if(customKey=='8')
{
range=344;
}
else if(customKey=='9')
{
range=346;
}
else if(customKey=='0')
{
range=288.5;
}
else if(customKey=='A')
{
range=307.5;
}
else if(customKey=='B')
{
range=252;
}
else if(customKey=='C')
{
range=252;
}
else if(customKey=='D')
{
range=288;
}
else if(customKey=='*')
{
range=233;
}
else if(customKey=='#')
{
range=337;
}
}
lcd.print(customKey);
lcd.setCursor(10,0);
lcd.print(range);
lcd.setCursor(0,1);
if(newWeight){
lcd.print(w);
newWeight = false;
}
//recalibrate the system and reset all value to 0
if(digitalRead(sw)==1)
{
val=0;
sample=0;
w=0;
count=0;
reject=0;
calibrate();
range=0;
}
//call the sendSensor() function after 1second
int callFunction1 = timer.setTimeout(60000, detectWeight);
}
void detectWeight(){
if(newWeight){
delay(3000);
Blynk.virtualWrite(V3,w); //write the weight to Virtual pin 3
//check if the weight measured is more than 30g and out of the acceptable range
if((w > 30) && ((w < range*0.98)||(w> range*1.02))){
lcd.clear();
lcd.print("Weight");
lcd.setCursor(8,0);
lcd.print(w);
lcd.setCursor(0,1);
lcd.print("REJECTED!!");
//saveWeight();
blinkyBlinky(5, blinkTime);//trigger red LED
blinkyBuzzer(5,300);//trigger buzzer
noTone(buzzer);
reject++;
}
//if weight measured is within acceptable range
else if ((w>=range*0.98) && (w<=range*1.02)){
digitalWrite(led_green, HIGH);
delay(2000);
reject=0;
digitalWrite(led_green, LOW);
}
//if 10 consecutive objects measured are not within the range
if(reject==10)
{
Blynk.notify("ESP8266 Alert - Insufficient Weight!");//send notification to phone
Blynk.email("ESP8266 Alert", "Insufficient Weight!");//send alert email
blinkyBuzzer(10,100);//trigger buzzer
noTone(buzzer);
reject=0;//reset the counter to 0
}
newWeight = false;
}
}
void loop()
{
Blynk.run();
timer.run();
sendSensor();
}
//function to blink red LED for 5 times
void blinkyBlinky(int repeats, int t)
{
for (int i = 0; i < repeats; i++)
{
digitalWrite(led_red, HIGH);
delay(t);
digitalWrite(led_red, LOW);
delay(t);
}
}
//function to trigger buzzer to buzz for 10 times
void blinkyBuzzer(int repeats, int t)
{
for (int i = 0; i < repeats; i++)
{
tone(buzzer, 2000);
delay(t);
tone(buzzer, 1000);
delay(t);
}
}