Advertisement
microrobotics

XKC-Y27 Contactless Flow Sensor

May 8th, 2023
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. Requirements:
  5.  
  6. Arduino board (e.g., Uno, Mega, Nano)
  7. XKC-Y27 Contactless Flow Sensor
  8. Jumper wires
  9. Power supply (24V)
  10.  
  11. 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.
  12.  
  13. 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.
  14. */
  15.  
  16. // Pins for the XKC-Y27 Contactless Flow Sensor
  17. const int sensorPin = 2;
  18.  
  19. // State variables
  20. int previousSensorState = HIGH;
  21. int sensorState;
  22.  
  23. void setup() {
  24.   // Initialize the Serial Monitor
  25.   Serial.begin(9600);
  26.  
  27.   // Set the sensor pin as an input
  28.   pinMode(sensorPin, INPUT);
  29. }
  30.  
  31. void loop() {
  32.   // Read the sensor state
  33.   sensorState = digitalRead(sensorPin);
  34.  
  35.   // Check if the sensor state has changed
  36.   if (sensorState != previousSensorState) {
  37.     if (sensorState == LOW) {
  38.       Serial.println("Liquid detected");
  39.     } else {
  40.       Serial.println("No liquid detected");
  41.     }
  42.     previousSensorState = sensorState;
  43.   }
  44.  
  45.   delay(500); // Adjust the delay as needed for your application
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement