Hello everyone. This is my first time posting here - I am really glad this forum exists!
I am super new to arduino and programming in general, so be gentle.
I am having issues setting up a 50kg bar type load cell to work with my arduino via an HX711.
Things seemed to be working at first…but now my output on the serial monitor just randomly increases no matter what I do. It will steadily increase with almost ever measure by a small amount…but If I let it it will run up to huge numbers like this.
I had it all set up and took everything apart thinking it might be my setup…but even with everything just flat on a table this happens.
I’m at my wits end with this. Any suggestions?
Here is my sketch:
#include <hx711.h>
/* sample for digital weight scale of hx711, display with a HD44780 liquid crtstal monitor
*
* hardware design: syyyd
* available at http://syyyd.taobao.com
*
* library design: Weihong Guan (@aguegu)
* http://aguegu.net
*
* library host on
* https://github.com/aguegu/Arduino
*/
// Hx711.DOUT - pin #A1
// Hx711.SCK - pin #A0
int result;
#include "hx711.h"
Hx711 scale(A1, A0);
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(scale.getGram() );
Serial.println(" g");
delay(200);
}
.cpp file:
/*
* Hx711.cpp
*
* Created on: Oct 31, 2012
* Author: agu
*/
#include "hx711.h"
Hx711::Hx711(uint8_t pin_dout, uint8_t pin_slk) :
_pin_dout(pin_dout), _pin_slk(pin_slk)
{
pinMode(_pin_slk, OUTPUT);
pinMode(_pin_dout, INPUT);
digitalWrite(_pin_slk, HIGH);
delayMicroseconds(100);
digitalWrite(_pin_slk, LOW);
averageValue();
this->setOffset(averageValue());
this->setScale();
}
Hx711::~Hx711()
{
}
long Hx711::averageValue(byte times)
{
long sum = 0;
for (byte i = 0; i < times; i++)
{
sum += getValue();
}
return sum / times;
}
long Hx711::getValue()
{
byte data[3];
while (digitalRead(_pin_dout))
;
for (byte j = 3; j--;)
{
for (char i = 8; i--;)
{
digitalWrite(_pin_slk, HIGH);
bitWrite(data[j], i, digitalRead(_pin_dout));
digitalWrite(_pin_slk, LOW);
}
}
digitalWrite(_pin_slk, HIGH);
digitalWrite(_pin_slk, LOW);
data[2] ^= 0x80;
return ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8)
| (uint32_t) data[0];
}
void Hx711::setOffset(long offset)
{
_offset = offset;
}
void Hx711::setScale(float scale)
{
_scale = scale;
}
float Hx711::getGram()
{
long val = (averageValue() - _offset);
return (float) val / _scale;
}