Arduino nano with 2 load cells fluctuating

98 Views Asked by At

I built an Arduino Scale With 2 20 Kg Load Cells and HX711 Amplifier. 2 load cell connection are on different analog A0 and A1 pins. I am using an Arduino Nano. However my analog outputs are fluctuating up non-stop, even without any weight on scale. This is my code:

#include <Arduino.h>
#include "HX711.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN1 = A0;
const int LOADCELL_SCK_PIN1 = 2;
const int LOADCELL_DOUT_PIN2 = A1;
const int LOADCELL_SCK_PIN2 = 3;
const int tareButtonPin = 4;           // Digital pin connected to the tare button
float loadCell1Value = 0;
float loadCell2Value = 0;
float loadCell1Value_avg = 0;
float loadCell2Value_avg = 0;
float loadCell1Value_getvalue = 0;
float loadCell2Value_getvalue = 0;
float loadCell1Value_getunit = 0;
float loadCell2Value_getunit = 0;
float loadCellValue_getunit = 0;
float loadCellValue_getunit_kg = 0;
int tareButtonState = HIGH;            // Tare button state
int sensorValue1 = 0;  // variable to store the value coming from the sensor
int sensorValue2 = 0;
int x1_D = 0;
int x2_D = 0;

HX711 scale1, scale2;

void setup() {
  Serial.begin(57600);
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print(" ACT Scientific");
  lcd.setCursor(0,1);
  lcd.print("   Tarti v1.0"); 

  delay(1000);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  Baslatiliyor");

  delay(1000);

  //lcd.clear();
  
  pinMode(tareButtonPin, INPUT_PULLUP); // Set tare button pin as input with internal pull-up resistor

/*****************1. SENSOR**************************/
  scale1.begin(LOADCELL_DOUT_PIN1, LOADCELL_SCK_PIN1);

  loadCell1Value = scale1.read();
  Serial.println("Before setting up the scale:");
  Serial.print("read1: \t\t");
  Serial.println(loadCell1Value);      // print a raw reading from the ADC

  loadCell1Value_avg = scale1.read_average(20);
  Serial.print("read average1: \t\t");
  Serial.println(loadCell1Value_avg);   // print the average of 20 readings from the ADC

  loadCell1Value_getvalue = scale1.get_value(5);
  Serial.print("get value1: \t\t");
  Serial.println(loadCell1Value_getvalue);   // print the average of 5 readings from the ADC minus the tare weight (not set yet)

  loadCell1Value_getunit = (scale1.get_units(5), 1);
  Serial.print("get units1: \t\t");
  Serial.println(loadCell1Value_getunit);  // print the average of 5 readings from the ADC minus tare weight (not set) divided
            // by the SCALE parameter (not set yet)

/*****************2. SENSOR**************************/
  scale2.begin(LOADCELL_DOUT_PIN2, LOADCELL_SCK_PIN2);

  loadCell2Value = scale2.read();
  Serial.println("Before setting up the scale:");
  Serial.print("read2: \t\t");
  Serial.println(loadCell2Value);      // print a raw reading from the ADC

  loadCell2Value_avg = scale2.read_average(20);
  Serial.print("read average2: \t\t");
  Serial.println(loadCell2Value_avg);   // print the average of 20 readings from the ADC

  loadCell2Value_getvalue = scale2.get_value(5);
  Serial.print("get value2: \t\t");
  Serial.println(loadCell2Value_getvalue);   // print the average of 5 readings from the ADC minus the tare weight (not set yet)

  loadCell2Value_getunit = (scale2.get_units(5), 1);
  Serial.print("get units2: \t\t");
  Serial.println(loadCell2Value_getunit);  // 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(-459.542);
  scale1.set_scale(127.15);
  //scale1.set_scale(117.45283);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale1.tare();               // reset the scale to 0
  //scale.set_scale(-459.542);
  scale2.set_scale(127.15);
  //scale2.set_scale(117.45283);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale2.tare();               // reset the scale to 0
  
  Serial.println("After setting up the scale:");

  loadCell1Value = scale1.read();
  Serial.print("read1: \t\t");
  Serial.println(loadCell1Value);      // print a raw reading from the ADC

  loadCell1Value_avg = scale1.read_average(20);
  Serial.print("read average1: \t\t");
  Serial.println(loadCell1Value_avg);   // print the average of 20 readings from the ADC

  loadCell1Value_getvalue = scale1.get_value(5);
  Serial.print("get value1: \t\t");
  Serial.println(loadCell1Value_getvalue);   // print the average of 5 readings from the ADC minus the tare weight (not set yet)

  loadCell1Value_getunit = scale1.get_units(5);
  Serial.print("get units1: \t\t");
  Serial.println(loadCell1Value_getunit);  // print the average of 5 readings from the ADC minus tare weight (not set) divided
            // by the SCALE parameter (not set yet)

  loadCell2Value = scale2.read();
  Serial.print("read2: \t\t");
  Serial.println(loadCell2Value);      // print a raw reading from the ADC

  loadCell2Value_avg = scale2.read_average(20);
  Serial.print("read average2: \t\t");
  Serial.println(loadCell2Value_avg);   // print the average of 20 readings from the ADC

  loadCell2Value_getvalue = scale2.get_value(5);
  Serial.print("get value2: \t\t");
  Serial.println(loadCell2Value_getvalue);   // print the average of 5 readings from the ADC minus the tare weight (not set yet)

  loadCell2Value_getunit = scale2.get_units(5);
  Serial.print("get units2: \t\t");
  Serial.println(loadCell2Value_getunit);  // print the average of 5 readings from the ADC minus tare weight (not set) divided
            // by the SCALE parameter (not set yet)

  Serial.println("Readings:");
}

