Hello, I am brand new to arduino and have been working my way through the starter kit projects and have started my first self-generated project and have run into a few issues.
I have a 5kg straight bar load cell like this one and have hooked it up to a HX711 amplifier. Its taking readings and everything seems to be fine, I used this tutorial and got a lot of help off my dad... What I'd like to be able to do now is write a program for a LED to switch on when a particular weight threshold is met (with the view to later power other outputs like motors).
Obviously I'm no programming expert but from the tiny amount I've learnt from the starter kit I reckon that I should use an if then else statement and have attempted to use this alongside the code in the tutorial but am having no luck. I think this is because the load cell and amplifier has 2 analog outputs (SCK and DOUT to A0 and A1), and I don't understand how to write this into the program or if its even possible? I have tried to work round this in various (and I'm quite sure ridiculous) ways but with very little grasp of C I'm not having much luck! I was wondering if anyone here could point me in the right direction and help me understand if and how this could work? and how to write a program for it?
Sorry if this was rather long winded, if i've left anything important out please let me know!
Hi,
Your code produces a number for the weight, right?? Or not? The "SCK and DOUT" are for DIGITAL data coming out of the amplifer, I think.
You need to get a number for weight first. THEN compare it to a value and control the LED..
Ok so I started with this code:
#include "HX711.h"
HX711 scale(A1, A0); // DOUT, SCK
void setup()
{
Serial.begin(9600);
scale.set_scale(2280.f); //// this value is obtained by calibrating the scale with known weights
scale.tare();
}
void loop()
{
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
}
Which is giving me readings from the HX711
I then I just sort of tacked the if then else statement on the bottom because i have no idea what I'm doing:
#include "HX711.h"
HX711 scale(A1, A0); // DOUT, SCK
int ledPin = 13;
float value = 20;
int weight;
void setup()
{
Serial.begin(9600);
scale.set_scale(2280.f); //// this value is obtained by calibrating the scale with known weights
scale.tare();
pinMode(ledPin, OUTPUT);
}
void loop()
{
weight = analogRead(HX711 scale (A1, A0)
Serial.print("weight");
Serial.print(scale.get_units(), 1);
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
if (weight>value){
digitalWrite (ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
delay(100);
}
But I get the error
"exit status 1
expected primary-expression before 'scale'" and I have no idea how to work around it or if the code is even workable because I understand very little about how the original code works.
So... I did some reading and I think I've got it working
This is my current code:
#include "HX711.h"
HX711 scale (A1, A0);
int ledPin = 13;
void setup()
{
Serial.begin(9600);
scale.set_scale(2280.f); //// this value is obtained by calibrating the scale with known weights
scale.tare();
pinMode (ledPin, OUTPUT);
}
void loop()
{
Serial.print(scale.get_units(), 1);
Serial.println("measurement");
delay(200);
if (scale.read () > 30) {
digitalWrite (ledPin, HIGH);
}
else{
digitalWrite (ledPin, LOW);
}
delay (100);
}
The LED lights up when the value in the serial monitor goes over 30! Can someone maybe let me know if this code is correct/ should work and I'm not just imagining it? I would like to progress to drive a motor instead of an LED