Example Code for Arduino-DIY Water Pressure Detector

Last revision 2025/12/16

This article offers a detailed guide on building a DIY water pressure detector using Arduino, including necessary hardware, software, wiring diagrams, and sample code for calibration and measurement.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0257 Water Pressure Detector

Other Preparation Work

Download the LCD library: DFRobot RGB LCD. Refer to How to install Libraries in Arduino IDE for installation.

Sample Code

/*!
 * @file  SEN0257.ino
 * @brief  Water pressure sensor demoB(LCD1602)
 * @n      - Obtain the water pressure through the output voltage
 * @n        of the sensor.
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  DFRobot
 * @version  V1.0
 * @date  2023-07-06
 */

/************************************************************
  Water Sensor Key Parameter
  - Parts No.:KY-3-5
  - Sensing range: 0 - 1 MPa
  - Input Voltage: 5VDC
  - Output Voltage: 0.5 - 4.5 VDC
    (Linearly corresponding to 0 - 1 MPa)
  - Accuary: 0.5% - 1% FS
**************************************************************/

/************************************************************
  Water Sensor Calibration

  The output voltage offset of the sensor is 0.5V (norminal).
  However, due to the zero-drifting of the internal circuit, the
  no-load output voltage is not exactly 0.5V. Calibration needs to
  be carried out as follow.

  Calibration: connect the 3 pin wire to the Arduio UNO (VCC, GND and Signal)
  without connecting the sensor to the water pipe and run the program
  for once. Mark down the LOWEST voltage value through the serial
  monitor and revise the "OffSet" value to complete the calibration.

  After the calibration the sensor is ready for measuring!
**************************************************************/

#include <Wire.h>
#include "DFRobot_RGBLCD1602.h"                          //LCD头文件
const float  OffSet = 0.483 ;
float V;
int P;
unsigned int lcdR = 0, lcdG = 0, lcdB = 0;
unsigned long delaytime = 0, lighttime = 0;

/*
Change the RGBaddr value based on the hardware version
-----------------------------------------
       Moudule        | Version| RGBAddr|
-----------------------------------------
  LCD1602 Module      |  V1.0  | 0x60   |
-----------------------------------------
  LCD1602 Module      |  V1.1  | 0x6B   |
-----------------------------------------
  LCD1602 RGB Module  |  V1.0  | 0x60   |
-----------------------------------------
  LCD1602 RGB Module  |  V1.1  | 0x2D   |
-----------------------------------------
*/
DFRobot_RGBLCD1602 lcd(/*RGBAddr*/0x60 ,/*lcdCols*/16,/*lcdRows*/2);  //16 characters and 2 lines of show

void setup()
{
  lcd.init();
  delay(5000);
  Serial.begin(115200);
  Serial.println("hello start");
  lighttime = millis();
  lcd.setCursor(0, 0);
  lcd.print("Water Pressure:");
  lcd.setRGB(255, 255, 0);
}

void loop()
{
  lcdR = random(256);
  delayMicroseconds(10);
  lcdG = random(256);
  delayMicroseconds(10);
  lcdB = random(256);
  if (millis() - lighttime > 3000)
  {
    lcd.setRGB(lcdR, lcdG, lcdB);
    lighttime = millis();
  }
  //delay(100);

  V = analogRead(5) * 5.00 / 1024;                    //Sensor output voltage
  P = (V - OffSet) * 250 * 10;                        //Calculate water pressure
  lcd.setCursor(3, 1);

  lcd.print(  P / 10000 % 10);                       //LCD显示
  lcd.print(  P / 1000 % 10);
  lcd.print(   P / 100 % 10);
  lcd.print(   P / 10 % 10);
  lcd.print('.');
  lcd.print(  P  % 10);
  lcd.print(" kPa");
}

Result

The LCD1602 display will show the water pressure value. Expected result:

SEN0257 Water Pressure Display

Was this article helpful?

TOP