This example demonstrates how to measure gauge pressure using an Arduino UNO and a 0–5V pressure sensor. The voltage is converted into multiple pressure units:
The sensor’s voltage output is linearly proportional to gauge pressure.
/*
* © 2024 Copyright Peter I. Dunne, all rights reserved
* Prepared for educational use
* Released under the Mozilla Public License
* Arduino UNO Pressure Measurement
* Measures gauge pressure and converts readings to Pascals, kilopascals, bar, PSI, and ATM.
*/
const int pressurePin = A0;
const float VRef = 5.0;
const int maxADCValue = 1023;
// ─── Sensor parameters (updated for ratiometric 0.5–4.5V sensors) ───
const float vmin = 0.5; // Sensor output at 0% pressure
const float vmax = 4.5; // Sensor output at 100% pressure
const float pmax = 10.0; // Full-scale pressure in bar
const bool gauge = true; // true = gauge sensor, false = absolute sensor
// Unit conversions
const float barToPa = 1e5;
const float barToPSI = 14.5038;
const float barToATM = 0.986923;
// Convert voltage to pressure in bar
float voltageToPressure(float voltage) {
// If below vmin, clamp to zero pressure
if (voltage <= vmin) return 0.0;
// If above vmax, clamp to max pressure
if (voltage >= vmax) return pmax;
// Linear scaling
float scaled = (voltage - vmin) / (vmax - vmin); // 0 → 1 range
float pressure = scaled * pmax;
// Gauge sensors measure relative pressure
// Absolute sensors include atmospheric baseline internally
if (!gauge) {
return pressure; // absolute pressure directly
}
// Gauge = pressure above atmospheric, output already correct
return pressure;
}
void setup() {
Serial.begin(115200);
Serial.println("Arduino pressure measurement, by Peter Ivan Dunne, ©2024");
Serial.println("Released under the Mozilla Public License");
Serial.println("https://jazenga.com/educational");
Serial.println("Demonstration for 0-5v proportional pressure sensors (0.5–4.5V typical)");
Serial.println("Calibration required for correct pressure range.");
}
void loop() {
int adcValue = analogRead(pressurePin);
float voltage = (adcValue / float(maxADCValue)) * VRef;
float pressureBar = voltageToPressure(voltage);
float pressurePa = pressureBar * barToPa;
float pressureKPa = pressurePa / 1000.0;
float pressurePSI = pressureBar * barToPSI;
float pressureATM = pressureBar * barToATM;
Serial.print("Voltage: ");
Serial.print(voltage, 3);
Serial.print(" V | Pressure: ");
Serial.print(pressureBar, 3);
Serial.print(" bar, ");
Serial.print(pressureKPa, 2);
Serial.print(" kPa, ");
Serial.print(pressurePa, 2);
Serial.print(" Pa, ");
Serial.print(pressurePSI, 2);
Serial.print(" PSI, ");
Serial.print(pressureATM, 2);
Serial.println(" ATM");
delay(1000);
}
Most modern ratiometric pressure sensors powered from 5 V output a scaled voltage in the 0.5 V to 4.5 V range instead of a full 0–5 V sweep. This represents the usable pressure range while leaving headroom to detect wiring faults such as short-to-ground or short-to-supply.
scaled = (V − vmin) / (vmax − vmin)Values below vmin are clamped to 0; values above vmax are clamped to pmax.
pressureBar = scaled × pmax
voltage = (ADC / 1023) × VrefWhere Vref is nominally 5.0 V unless using AREF or internal reference.