Arduino Project Problem, Black Floating Liquid Switch Level Sensor

Hey everyone there, so recently I've been tasked with this project where I need to check if the floating liquid level sensor works properly or not, and everything that has to do with the Hardware stuff is just fine, and I'm going to insert a picture below of how I have it connected in my Arduino Uno.

Now, apparently there seems to be a problem with my code on Arduino IDE, this is the code:

int FloatSensor = 4;
int led = 13;

void setup() {
Serial.begin(9600);
pinMode(FloatSensor, INPUT_PULLUP);
pinMode (led, OUTPUT);
}

void loop() {

if (FloatSensor == LOW) {
digitalWrite(led, LOW);
Serial.println("WATER LEVEL - LOW");
}
else {
digitalWrite(led, HIGH);
Serial.println("WATER LEVEL - HIGH");
}
delay(1000);
}

for some reason the Serial Monitor is stuck on HIGH all the time and the LED is always on, the problem is not with the Float Sensor itself (I'm sure of that), but I couldn't tell what the problem is with the code.
I've tried another code earlier with few changes and it was something like:

int GREEN=13;
int Float=2;
void setup() {
Serial.begin(9600);
pinMode(GREEN,OUTPUT);
pinMode(Float,INPUT_PULLUP);
}

void loop() {
if (digitalRead (Float == HIGH))
{
Serial.println("WATER LEVEL IS HIGH");
digitalWrite(GREEN,HIGH);
}
if (digitalRead (Float == LOW))
{
Serial.println("WATER LEVEL IS LOW");
digitalWrite(GREEN,LOW);
}
}

and this time the Serial Monitor kept switching almost instantly between HIGH and LOW, and the LED is flickering all the time.

this made me futher more sure that the problem is with the code not the Hardware, I tried and switched the Float Liquid Level Sensor, not once but twice, using 3 different ones, and the problem remained the same, I tried switching the cables and using different ones, and it's still there.
if you can think of a way to solve the problem please reply to this asap, would appreciate it if someone could right down the mistakes, oh and yea here is the project:

1 Like

When the water is below the float, is the switch OPEN or CLOSED?

You should re-edit your posting like described in the tutorial below.
Your code should be inside a so called code-section

The "asap" makes me not answering

I mean isn’t it supposed to be low? It’s not sensing anything, but no matter how much I move the sensor up and down with my hand, the answer serial monitor and the led’s reaction never change.
Basically it’s stuck in a loop, and by the way I didn’t measure it with water but with my hand (i lift it up to and down to act as water)

Here's your code properly formatted:

int GREEN = 13;
int Float = 2;

void setup() {
  Serial.begin(9600);
  pinMode(GREEN, OUTPUT);
  pinMode(Float, INPUT_PULLUP);
}
void loop() {
  if (digitalRead (Float == HIGH))
  {
    Serial.println("WATER LEVEL IS HIGH");
    digitalWrite(GREEN, HIGH);
  }
  if (digitalRead (Float == LOW))
  {
    Serial.println("WATER LEVEL IS LOW");
    digitalWrite(GREEN, LOW);
  }
}

Try this test program:

int GREEN = 13;
int Float = 2;

void setup() {
  Serial.begin(9600);
  pinMode(GREEN, OUTPUT);
  pinMode(Float, INPUT_PULLUP);
}
void loop() {
  digitalWrite(GREEN, digitalRead(Float));
}

Move the float UP and DOWN with your finger, if the float is down, is the LED ON?

1 Like

because your code does not sense the correct IO -pin

Is the switch connected from pin 2 to GND?

The pictures shows connected between IO-pin 2 and GND

But the first code said pin 4!

run this sketch to see which IO-pin you are reading and what logic state this IO-pin has

int GREEN = 13;
int Float = 2;

void setup() {
  Serial.begin(9600);
  pinMode(GREEN, OUTPUT);
  pinMode(Float, INPUT_PULLUP);
}
void loop() {
  Serial.print("digitalRead(");
  Serial.print( (Float == HIGH)  );
  Serial.print(")=");
  Serial.println(digitalRead (Float == HIGH) );
  delay(2000);
  
  if (digitalRead (Float == HIGH))
  {
    Serial.println("WATER LEVEL IS HIGH");
    digitalWrite(GREEN, HIGH);
  }

  Serial.print("digitalRead(");
  Serial.print( (Float == LOW)  );
  Serial.print(")=");
  Serial.println(digitalRead (Float == LOW) );
  delay(2000);
  
  if (digitalRead (Float == LOW))
  {
    Serial.println("WATER LEVEL IS LOW");
    digitalWrite(GREEN, LOW);
  }
}

Should be:

if (digitalRead (Float) == HIGH)

Im aware of that but i have it connected to 4 on my arduino, i just followed the steps of this drawing and changed the pin from 2 to 4, which is why i changed it in the code as well

Does that make a big difference?

Put the switch on pin 2, run the test code I posted. Report results.

Wdym, i’m sorry i’m not really an expert or that knowledgable but io pins are the numbers (analog and digital pins) right?

YES.

Would you be around tomorrow? I honestly posted this after trying too much and almost giving up, i posted here thinking i will get an answer in months.
I’m really grateful for you all but i’m not near the laptop and the arduino anymore

run this code to SEE the difference

int GREEN = 13;
int Float = 2;

void setup() {
  Serial.begin(9600);
  pinMode(GREEN, OUTPUT);
  pinMode(Float, INPUT_PULLUP);
}
void loop() {
  Serial.print("digitalRead(");
  Serial.print( (Float == HIGH)  );
  Serial.print(")=");
  Serial.println(digitalRead (Float == HIGH) );
  delay(2000);
  
  if (digitalRead (Float == HIGH))
  {
    Serial.println("WATER LEVEL IS HIGH");
    digitalWrite(GREEN, HIGH);
  }

  Serial.print("digitalRead(");
  Serial.print( (Float == LOW)  );
  Serial.print(")=");
  Serial.println(digitalRead (Float == LOW) );
  delay(2000);
  
  if (digitalRead (Float == LOW))
  {
    Serial.println("WATER LEVEL IS LOW");
    digitalWrite(GREEN, LOW);
  }
}

Can you explain why please