首页 > 解决方案 > Arduino 上的重量读数为 404.6kg

问题描述

我正在使用下面的代码对我的 Arduino 进行编程以制作体重秤。当秤上没有重量时,它的读数为-404.6kg。当我增加重量时,增加是正确的,即当我增加 3kg 时增加是 3kg,依此类推,请参见屏幕截图。我想在我的代码中添加一行,将 404.6kg 添加到称重传感器读数中。我该怎么做?

#include "HX711.h" 
#define LOADCELL_DOUT_PIN  23
#define LOADCELL_SCK_PIN  22

HX711 scale;

float calibration_factor = -20600; 

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.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  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.       
  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(" Kgs"); //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();

  delay(1000);

  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
  }
}

截屏

标签: arduino

解决方案


看起来您需要在calibration_factor此处调整变量。我查看了芯片的库,在标题 [1] 的顶部看到了这条评论:

Setup your scale and start the sketch WITHOUT a weight on the scale
 Once readings are displayed place the weight on the scale
 Press +/- or a/z to adjust the calibration_factor until the output readings match the known weight

并在稍后的代码中定义它:

float calibration_factor = 2125; //-7050 worked for my 440lb max scale setup

所以看起来你应该调整这个值,直到你得到一个正确校准和归零的读数。

编辑:我找到了一个教程[2],它更多地讨论了如何校准传感器,它还建议通过反复试验来找到正确的数字。我不知道calibration_factor的单位是什么,或者数字本身代表什么——但调整它应该可以满足你的需求。

祝你好运!

[1] 我在哪里看到评论: https ://gist.github.com/matt448/14d118e2fc5b6217da11

[2] 在此处查找有关校准传感器的部分: https ://circuits4you.com/2016/11/25/hx711-arduino-load-cell/


推荐阅读