Arduino Tutorials

How to Use a Pressure Sensor with Arduino Nano and LCD

Welcome to Engineers Butty Lab BD

Phone: 01888-011500

Address: 31 A/B, Seyed Villa, Jobbar Housing Society, Mazar Road, Mirpur -1, Dhaka.

Introduction

Pressure measurement is an important requirement in many electronics and IoT projects. In this tutorial, we will interface an HX710B pressure sensor module with an Arduino Nano and display the readings on a 16×2 I2C LCD.

To better understand the project, we will divide the development process into two stages:

  1. Primary Code – Reading and displaying raw sensor data.
  2. Final Code – Adding automatic calibration and converting raw values into pressure units (kPa).

This step-by-step approach helps beginners understand how the sensor works before moving to actual pressure measurements.

Components Required

  • Arduino Nano
  • HX710B Pressure Sensor Module
  • 16×2 I2C LCD
  • Jumper Wires
  • Breadboard (Optional)

Circuit Diagram Connections

HX710B to Arduino Nano

HX710B Pin

Arduino Nano

VCC

5V

GND

GND

DOUT

D3

SCK

D2

I2C LCD to Arduino Nano

LCD Pin

Arduino Nano

VCC

5V

GND

GND

SDA

A4

SCL

A5

Stage 1: Primary Code – Reading Raw Sensor Values

Before converting the sensor output into pressure units, it is important to verify that the sensor and HX710B are working correctly.

The primary code simply reads the 24-bit output from the HX710B and displays the raw values on both the Serial Monitor and LCD.

Primary Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define DOUT 3
#define SCK  2

LiquidCrystal_I2C lcd(0x27, 16, 2);

long readHX710B()
{
  long Count = 0;

  while (digitalRead(DOUT));

  for (int i = 0; i < 24; i++)
  {
    digitalWrite(SCK, HIGH);
    Count = Count << 1;
    digitalWrite(SCK, LOW);

    if (digitalRead(DOUT))
      Count++;
  }

  digitalWrite(SCK, HIGH);
  Count ^= 0x800000;
  digitalWrite(SCK, LOW);

  return Count;
}

void setup()
{
  Serial.begin(9600);

  pinMode(SCK, OUTPUT);
  pinMode(DOUT, INPUT);

  digitalWrite(SCK, LOW);

  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("Pressure Sensor");

  Serial.println("Pressure Sensor Test");

  delay(2000);
  lcd.clear();
}

void loop()
{
  long pressureValue = readHX710B();

  Serial.print("Raw Pressure: ");
  Serial.println(pressureValue);

  lcd.setCursor(0, 0);
  lcd.print("Pressure Raw:");

  lcd.setCursor(0, 1);
  lcd.print(pressureValue);
  lcd.print("     ");

  delay(500);
}

Output of Primary Code

Serial Monitor

9417734
9413811
9409352

LCD Display

Pressure Raw:
9417734

The displayed numbers are the raw ADC values from the HX710B. These values change when pressure is applied to the sensor.

At this stage, the readings are not yet meaningful pressure units. They simply confirm that communication between the Arduino and sensor is working correctly.

Why We Need Calibration

Raw values alone do not indicate actual pressure.

Every pressure sensor has:

  • Offset error
  • Manufacturing variations
  • Different zero points

Therefore, calibration is required to establish a reference value when no pressure is applied.

Stage 2: Final Code – Pressure Measurement in kPa

In the final version:

  • Automatic zero calibration is performed at startup.
  • Twenty readings are averaged.
  • The average becomes the zero-pressure reference.
  • Raw values are converted into pressure units (kPa).
  • Pressure is displayed on both the LCD and Serial Monitor.

Auto-Zero Calibration

When the Arduino starts, it collects 20 readings:

long sum = 0;

for (int i = 0; i < 20; i++)
{
    sum += readHX710B();
    delay(50);
}

zeroRaw = sum / 20;

The LCD shows:

Calibration OK

This value is stored as the sensor's zero-pressure point.

Pressure Conversion Formula

float pressure_kPa =
((float)(raw - zeroRaw) * 40.0) /
(16777215 - zeroRaw);

Where:

Variable

Description

raw

Current sensor value

zeroRaw

Zero calibration value

40.0

Sensor full-scale range (40 kPa)

16777215

Maximum 24-bit ADC value

Final Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define DOUT 3
#define SCK  2

LiquidCrystal_I2C lcd(0x27, 16, 2);

long zeroRaw;

long readHX710B()
{
  long Count = 0;

  while (digitalRead(DOUT));

  for (int i = 0; i < 24; i++)
  {
    digitalWrite(SCK, HIGH);
    Count <<= 1;
    digitalWrite(SCK, LOW);

    if (digitalRead(DOUT))
      Count++;
  }

  digitalWrite(SCK, HIGH);
  Count ^= 0x800000;
  digitalWrite(SCK, LOW);

  return Count;
}

void setup()
{
  Serial.begin(9600);

  pinMode(SCK, OUTPUT);
  pinMode(DOUT, INPUT);

  digitalWrite(SCK, LOW);

  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("Pressure Sensor");

  delay(2000);

  long sum = 0;

  for (int i = 0; i < 20; i++)
  {
    sum += readHX710B();
    delay(50);
  }

  zeroRaw = sum / 20;

  Serial.println("Calibration Complete");

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Calibration OK");

  delay(1500);
  lcd.clear();
}

void loop()
{
  long raw = readHX710B();

  float pressure_kPa =
    ((float)(raw - zeroRaw) * 40.0) /
    (16777215 - zeroRaw);

  if (pressure_kPa < 0)
    pressure_kPa = 0;

  Serial.print("Pressure: ");
  Serial.print(pressure_kPa, 2);
  Serial.println(" kPa");

  lcd.setCursor(0, 0);
  lcd.print("Pressure:");

  lcd.setCursor(0, 1);
  lcd.print(pressure_kPa, 2);
  lcd.print(" kPa   ");

  delay(200);
}

Output of Final Code

Serial Monitor

Pressure: 0.00 kPa
Pressure: 0.00 kPa
Pressure: 1.86 kPa
Pressure: 13.85 kPa
Pressure: 19.78 kPa
Pressure: 20.19 kPa
Pressure: 20.18 kPa
Pressure: 20.66 kPa
Pressure: 21.73 kPa
Pressure: 25.71 kPa
Pressure: 27.06 kPa
Pressure: 26.40 kPa
Pressure: 26.08 kPa
Pressure: 25.86 kPa
Pressure: 26.47 kPa
Pressure: 27.37 kPa
Pressure: 9.58 kPa
Pressure: 0.99 kPa

LCD Display

Pressure:
26.40 kPa

The pressure value increases when pressure is applied and decreases when pressure is released.

Comparison Between Primary and Final Code

Feature

Primary Code

Final Code

Read HX710B Data

Display on LCD

Serial Output

Auto Calibration

Pressure Conversion

Pressure Unit (kPa)

Practical Measurement

Applications

This project can be used in:

  • Water level monitoring
  • Air pressure measurement
  • Vacuum monitoring
  • Industrial automation
  • Pneumatic systems
  • Weather stations
  • IoT monitoring systems

Conclusion

In this project, we first used a Primary Code to verify communication between the HX710B pressure sensor and Arduino Nano by displaying raw ADC values. After confirming proper operation, we upgraded the project with a Final Code that performs automatic zero calibration and converts the raw readings into meaningful pressure values in kPa.

Leave a Reply

Your email address will not be published. Required fields are marked *