This example demonstrates how to measure gauge pressure using an Arduino UNO and a sensor with a 0-5V output. The code reads the sensor output and converts the voltage to pressure, displaying it in multiple units:
/*
* © 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; // Pressure sensor connected to A0
const float VRef = 5.0; // Reference voltage (5V)
const int maxADCValue = 1023; // 10-bit ADC resolution
// Conversion factors
const float pressureMaxVoltage = 5.0; // Maximum output voltage from the sensor
const float pressureMaxValue = 10.0; // Maximum pressure value corresponding to 5V (example: 10 bar)
// Conversion constants
const float barToPa = 1e5; // 1 bar = 100,000 Pa
const float barToPSI = 14.5038; // 1 bar = 14.5038 PSI
const float barToATM = 0.986923; // 1 bar = 0.986923 ATM
// Function to convert voltage to pressure in bar
float voltageToPressure(float voltage) {
return (voltage / pressureMaxVoltage) * pressureMaxValue;
}
void setup() {
Serial.begin(115200); // Start serial communication
Serial.println("Arduino pressure measurement, by Peter Ivan Dunne, ©2024, all rights reserved");
Serial.println("Released under the Mozilla Public License");
Serial.println("https://jazenga.com/educational");
Serial.println("This demonstration uses 0-5v proportional representation sensors");
Serial.println("The code must be calibrated for the correct pressure range ");
}
void loop() {
// Read the analog value from the pressure sensor
int adcValue = analogRead(pressurePin);
// Convert ADC value to voltage
float voltage = (adcValue / float(maxADCValue)) * VRef;
// Convert voltage to pressure in bar
float pressureBar = voltageToPressure(voltage);
// Convert pressure to Pascals, kilopascals, PSI, and ATM
float pressurePa = pressureBar * barToPa; // Pascals
float pressureKPa = pressurePa / 1e3; // Kilopascals
float pressurePSI = pressureBar * barToPSI; // Pounds per Square Inch
float pressureATM = pressureBar * barToATM; // Atmospheres
// Output pressure readings
Serial.print("Pressure: ");
Serial.print(pressureBar, 2);
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); // Delay between readings
}