void loop() {


sensorValue1 = analogRead(LOADCELL_DOUT_PIN1);
  sensorValue2 = analogRead(LOADCELL_DOUT_PIN2);
  x1_D = digitalRead(LOADCELL_SCK_PIN1);
  x2_D = digitalRead(LOADCELL_SCK_PIN2);


  
  loadCell1Value_getunit = scale1.get_units(10);
  Serial.print("1. reading:\t");
  Serial.println(loadCell1Value_getunit);
  loadCell2Value_getunit = scale2.get_units(10);
  Serial.print("2. reading:\t");
  Serial.println(loadCell2Value_getunit);
  loadCellValue_getunit = loadCell1Value_getunit + loadCell2Value_getunit;
  Serial.print("Total reading:\t");
  Serial.println(loadCellValue_getunit);
  Serial.print("Total reading (kg):\t");
  Serial.println(loadCellValue_getunit_kg);
  Serial.println("----------------------");

//  loadCellValue_getunit_kg = loadCellValue_getunit/1000;
  loadCellValue_getunit_kg = loadCellValue_getunit;
/*  if(loadCellValue_getunit_kg < 0.01)
  {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Olcum Degeri:");
  lcd.setCursor(0,1);
  lcd.print("0.00");
  lcd.setCursor(7,1);
  lcd.print("g");
  
  }
  else
  { */
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Olcum Degeri:");
  lcd.setCursor(0,1);
  lcd.print(loadCellValue_getunit_kg);
  lcd.setCursor(7,1);
  lcd.print("g");

  Serial.print("1. sensor");
  Serial.println(sensorValue1);
  Serial.print("2. sensor");
  Serial.println(sensorValue2);
  Serial.print("1_D");
  Serial.println(x1_D);
  Serial.print("2_D");
  Serial.println(x2_D);
  
//  }
  
  // Read tare button state
  tareButtonState = digitalRead(tareButtonPin);

  // If the tare button is pressed, perform tare
  if (tareButtonState == LOW) {
  scale1.tare(); 
  scale2.tare();  
  }
  
  delay(300);
}

Here is my output: (https://i.stack.imgur.com/nL5xV.png)

0

There are 0 best solutions below