Pressure Sensor Interface with Alarm System by Maruthavel T — ESP32 simulation

This project monitors pressure in real time using a BME280 sensor and ESP32. The pressure value is displayed on an OLED screen and compared with predefined limits. When abnormal pressure is detected, an LED and buzzer provide an immediate warning.

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by maruthavelssiet.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// ================= PIN SETTINGS =================

#define SDA_PIN 21
#define SCL_PIN 22

#define LED_PIN 26
#define BUZZER_PIN 27

#define BME280_ADDR 0x76
#define OLED_ADDR 0x3C

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64


// ================= OBJECTS =================

Adafruit_BME280 bme;

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  -1
);


// ================= PRESSURE LIMITS =================

const float LOW_LIMIT = 950.0;
const float HIGH_LIMIT = 1050.0;


// ================= SETUP =================

void setup() {

  Serial.begin(115200);

  // Start I2C
  Wire.begin(SDA_PIN, SCL_PIN);

  // Set LED and Buzzer as output
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  // Initially OFF
  digitalWrite(LED_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);


  // ================= BME280 START =================

  if (!bme.begin(BME280_ADDR)) {

    Serial.println("BME280 NOT FOUND!");

    while (1) {
      delay(10);
    }
  }


  // ================= OLED START =================

  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {

    Serial.println("OLED NOT FOUND!");

    while (1) {
      delay(10);
    }
  }


  // ================= STARTING SCREEN =================

  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);

  display.setTextSize(1);

  display.setCursor(0, 0);
  display.println("PRESSURE MONITOR");

  display.setCursor(0, 20);
  display.println("System Starting...");

  display.display();

  delay(1500);
}


// ================= MAIN LOOP =================

void loop() {

  // Read BME280 values

  float temperature = bme.readTemperature();

  float pressure = bme.readPressure() / 100.0;

  float humidity = bme.readHumidity();


  // ================= PRESSURE CHECK =================

  bool alarm = false;

  if (pressure < LOW_LIMIT || pressure > HIGH_LIMIT) {

    alarm = true;
  }


  // ================= LED AND BUZZER =================

  if (alarm == true) {

    digitalWrite(LED_PIN, HIGH);

    digitalWrite(BUZZER_PIN, HIGH);

  }
  else {

    digitalWrite(LED_PIN, LOW);

    digitalWrite(BUZZER_PIN, LOW);
  }


  // ================= SERIAL MONITOR =================

  Serial.println("----------------------------");

  Serial.print("Temperature: ");
  Serial.print(temperature, 2);
  Serial.println(" C");

  Serial.print("Pressure: ");
  Serial.print(pressure, 2);
  Serial.println(" hPa");

  Serial.print("Humidity: ");
  Serial.print(humidity, 2);
  Serial.println(" %");


  // Pressure status

  if (pressure < LOW_LIMIT) {

    Serial.println("STATUS: LOW PRESSURE");

  }
  else if (pressure > HIGH_LIMIT) {

    Serial.println("STATUS: HIGH PRESSURE");

  }
  else {

    Serial.println("STATUS: NORMAL");
  }


  // Alarm status

  if (alarm == true) {

    Serial.println("ALARM: ON");

  }
  else {

    Serial.println("ALARM: OFF");
  }


  // ===

More projects by maruthavelssiet