safwan092

Arduino Scale With 5kg Load Cell and HX711 Amplifier

Mar 3rd, 2021
520
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Setup your scale and start the sketch WITHOUT a weight on the scale
  3.   Once readings are displayed place the weight on the scale
  4.   Press +/- or a/z to adjust the calibration_factor until the output readings match the known weight
  5.   Arduino pin 5 -> HX711 CLK
  6.   Arduino pin 6 -> HX711 DOUT
  7.   Arduino pin 5V -> HX711 VCC
  8.   Arduino pin GND -> HX711 GND
  9. */
  10.  
  11. #include "HX711.h"
  12.  
  13. HX711 scale;
  14.  
  15. float calibration_factor = -375; // this calibration factor is adjusted according to my load cell
  16. float units;
  17. float ounces;
  18.  
  19. void setup() {
  20.   Serial.begin(9600);
  21.   Serial.println("HX711 calibration sketch");
  22.   Serial.println("Remove all weight from scale");
  23.   Serial.println("After readings begin, place known weight on scale");
  24.   Serial.println("Press + or a to increase calibration factor");
  25.   Serial.println("Press - or z to decrease calibration factor");
  26.   scale.begin(3, 2);
  27.   scale.set_scale();
  28.   scale.tare();  //Reset the scale to 0
  29.  
  30.   long zero_factor = scale.read_average(); //Get a baseline reading
  31.   Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  32.   Serial.println(zero_factor);
  33. }
  34.  
  35. void loop() {
  36.  
  37.   scale.set_scale(calibration_factor); //Adjust to this calibration factor
  38.  
  39.   Serial.print("Reading: ");
  40.   units = scale.get_units(), 10;
  41.   if (units < 0)
  42.   {
  43.     units = 0.00;
  44.   }
  45.   ounces = units * 0.035274;
  46.   Serial.print(units);
  47.   Serial.print(" grams");
  48.   Serial.print(" calibration_factor: ");
  49.   Serial.print(calibration_factor);
  50.   Serial.println();
  51.  
  52.   if (Serial.available())
  53.   {
  54.     char temp = Serial.read();
  55.     if (temp == '+' || temp == 'a')
  56.       calibration_factor += 1;
  57.     else if (temp == '-' || temp == 'z')
  58.       calibration_factor -= 1;
  59.   }
  60.   delay(1000);
  61. }
RAW Paste Data