Loadcell as a switch

Hello everyone, I would like to ask what is wrong with my code, I am trying to use Load cell as a switch which would turn on the cooling fan/ motor and turn off if there are no weight detected. The materials that I used are load cell 5kg, HX711 module and 12V cooling fan directly connected to Arduino. I don't know what's wrong with my code, pls help me. I am a beginner at coding I only learned blink codes. This is for my research project.

Code:

#include <HX711.h>

const int LOADCELL_DOUT_PIN =4;
const int LOADCELL_SCK_PIN =5;
const int FAN_PIN =8;

HX711 loadcell;

void setup ()
{
loadcell.begin (LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
pinMode (FAN_PIN, OUTPUT);

void loop ()
{
long weight = loadcell.read();
if ( weight > 0 ) {
digitalWrite (FAN_PIN, HIGH); 
} else {
digitalWrite (FAN_PIN, LOW);
}
}

P.s at this point I don't even know what I'm doing :sob:

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

I suggest you use a number greater then zero as there will be noise at that point. Something a little bigger then that say 5 somewhat below your minimum weight. This will help to keep it from oscillation.

1 Like

Hello

The Arduino family provides a function, macro or whatever called, to design a hysterese by using the constrain().

1 Like

Neither do we.
So what is the problem? Fan always ON, never ON, sometimes ON??

1 Like

The Arduino will not make the fan work. You need to connect an Arduino output pin to control a relay compatible with Arduino. The relay will pass the 12vdc needed from a 12vdc power supply to the fan.

Let's draw a circuit.

               +---|EXT|--------|USB|---+
               |    PWR          A5/SCL |           +-----------+
               |                 A4/SDA |           |POWER  GND |----+
               |          UNO      AREF |           |      +12V |--+ |
               |                    GND |           +-----------+  | |
               | IOREF          SCK/D13 |                          | |
               | RST             DI/D12 |           +-----------+  | |
               | 3V3             DO/D11~|           |   RELAY   |  | |
          +----| +5V                D10~|      +----| DC-    NC |  | |
          |    | GND                 D9~|      | +--| DC+   COM |--+ |
          | +--| GND                 D8 |------|-|--| DIN    NO |--+ |
          | |  | Vin                 D7 |      | |  +-----------+  | |
          | |  |                     D6~|      | |                 | |
          | |  | A0                  D5~|----+ | |  +-----------+  | |
          | |  | A1                  D4 |--+ | | |  |      +12V |--+ |
          | |  | A2             INT1/D3 |  | | | |  | FAN   GND |----+
          | |  | A3             INT0/D2~|  | | | |  +-----------+
          | |  | A4/DA  RS CK DI  TX>D1 |  | | | |
          | |  | A5/CL  GD D0 5V  RX<D0 |  | | | |
          | |  +------------------------+  | | | |
          | +------------------------------|-|-+ |
          |    +------------------------+  | | | |
 +--------|----| E+ RED    HX711        |  | | | |
 | +------|----| E- BLK  AMPLIFIER      |  | | | |
 | | +----|----| A- WHT             GND |--|-|-+ |
 | | | +--|----| A+ GRN            DOUT |--+ |   |
 | | | |  |    | B-    (E) Excite   SCK |----+   |
 | | | |  |    | B+  (A/B) Output   VCC |--------+
 | | | |  |    +------------------------+        |
 | | | |  +--------------------------------------+
 | | | |       +--------------------------------------------+
 | | | +-------| RED EX+     EX+ ----/\               HX711 |
 | | +---------| BLK EX-    SIG+ ___/  \___ SIG-  LOAD CELL |
 | +-----------| WHT SIG-           \  /                    |
 +-------------| GRN SIG+            \/____  EX-            |
               +--------------------------------------------+

... and your code, annotated...

#include <HX711.h> // Library abstraction layer to convert high level code to low level commands 

const int LOADCELL_DOUT_PIN = 4;  // DATA OUT pin from  HX711 amplifier to Arduino
const int LOADCELL_SCK_PIN = 5;  // CLOCK pin from HX711 amplifier to Arduino
const int FAN_PIN = 8;  // this pin will enable and disable a relay to power the fan

HX711 loadcell; // create an INSTANCE of HX711

void setup () // initialize
{
  loadcell.begin (LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  pinMode (FAN_PIN, OUTPUT);
} // you needed a close-brace here... it was missing

void loop () // this is your main code that will repeat
{
  long weight = loadcell.read(); // declare weight a long datatype, to store loadcell.read() data
  if ( weight > 1 ) { // if the data/weight is greater than 5 (for noise protection - see @gilshultz)
    digitalWrite (FAN_PIN, HIGH); // set the RELAY pin HIGH to turn the fan ON
  } else { // otherwise... if the weight is <= 5...
    digitalWrite (FAN_PIN, LOW); // set the RELAY pin LOW to turn the fan OFF
  }
} // fin

You will need to CALIBRATE with a known weight (a bag of rice, a slug, et c.) for the EMPTY WEIGHT (tare).

1 Like

I want the fan to turn on if the loadcell can detect a weight 1kg to 5kg

I tried it yesterday with the help of my friends brother who is an IT, the fan turned on even though it is directly connected to the Arduino however the rotation of the fan is very slow or average.

Our weight range is below 1kg or 1kg to 5kg

You are killing your Arduino - disconnect it from the fan. Arduino is a microcontroller on a 5vdc board, and not a power supply. Look at my drawing... the fan is powered by the power supply when the relay is energized when the load cell weighs more than "5" (you may change the weight to your choice). 12vdc is present on your computer's power supply (as are GND, +/-5VDC, +/-12VDC terminals).

1 Like

Noted. What material should we use about the relay and power?

1 Like

I love your suggestions sir, and for our 12v heater what could you suggest? What type of batter should we use?

There is no heater.

1 Like

It's a different part which isn't gonna be connected to the Arduino

AAA battery

1 Like

How many Amp? Is it for the Arduino? Load cell? Tea? In a vehicle?

1 Like

It doesn't indicate how much amp the material has, it just said 12V PTC heater and by the way sir we're going to change the cooling fan to a DC motor how many volts do you think we should use? And do we still need the relay module if we're gonna use DC motor? Or can we directly connect it to the Arduino if it's a 5V or 3V DC motor?

A fan using more volts means a bigger power supply. Cooling fans have a "cubic feet per minute" (CFM) rating that you will need to experiment with for your application (to keep it cool). Never power anything with an Arduino. Use the relay to pass the power from the power supply to the fan. Ensure the fan power requirement does not exceed the power supply output, and you really should not use 100% of the power supply output.

1 Like

Noted sir. I was told by my teacher to use one Ac to Dc power supply for our Arduino and 12Ptc heater, are there material you can suggest to about this? He said about to make it parallel circuit to connect the power supply of both Arduino and heater since they are a different components of our product. And I would like to clarify if is it ok to power the Arduino with the 12V ac to DC through the power in Arduino or we need the back converter?