본문 바로가기
카테고리 없음

ESP32 HX711 로드셀 무게감지 센서

by 달콤한숟가락 2024. 7. 23.
#include "HX711.h"

const int LOADCELL_DOUT_PIN = 5; // 데이터 출력 핀
const int LOADCELL_SCK_PIN = 6;  // 시계 핀

HX711 scale;

void setup() {
  Serial.begin(9600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  Serial.println("HX711 calibration");

  // 초기 영점 조정
  scale.tare();

  Serial.println("Put known weight on the scale and press any key to continue.");
  while (!Serial.available()) {
    delay(100);
  }
  Serial.read();

  // 표준 추의 무게 (예: 500g)
  float known_weight = 4000.0;

  // 새로운 보정 계수 계산
  scale.set_scale(scale.get_units(20) / known_weight);
  Serial.print("Calibration factor: ");
  Serial.println(scale.get_scale());

  delay(5000);
  // 보정 계수와 영점 조정 완료 후 준비
  scale.tare(); // 재영점 조정 (표준 추 제거 후)
}

void loop() {
  if (scale.is_ready()) {
    float reading = scale.get_units(20);
    Serial.print("Weight: ");
    Serial.print(reading);
    Serial.println(" g");
  } else {
    Serial.println("HX711 not found.");
  }
  delay(100);
}

댓글