Example Code for Arduino-Liquid Level Detection
This project demonstrates how to use the Ultrasonic Liquid Level Sensor with an Arduino board to measure liquid levels via UART communication. Users will learn how to interface the sensor with Arduino, read and process UART data frames, and obtain real-time liquid level values.
Hardware Preparation
- DFRduino UNO R3 x 1
- Ultrasonic Liquid Level Sensor (SEN0356) x 1
- Wires x 1 set
Software Preparation
- Arduino IDE: Download and install from Arduino Official Website (compatible with versions 1.8.x or later). No additional libraries are required for this project.
Wiring Diagram

Other Preparation Work
Sample Code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,10); // RX, TX
unsigned char dat[4]={};
int depth;
int sum;
void setup()
{
Serial.begin(115200);
mySerial.begin(9600);
}
void loop()
{
do{
for(int i=0;i<4;i++)
{
dat[i]=mySerial.read();
}
}while(mySerial.read()==0xff);
mySerial.flush();
if(dat[0]==0xff)
{
sum=(dat[0]+dat[1]+dat[2])&0x00FF;
if(sum==dat[3])
{
depth=dat[1]*256+dat[2];
Serial.print("depth=");
Serial.print(depth);
Serial.println("mm");
}
else
{
Serial.println("no water");
}
}
delay(500);
}
Result
- Serial Monitor Output: After uploading the code, open the Arduino Serial Monitor (set to 115200 baud rate). If liquid is detected, the monitor will display
depth=XXXmm(e.g.,depth=1953mm). If no liquid is present, it will showno water. - Sensor LED Status: The sensor’s LED stays on when no liquid is detected and blinks slowly (once per second) when liquid is present.
Additional Information
Was this article helpful?
