Water level sensor fluctuation

Hello. I am using a non contact water level sensor (XKC-Y25-PNP) along with a water pump. When the sensor senses water, it turns on a water pump that is connected to a relay. The sensor works fine when tested alone, but when tested along with the pump the value fluctuates a lot. It starts to rapidly switch between 0 and 1. The pump is powered by an external 12V supply. I would appreciate any help in sorting this issue out.

int wls = 3;  //Water level sensor
int level = 0;  //Variable to store the data received from sensor

int pump = 5;

void setup() {
  pinMode(wls, INPUT);
  pinMode(pump, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  level = digitalRead(wls);
  Serial.println(level);
  if (level == 0)  //My sensor gives 0 when it senses water and 1 when there is no water
  {
    digitalWrite(pump, HIGH);
  }
  else if (level == 1)
  {
    digitalWrite(pump, LOW);
  }
 delay(500);
}

Please post a link to the water sensor and a wiring diagram.

And your schematic / wiring diagram is where ?
Please include models & types of all components,

PumpRelay

I wired my relay similarly and connected my sensor to pin 3 and relay to pin 5.
The sensor used is: https://robu.in/product/xkc-y25-pnp-intelligent-non-contact-water-level-sensor/
The relay used is: https://robu.in/product/4-channel-isolated-5v-10a-relay-module-opto-coupler-for-arduino-pic-avr-dsp-arm/

Hello
Did you already search inside the WWW for a tutorial, didn´t you?

It looks like you may have to "smooth" the output of that sensor.
Read it every X ms, in the loop(), and keep the last N readings.
Define a threshold and if that threshold is exceeded, assume a positive reading, otherwise a negative reading.
You could also try adding a capacitor ( say 100uF) between the GND and Vcc connections to the sensor.
Here is a better description of that sensor, although it may not be exactly the same, since yours implies a PNP output: http://www.naylampmechatronics.com/img/cms/Datasheets/XKC%20Y25%20T12V.pdf It still talks vaguely about ". . . a certain algorithm to judge [the output] . . . "

Edit.

If the relay module is a 12 volt one, it should not be powered through the Arduino 5v supply.

I use one of those water sensors in a project of mine. . Did you note, from the data sheet, the .250ma current requirement for the sensor?

In the fritsnutz drawing the sensor is hooked to pin 10. Your code does not reference pin 10.

As has been mentioned, your relay module is a 12V relay module. An Uno only puts out 5V. Not going to work.

the relay will wreak havoc on the power supply.
move that off to a separate power supply.

remove the relay temporarily, and replace with a switch.
test and see what happens.

Hi, @sudhir98
Can you please post a hand drawn circuit diagram and include the sensor and relay?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

I have not hooked up the sensor to pin 10. As mentioned in one of my previous comments, I have put up the picture as a reference and I have connected the sensor to pin number 3. Also I am powering the pump through an external 12V supply and only the sensor and the VCC pin of the relay are connected to the Arduino 5V supply .

Sorry for the bad drawing :sweat_smile: Not great at drawing circuits.

A 12v relay module will require 12v to operate, not 5v.
In any case, a 4 relay module (even if you use 5v unit)
will be a rather large drain(if not excessive) on the Arduino regulator.
Do you know or have you measured how much current the sensor draws?

Ah okay I'll give that a try. According to the datasheet it draws 5mA.

You are powering the non-contact switch directly from the Uno 5V supply? The non-contact spec sheet indicates that their needs to be .250ma available for the switch. Powering the switch through or from the Uno's 5 volt can be an issue.

Perhaps this will help the OP to see another issue:

Could you please post an image you your project? Especially, of the relay wiring and the non-contact switch wiring.

the relay takes about 90mA
when you switch that off and on, it will effect all sensors to some degree.

if your sensors are not outputting smooth values, look to power conditioning.

===
Idahowalker.... 5volt relay on a 12volt power supply ?

Those opto relay modules have a power source jumper. If I remember correctly, it is used to separate the power being used to run the module from the power being used to trigger the module.


The OP may need to move the power signal VCC source jumper.

You will need to provide a drawing of the wiring with all the parts. And you drawing, sketch must document exactly how the physical wires are connected. This is important because often this type of issue is due to electrical noise causes the variation is output of the sensor.

What pump, container material and what liquid are you using.

Does the pump actually operate? If you are feeding a 12 V relay module with 5 V, I doubt the relay would actually pull in, but if it partially actuates it will cause all sorts of mayhem.

Please confirm that on the relay module you have the relays are labelled "JQC3F-12VDC-C" or similar and not "5V".

So if you have a 12 V (DC) pump, then you connect the 12 V plus to "JD-VCC" in that relay module with the jumper removed, and 12 V minus to "GND". Use a twin cable to do this, keeping the supply and return together at all times.

Then connect "VCC" to the 5 V of your Arduino and the corresponding "IN" to your chosen Arduino pin which you write LOW to actuate the relay. Again, keep the two wires together between relay and Arduino.

Yes the pump operates perfectly. I even tried pairing it with a solenoid valve and both were controlled using the same relay and they worked just fine.

I am using a 5V relay. SRD-05VDC-SL-C to be specific. Here is the link to the relay I am using: https://robu.in/product/4-channel-isolated-5v-10a-relay-module-opto-coupler-for-arduino-pic-avr-dsp-arm/?gclid=CjwKCAjwpMOIBhBAEiwAy5M6YL66e5V1iZ2_wb207jY62t2rHMUSGGGApKSHHtORZodnQkm5z2TXZBoCj_sQAvD_BwE

I am using a 12V submersible pump. Here is a link to the same: https://robu.in/product/12v-high-quality-dc-mini-submersible-pump/

The liquid I am using is water and the container I tested it with is a PET bottle.