microrobotics icon

XKC-Y27 Contactless Flow Sensor

microrobotics | PRO | 05/08/23 11:38:05 AM UTC | 0 ⭐ | 2520 👁️ | Never ⏰ | []
Arduino |

1.6 KB

|

None

|

0 👍

/

0 👎

/*
To interface the XKC-Y27 Contactless Flow Sensor with an Arduino, you need to connect the sensor to the Arduino using its NPN or PNP output signal. In this example, I will demonstrate how to use the NPN output signal.
 
Requirements:
 
Arduino board (e.g., Uno, Mega, Nano)
XKC-Y27 Contactless Flow Sensor
Jumper wires
Power supply (24V)
 
In this example, the XKC-Y27 Contactless Flow Sensor's NPN output signal is connected to the digital input pin 2 of the Arduino. The 24V power supply is connected to the sensor's power input. The sensor's GND is connected to the Arduino's GND.
 
The code reads the sensor's state and checks if it has changed. If the state has changed, it prints "Liquid detected" or "No liquid detected" to the Serial Monitor, depending on the sensor's output. The sensor state is checked every 500 ms, but you can adjust the delay as needed for your application.
*/
 
// Pins for the XKC-Y27 Contactless Flow Sensor
const int sensorPin = 2;
 
// State variables
int previousSensorState = HIGH;
int sensorState;
 
void setup() {
  // Initialize the Serial Monitor
  Serial.begin(9600);
 
  // Set the sensor pin as an input
  pinMode(sensorPin, INPUT);
}
 
void loop() {
  // Read the sensor state
  sensorState = digitalRead(sensorPin);
 
  // Check if the sensor state has changed
  if (sensorState != previousSensorState) {
    if (sensorState == LOW) {
      Serial.println("Liquid detected");
    } else {
      Serial.println("No liquid detected");
    }
    previousSensorState = sensorState;
  }
 
  delay(500); // Adjust the delay as needed for your application
}
 

Comments