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);
}
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 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 .
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?
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.
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.
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.