How to make a weight sensor having weight as variable

This may be a dumb question, but I basically know nothing about coding arduino.

Basically, my project is that I need to make a weight sensor that activates a motor once a certain weight (lets say 400grams) and above has been reached. I already know how to code the motor bit, but I require assistance coding the weight as all tutorials I have found do not include the weight being a variable. I am using an hx711 and a 1kg load cell on an arduino uno. Thank you

you may seen calibration function or it own sketch. built it in and call when some pin is activated, this calibration value you can hold in memory until power down or store in EEPROM and recall after start if no calibration pin activated.

Do you have a sketch that prints the weight to Serial?
Show that here. It should be fairly easy to put the value that is printed in some variable...


Is this what your talking about? Sorry if i am wrong this is my first project and I have no prior experience with arduino.

Welcome to the forum.

Perhaps you've not seen that computer programs are called sketches from time to time in Arduino-land.

We want to see the program or code you've tried with the weight sensor, how it gets the measurement and prints the result.

a7

what do yo want to have a result? Should the weight have an impact on the fact if the motor should run or not?

#include <Arduino.h>
#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

HX711 scale;

void setup() {
  Serial.begin(57600);
  Serial.println("HX711 Demo");
  Serial.println("Initializing the scale");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  Serial.println("Before setting up the scale:");
  Serial.print("read: \t\t");
  Serial.println(scale.read());      // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));   // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight (not set yet)

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);  // print the average of 5 readings from the ADC minus tare weight (not set) divided
            // by the SCALE parameter (not set yet)
            
  scale.set_scale(-459.542);
  //scale.set_scale(-471.497);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();               // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
            // by the SCALE parameter set with set_scale

  Serial.println("Readings:");
}

void loop() {
  Serial.print("one reading:\t");
  Serial.print(scale.get_units(), 1);
  Serial.print("\t| average:\t");
  Serial.println(scale.get_units(10), 5);

  delay(5000);
}

is this what you mean?

yes, the weight should have an impact on the fact the motor should run

Yeth.

So why not

    float myVariableWhichIsTheWeight = scale.get_units(10);

put the measurement into a variable? You could use a shorter name.

a7

how would you like to set the weight?

possibly using a potentiometer or enter it thru the Arduino IDE serial monitor as a command.

do you want the weight to be persistent between power cycles?

of course the pot value would be persistent, but do you need a display (e.g. lcd) that shows the weight set by the pot.

if you set the weight thru the serial monitor, should it be stored in EEPROM which can be read after the next power cycles

please describe what you want the program to do.
For example:
When the sensor get_value returns 50 the motor on pin 4 should switch on.
When the sensor get_value returns 60 the motor on pin 4 should switch off.
The sensor should be readed each 5 seconds.
The result of sensor get_value should be printed to serial monitor.

A conveyor belt loading boxes with drygoods. What if the weight is far over 400g? Is it discarded?

(your sketch in a simulation)

Yes, I do want my weight to be persistent between power cycles so I don't need to recalibrate it after a power cycle. Is there an EEPROM built into the arduino or would I need to get it separately?

When the sensor get_value returns 400 grams pin 4 should switch on

just confirming, will this give me the weight in grams?

Not quite. It will return something with a linear relationship to the mass placed on the device.

You must calibrate the scale to determine the factor that the library code will use to return the mass in grams or ounces or whatever.

The procedure is outlined here on the github page for the library, please read there for much good tips and info.


From that page the steps for calibartion:

  1. Call set_scale() with no parameter.
  2. Call tare() with no parameter.
  3. Place a known weight on the scale and call get_units(10).
  4. Divide the result in step 3 to your known weight. You should get about the parameter you need to pass to set_scale().
  5. Adjust the parameter in step 4 until you get an accurate reading.

I believe once you've done that, then the get_units() method will return the mass using the same units as your "known weight" is expressed in.

HTH

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.