ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mics_5524.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5
10
11#include "mics_math.h"
12
13namespace esphome::mics_5524 {
14
17 public:
19 MiCS5524GasSensor(float threshold, float gain, float min_ppm, float max_ppm)
20 : curve_{threshold, gain, min_ppm, max_ppm} {}
21
23 void publish_ratio(float ratio);
24
25 float get_min_ppm() const { return this->curve_.min_ppm; }
26 float get_max_ppm() const { return this->curve_.max_ppm; }
27
28 protected:
30};
31
35 public:
38
39 void setup() override;
40 void loop() override;
41 void update() override;
42 void dump_config() override;
43
44#ifdef MICS_5524_GAS_COUNT
45 void register_gas_sensor(MiCS5524GasSensor *gas) { this->gases_.push_back(gas); }
46#endif
47
48 void set_vcc(float vcc) { this->vcc_ = vcc; }
49 void set_voltage_multiplier(float multiplier) { this->voltage_multiplier_ = multiplier; }
50 void set_samples(uint8_t samples) { this->samples_ = samples; }
51 void set_warmup_time(uint32_t warmup) { this->warmup_time_ = warmup; }
53 void set_calibration(uint32_t delay, uint32_t samples, uint32_t sample_interval);
54 void set_air_reference(float air_reference) { this->reference_ = air_reference; }
55
56 protected:
58 float sample_voltage_();
60 float read_air_value_();
61 void publish_ratio_(float ratio);
62
63 void begin_calibration_();
67 bool has_reference_() const { return this->reference_ > 0.0f; }
68
70 float vcc_{5.0f};
73
79
80 static constexpr uint32_t CALIBRATION_START_ID = 0;
82 static constexpr uint32_t CALIBRATION_RETRY_MIN_MS = 10000;
83
87 float calibration_sum_{0.0f};
89 float calibration_min_{0.0f};
90 float calibration_max_{0.0f};
91
93 float reference_{0.0f};
94
95 // Sized by code generation; the host unit tests run none, so the list compiles out there.
96#ifdef MICS_5524_GAS_COUNT
98#endif
99
100 uint8_t samples_{4};
101 bool calibrating_{false};
102};
103
104} // namespace esphome::mics_5524
This class simplifies creating components that periodically check a state.
Definition component.h:514
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:286
The MiCS-5524 breakout: one analog output, converted for every configured gas against the clean-air r...
Definition mics_5524.h:34
uint32_t calibration_samples_
0 when calibration is disabled.
Definition mics_5524.h:76
void set_voltage_multiplier(float multiplier)
Definition mics_5524.h:49
MiCS5524Component(voltage_sampler::VoltageSampler *source)
source must outlive this component, which code generation guarantees.
Definition mics_5524.h:37
void register_gas_sensor(MiCS5524GasSensor *gas)
Definition mics_5524.h:45
uint32_t sample_interval_
Spacing of the clean-air calibration samples (ms); the ~16 ms loop tick is the floor.
Definition mics_5524.h:78
static constexpr uint32_t CALIBRATION_START_ID
Definition mics_5524.h:80
float reference_
Clean-air reference x_air in volts, 0 while there is none.
Definition mics_5524.h:93
StaticVector< MiCS5524GasSensor *, MICS_5524_GAS_COUNT > gases_
Definition mics_5524.h:97
void set_warmup_time(uint32_t warmup)
Definition mics_5524.h:51
float sample_voltage_()
Averaged, scaled output voltage (V); the conversions run back to back.
Definition mics_5524.cpp:66
void set_calibration(uint32_t delay, uint32_t samples, uint32_t sample_interval)
Calibrate at boot: start after delay ms, average samples readings sample_interval ms apart.
float read_air_value_()
x = vcc - V_AO, or NAN (and a status warning) when the voltage is unusable.
Definition mics_5524.cpp:82
voltage_sampler::VoltageSampler * source_
Definition mics_5524.h:69
void set_samples(uint8_t samples)
Definition mics_5524.h:50
static constexpr uint32_t CALIBRATION_RETRY_MIN_MS
Floor of the delay between two calibration attempts (ms).
Definition mics_5524.h:82
void set_air_reference(float air_reference)
Definition mics_5524.h:54
float calibration_min_
Smallest and largest clean-air sample of the run, see calibration_is_stable().
Definition mics_5524.h:89
One gas of a MiCS-5524: converts the measured ratio with the vendor curve of that gas.
Definition mics_5524.h:16
void publish_ratio(float ratio)
Publish the concentration for ratio; NAN publishes unknown.
Definition mics_5524.cpp:13
MiCS5524GasSensor(float threshold, float gain, float min_ppm, float max_ppm)
The vendor constants from coefficients.py.
Definition mics_5524.h:19
Base-class for all sensors.
Definition sensor.h:47
Abstract interface for components to request voltage (usually ADC readings)
AlsGain501 gain
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
Per-gas constants of the vendor model.
Definition mics_math.h:11
float max_ppm
upper end of the measuring range
Definition mics_math.h:15
float min_ppm
the vendor reports 0 ppm below this
Definition mics_math.h:14