How water sensor turn on a relay?

Hello, im making a simple new project but I have difficulties making the relay work. My sensor is a XKC-Y25-NPN powered by an external power, I'm using arduino UNO and a standard 5V relay powered by the UNO. The idea is when the sensor sense water the relay turns ON, and when there is not water the relay is OFF. I have the "sensor part" of the program but not the "relay part"

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(500); // delay in between reads for stability
}

So when the sensor sense water the monitor gives the number 17 and when there is not water the sensor gives 0. Any help or better idea is welcome (I'm a begginer). Thanks.

you can use the if statement .
for example

if(sensorValue == 0) {
   //code to turn on relay
}

Your can refer to a similar project: Arduino - Light Sensor Triggers Relay

What voltage are you powering the level sensor with? The sensor negative ( - ) wire must be connected to the Arduino GND pin.
That sensor has a digital (ON / OFF) output that goes LOW when triggered. Try this test sketch:

const byte relayPin = 10;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);
  pinMode(A0,INPUT_PULLUP);
}
// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  byte sensorValue = digitalRead(A0);
  // print out the value you read: // 1 or 0
  Serial.println(sensorValue);
  digitalWrite(relayPin, sensorValue);

  delay(500);        // delay in between reads for stability
}

IoT_hobbiest answered your question, however I have a few observations.

  1. the ground from your sensor must connect to the arduino ground someplace.

  2. Electrical signals are messy. They don't look like those in the specifications.

They have noise (small variations in value due to local electricity)
They have spikes (large very fast variations due to something big switching).

I would put a 0.1µF capacitor (ceramic, any voltage) across the board analog input and ground (at the board)

Perhaps reading this might help Noise on electrical signals

Thank you guys, now it works due to your help, very grateful!

katatus:
Thank you guys, now it works due to your help, very grateful!

What did you do to solve your problem.
Let us know so that this thread might be helpful to someone else with your symptoms.
Tom... :slight_smile:

TomGeorge:
What did you do to solve your problem.
Let us know so that this thread might be helpful to someone else with your symptoms.
Tom... :slight_smile:

Sure :slight_smile: :
1: I was powering the sensor with an external source of power (5V battery) but it HAS to be connected to the arduino 5V output pin and the arduino GND. Same with the relay, it has to be connected with the arduino powering source (5V and GND from the arduino). My worry was the overcharge of the arduino output 5V source, but the relay + the sensor just use like a 10% of the capacity of the 5V pin so it's safe (70mA the relay + 5mA the sensor).
2: The rest was easy, i just made a new sketch with the if statement well placed (I changed the S from the relay from pin ~10 to pin 8 becuase why not):

// These constants won't change:
const int analogPin = A0;    // pin that the signal of the water sensor is attached to
const int relayPin = 8;       // pin that the relay is attached to
const int threshold = 10;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the relay pin as an output:
  pinMode(relayPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {

  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the relay:
  if (analogValue > threshold) {
    digitalWrite(relayPin, HIGH);
  } else {
    digitalWrite(relayPin, LOW);
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(1000);        // delay in between reads for stability
}

The analog A0 reads 17 when the sensor is ON and 0 when the sensor is OFF. So i place the threshold in 10.

Hi,

The analog A0 reads 17 when the sensor is ON and 0 when the sensor is OFF. So i place the threshold in 10.

Thanks for the reply.
That sensor has a digital, open circuit NPN output not a variable analog level proportional to the tank level.

It is not a variable analog level,

  • Change the input to digital mode, not analog.
  • Program in a INPUT_PULLUP resistor.
  • Now read the digital input with a digitalRead and test with if statement checking HIGH or LOW.

Tom... :slight_smile: