Example Code for Arduino-Liquid Level Measurement

Last revision 2025/12/30

Unlock the secrets of measuring liquid levels using Arduino with step-by-step guidance, including hardware setups, software preparation, wiring diagrams, and sample code for accurate depth measurement using pressure sensors.

Hardware Preparation

Software Preparation

Wiring Diagram

Other Preparation Work

When the liquid level transmitter is put into a certain depth of some liquid, the pressure at the end of the sensor is

The atmospheric pressure P0 on the liquid surface is introduced into the back chamber of the sensor through the air guiding tube to offset the atmospheric pressure P0 at the end of the sensor, so that the measured pressure of the sensor is P'=P-P0=ρgh. Therefore, if the liquid density ρ and the acceleration of gravity g are known, the liquid level depth h can be calculated by measuring the pressure P'.

The pressure measured by the liquid level sensor is then amplified and compensated by the circuit and output with a standard 4-20 mA current signal. The relationship of output current of the liquid level transmitter, output voltage of the current to voltage module and depth are shown below:

The depth ranges, voltages and currents shown in the figure are for pure water. If other liquid is to be measured, the density of the liquid needs to be considered. The specific conversion relationship is shown in the sample code.

Sample Code

    /***********************************************************
      DFRobot Gravity: Analog Current to Voltage Converter(For 4~20mA Application)
      SKU:SEN0262

      GNU Lesser General Public License.
      See <http://www.gnu.org/licenses/> for details.
      All above must be included in any redistribution
     ****************************************************/

    #define ANALOG_PIN A2
    #define RANGE 5000 // Depth measuring range 5000mm (for water)
    #define VREF 5000 // ADC's reference voltage on your Arduino,typical value:5000mV
    #define CURRENT_INIT 4.00 // Current @ 0mm (uint: mA)
    #define DENSITY_WATER 1  // Pure water density normalized to 1
    #define DENSITY_GASOLINE 0.74  // Gasoline density
    #define PRINT_INTERVAL 1000

    int16_t dataVoltage;
    float dataCurrent, depth; //unit:mA
    unsigned long timepoint_measure;

    void setup()
    {
      Serial.begin(9600);
      pinMode(ANALOG_PIN, INPUT);
      timepoint_measure = millis();
    }

    void loop()
    {
      if (millis() - timepoint_measure > PRINT_INTERVAL) {
        timepoint_measure = millis();

        dataVoltage = analogRead(ANALOG_PIN)/ 1024.0 * VREF;
        dataCurrent = dataVoltage / 120.0; //Sense Resistor:120ohm
        depth = (dataCurrent - CURRENT_INIT) * (RANGE/ DENSITY_WATER / 16.0); //Calculate depth from current readings

        if (depth < 0) 
        {
          depth = 0.0;
        }

        //Serial print results
        Serial.print("depth:");
        Serial.print(depth);
        Serial.println("mm");
      }
    }

Result

The depth of liquid the sensor detected will be constantly displayed on the Arduino IDE serial monitor.(Unit: mm)

Was this article helpful?

TOP