Hi all
Im using an ESP32 devkit with a 50kg load cell and a spark fun HX711.
All is wired fine and when using the calibration code as follows I can get the correct weight from the load cell. i.e. 5kg
[code]
/*
Example using the SparkFun HX711 breakout board with a scale
By: Nathan Seidle
SparkFun Electronics
Date: November 19th, 2014
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
This is the calibration sketch. Use it to determine the calibration_factor that the main example uses. It also
outputs the zero_factor useful for projects that have a permanent mass on the scale in between power cycles.
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
Use this calibration_factor on the example sketch
This example assumes pounds (lbs). If you prefer kilograms, change the Serial.print(" lbs"); line to kg. The
calibration factor will be significantly different but it will be linearly related to lbs (1 lbs = 0.453592 kg).
Your calibration factor may be very positive or very negative. It all depends on the setup of your scale system
and the direction the sensors deflect from zero state
This example code uses bogde's excellent library: https://github.com/bogde/HX711
bogde's library is released under a GNU GENERAL PUBLIC LICENSE
Arduino pin 2 -> HX711 CLK
3 -> DOUT
5V -> VCC
GND -> GND
Most any pin on the Arduino Uno will be compatible with DOUT/CLK.
The HX711 board can be powered from 2.7V to 5V so the Arduino 5V power should be fine.
*/
#include "HX711.h" //This library can be obtained here http://librarymanager/All#Avia_HX711
#define LOADCELL_DOUT_PIN 23
#define LOADCELL_SCK_PIN 22
HX711 scale;
float calibration_factor = -40050; //-7050 worked for my 440lb max scale setup
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. Useful in permanent scale projects.
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;
}
}
[/code]
When I then use the following code with the same calibration factor I get a different reading for the weight. See screen shot
[code]
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DHT.h"
#include "HX711.h"
//#ifdef ESP32
// #include <WiFi.h>
// #include <HTTPClient.h>
//#else
// #include <ESP8266WiFi.h>
// #include <ESP8266HTTPClient.h>
// #include <WiFiClient.h>
//#endif
//
//// Replace with your network credentials
//const char* ssid = "";
//const char* password = "";
//
//// REPLACE with your Domain name and URL path or IP address with path
//const char* serverName = "";
//
//// Keep this API Key value to be compatible with the PHP code provided in the project page.
//// If you change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key
//String apiKeyValue = "";
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define ONE_WIRE_BUS 15 // Data wire is connected to GPIO15
#define LOADCELL_DOUT_PIN 23
#define LOADCELL_SCK_PIN 22
HX711 scale;
float calibration_factor = -40050; //-7050 worked for my 440lb max scale setup
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0x6A, 0x48, 0x7C, 0x0C, 0x00, 0x00, 0xC4 };
DeviceAddress sensor2 = { 0x28, 0xFA, 0x14, 0x7B, 0x0C, 0x00, 0x00, 0x47 };
DHT dht(DHTPIN, DHTTYPE);
//// Time to sleep
//uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
//// sleep for 30 minutes = 1800 seconds
//uint64_t TIME_TO_SLEEP = 1800;
// Get Sensor Readings
String getSensorReadings(){
sensors.requestTemperatures();
}
void setup() {
Serial.begin(9600);
sensors.begin();
dht.begin();
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
//scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
// WiFi.begin(ssid, password);
// Serial.println("Connecting");
// while(WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.println("");
// Serial.print("Connected to WiFi network with IP Address: ");
// Serial.println(WiFi.localIP());
//#ifdef ESP32
// // enable timer deep sleep
// esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// Serial.println("Going to sleep now");
// // start deep sleep for 3600 seconds (60 minutes)
//
// #else
// // Deep sleep mode for 3600 seconds (60 minutes)
// Serial.println("Going to sleep now");
// ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// #endif
}
void loop(){
// //Check WiFi connection status
// if(WiFi.status()== WL_CONNECTED){
// HTTPClient http;
//
// // Your Domain name with URL path or IP address with path
// http.begin(serverName);
//
// // Specify content-type header
// http.addHeader("Content-Type", "application/x-www-form-urlencoded");
//
// // Prepare your HTTP POST request data
// String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(dht.readTemperature())
// + "&value2=" + String(dht.readHumidity()) + "&value3=" + String(sensors.getTempC(sensor1)) + "&value4=" + String(sensors.getTempC(sensor2)) + "&value5=" + String(scale.get_units())+"";
// Serial.print("httpRequestData: ");
// Serial.println(httpRequestData);
//
// // You can comment the httpRequestData variable above
// // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
// //String httpRequestData = "api_key=&value1=24.75&value2=49.54&value3=1005.14";
//
// // Send HTTP POST request
// int httpResponseCode = http.POST(httpRequestData);
//
// // If you need an HTTP request with a content type: text/plain
// //http.addHeader("Content-Type", "text/plain");
// //int httpResponseCode = http.POST("Hello, World!");
//
// // If you need an HTTP request with a content type: application/json, use the following:
// //http.addHeader("Content-Type", "application/json");
// //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
//
// if (httpResponseCode>0) {
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
// }
// else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// }
// // Free resources
// http.end();
// }
// else {
// Serial.println("WiFi Disconnected");
// }
// //Send an HTTP POST request every 30 seconds
// delay(30000);
// // Wait a few seconds between measurements.
// delay(20000);
//
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
// Serial.println("DONE");
// Serial.print("8 Frame Hive: ");
// Serial.print(sensors.getTempC(sensor1));
// Serial.print("°C: ");
//
//
// Serial.print("10 Frame Hive: ");
// Serial.print(sensors.getTempC(sensor2));
// Serial.print("°C: ");
Serial.print("Weight ");
Serial.print(scale.get_units(), 1);
Serial.print(" Kgs ");
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F( "Failed to read from DHT sensor!"));
return;
}
// Serial.print(F("8 Frame Roof Temp: "));
// Serial.print(t);
// Serial.print(F("°C "));
// Serial.print(F("8 Frame Roof Humidity: "));
// Serial.print(h);
// Serial.print(F("% "));
Serial.print('\n');
// esp_deep_sleep_start();
}
[/code]
Can some one help as to why this is happening.