Example Code for Arduino-Liquid Level Detection
Detecting the liquid level of non-metallic containers,and check the status of sensor through serial port. Users can learn how to interface the non-contact liquid level sensor with Arduino and read digital signals to determine liquid presence.
Hardware Preparation
-
Name: DFRduino UNO R3, Model/SKU: DFR0009, Quantity: 1, Purchase Link:
DFRduino UNO R3 -
Name: Small Pipe Diameter Level Sensor, Model/SKU: SEN0370, Quantity: 1, Purchase Link:
Small Pipe Diameter Level Sensor -
Name: Wires, Quantity: Several, Purchase Link: (Not specified)
Software Preparation
- Development Tool: Arduino IDE, Version: Latest stable version, Download Link: https://www.arduino.cc/en/Main/Software
Wiring Diagram

Adjust the sensor's sensitivity using the sensitivity adjustment potentiometer before use.
Sample Code
/*!
* @File DFRobot_LevelSensor.ino
* @brief Detecting the liquid level of non-metallic containers,and check the status of sensor through serial port
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [liunian]([email protected])
* @version V1.0
* @date 2020-08-13
*/
int inPin = 8;
boolean running = 0;//when running=1, the liquid is detected, print out 1, otherwise, print out 0; running=0, the liquid is detected, print out 0, otherwise, print out 1.
int modePin = 9;
void setup()
{
Serial.begin(9600);
pinMode(inPin, INPUT);
pinMode(modePin, OUTPUT);
digitalWrite(modePin, running);
}
void loop()
{
Serial.println(digitalRead(inPin));
delay(100);
}
Result
When the liquid level reaches the sensor's sensing point, the serial port will output a value corresponding to the running mode (1 or 0). When no liquid is present, the output will be the opposite value. Users can open the Arduino IDE's Serial Monitor (set to 9600 baud) to view the real-time output.
Installation Guide
- The product is suitable for pipe with outter diameter 3~10mm. Diameter over 11 cannot be used with this sensor.
- The spring plate cannot be bent over 60°.
Sensitivity Adjustment

Was this article helpful?
