Welcome to the firmware docs! Here you will find...fuinish
The software is divided into four main files:main.ino
, config.h
, mqGas.h
and alarmThresholds.h
.
The program monitors environmental safety parameters using various sensors and logs data to an SD card and serial output.
- Initialize pins, sensors, and libraries
- Read sensor data every
SAMPLING_INTERVAL_MS
- Update LCD menu display and check/triggers alarms
- Log data to SD card and serial output
- Sensor Initialization and Calibration:
- SCD41: Begins periodic measurement upon successful initialization
- ENS160: Configured to operate in standard mode
- MQ Series: Calibrated using
PERFORM_MQ_CALIBRATION
flag, analog values mapped to ppm using exponential regression, R0 value stored in EEPROM
- Alarm System:
- Threshold-based alarm triggering (DANGER/WARNING)
- Activates piezo buzzer and changes RGB LED color to red upon alarm
- Logging:
- SD card (CSV): Records sensor data with timestamp
- Serial output (JSON): Real-time monitoring or integration with other systems
- LCD Menu Interface:
- Utilizes LiquidMenu library for multiple screens displaying sensor readings
- Navigation via push button, with screens for system title, timestamp, and individual sensor readings
This header file contains all configurable settings, pin definitions, thresholds, and other constants used throughout the EnvSafetyMonitor system.
- Debugging Settings
ENABLE_DEBUG_PRINTS
: Toggle for enabling/disabling debug messages.ENABLE_DATA_PRINTS
: Toggle for enabling/disabling data output to Serial.
- Calibration Settings
PERFORM_MQ_CALIBRATION
: Determines whether to calibrate MQ sensors on startup. Note that once this is enabled you should boot the system, allow calibration, then reupload with this setting disabled during production use.
- Logging Settings
LOG_FILE_NAME
: Name of the CSV log file on the SD card.LOG_TO_SD
: Enables/disables logging sensor data to the SD card.
- Standard Colors Definitions
RED
: RGB values for red color.GREEN
: RGB values for green color.
- Sampling Settings
SAMPLING_INTERVAL_MS
: Time interval between consecutive sensor readings.
- I2C LCD Settings
LCD_I2C_ADDRESS
: I2C address of the LCD.LCD_COLUMNS
: Number of columns on the LCD.LCD_ROWS
: Number of rows on the LCD.
- Serial Communication Settings
SERIAL_BAUD_RATE
: Baud rate for Serial communication.
- Debounce Settings
DEBOUNCE_DELAY_MS
: Delay for debouncing the push button.
The following thresholds are used to trigger warning and danger alarms for various environmental parameters.
- Temperature
TEMP_WARNING_THRESHOLD_C
: 30.0°C - Temperature warning thresholdTEMP_DANGER_THRESHOLD_C
: 35.0°C - Temperature danger threshold- Temperatures around 30°C increase the risk of heat stress, while temperatures at or above 35°C pose a higher risk of heat-related illnesses.
- Relative Humidity (RH)
HUMIDITY_WARNING_THRESHOLD_RH
: 60.0%RH - Humidity warning thresholdHUMIDITY_DANGER_THRESHOLD_RH
: 80.0%RH - Humidity danger threshold High humidity levels reduce the body's ability to cool itself, with increased risk at 80% humidity.
-
Carbon Dioxide (CO2)
CO2_WARNING_THRESHOLD_PPM
: 5000 ppm - CO₂ warning thresholdCO2_DANGER_THRESHOLD_PPM
: 15000 ppm - CO₂ danger threshold- Based on: OSHA's PEL for CO₂ is 5,000 ppm over an 8-hour workday, with concentrations above 15,000 ppm causing respiratory issues.
-
Hydrogen (H2)
H2_WARNING_THRESHOLD_PPM
: 4000 ppm - H₂ warning thresholdH2_DANGER_THRESHOLD_PPM
: 8000 ppm - H₂ danger threshold- Based on: Hydrogen is highly flammable, with warning and danger thresholds set at 10% and 20% of the Lower Explosive Limit (LEL).
-
Hydrogen Sulfide (H2S)
H2S_WARNING_THRESHOLD_PPM
: 10 ppm - H₂S warning thresholdH2S_DANGER_THRESHOLD_PPM
: 50 ppm - H₂S danger threshold- Based on: OSHA's PEL for H₂S is 10 ppm over an 8-hour workday, with concentrations above 15 ppm causing eye irritation.
-
Ammonia (NH3)
NH3_WARNING_THRESHOLD_PPM
: 25 ppm - NH₃ warning thresholdNH3_DANGER_THRESHOLD_PPM
: 50 ppm - NH₃ danger threshold- Based on: OSHA's PEL for ammonia is 50 ppm and 25 ppm over an 8-hour workday, with levels above 50 ppm causing eye and respiratory irritation.
-
Methane (CH4)
CH4_WARNING_THRESHOLD_PPM
: 5000 ppm - CH₄ warning thresholdCH4_DANGER_THRESHOLD_PPM
: 10000 ppm - CH₄ danger threshold- Based on: Methane is flammable, with warning and danger thresholds set at 10% and 20% of the Lower Explosive Limit (LEL).
-
Carbon Monoxide (CO)
CO_WARNING_THRESHOLD_PPM
: 35 ppm - CO warning thresholdCO_DANGER_THRESHOLD_PPM
: 50 ppm - CO danger threshold- Based on: OSHA's PEL for CO is 50 ppm and 25 ppm over an 8-hour workday, with levels above 50 ppm causing headaches and dizziness.
The MQ gas sensors detect specific gases by measuring the change in their internal resistance when exposed to different gas concentrations. The resistance value (RS
) changes in proportion to the gas concentration, allowing for the calculation of the gas's parts per million (ppm) level.
The MQ sensors use a logarithmic formula to relate the sensor resistance ratio (RS / R0
) to gas concentration:
ppm = a * (RS / R0)^b
where:
- a and b are constants specific to each sensor and gas, derived from calibration and datasheet values.
- RS is the sensor resistance in the current environment.
- R0 is the baseline resistance of the sensor in clean air, obtained through calibration.
RS is calculated from the sensor's analog voltage reading as follows:
RS = ((V_supply - V_sensor) \* RL) / V_sensor
where:
- V_sensor is the voltage at the sensor’s output.
- V_supply is the supply voltage to the sensor.
- RL is the load resistance in series with the sensor.
R0 represents the sensor’s resistance in a known clean air environment and is used as a baseline for gas concentration calculations. During calibration:
- The sensor's RS is measured in clean air.
- R0 is calculated as the average RS in this environment and is saved in EEPROM to maintain consistency across power cycles.
The clean air ratio (RS / R0 in clean air) is a reference value specific to each sensor. This ratio helps establish the baseline resistance and is used during R0 calibration.
-
MQ-8 Sensor (Hydrogen, H₂)
MQ8_TYPE
: "MQ-8"MQ8_RATIO_CLEAN_AIR
: 70A_VALUE_MQ8
: 71.7592B_VALUE_MQ8
: -0.9760EEPROM_ADDRESS_MQ8_R0
: 0
-
MQ-136 Sensor (Hydrogen Sulfide, H₂S)
MQ136_TYPE
: "MQ-136"MQ136_RATIO_CLEAN_AIR
: 3.6A_VALUE_MQ136
: 1.0715B_VALUE_MQ136
: -0.8877EEPROM_ADDRESS_MQ136_R0
: 4
-
MQ-137 Sensor (Ammonia, NH₃)
MQ137_TYPE
: "MQ-137"MQ137_RATIO_CLEAN_AIR
: 4.2A_VALUE_MQ137
: 1.0613B_VALUE_MQ137
: -0.8905EEPROM_ADDRESS_MQ137_R0
: 8
-
MQ-9b Sensor (Carbon Monoxide, CO & Methane, CH₄)
MQ9B_TYPE
: "MQ-9"MQ9B_RATIO_CLEAN_AIR
: 9.6A_VALUE_MQ9B_CH4
: 4.9059B_VALUE_MQ9B_CH4
: -0.6699A_VALUE_MQ9B_CO
: 4.8726B_VALUE_MQ9B_CO
: -0.6989EEPROM_ADDRESS_MQ9B_R0
: 12SETTLE_TIME_TO_CO
: 20000 msSETTLE_TIME_TO_CH4
: 10000 ms