57 lines
1.6 KiB
C++
57 lines
1.6 KiB
C++
#include <Arduino.h>
|
|
|
|
// ========== پارامترهای مدار ==========
|
|
#define VREF_ADC 3.3f
|
|
#define ADC_RESOLUTION 4095.0f
|
|
#define R1 1448.0f
|
|
#define R2 1566.0f
|
|
#define VCC_SENSOR 4.67f
|
|
#define RL_SENSOR 10000.0f
|
|
#define R0 139160.0f // ← مقدار R0 بدستآمده از کالیبراسیون (بر حسب اهم)
|
|
#define CO_A 1.9f // ضریب A در فرمول ppm = A * (Rs/R0)^B
|
|
#define CO_B -0.6f // ضریب B (معمولاً منفی)
|
|
#define ADC_PIN A2
|
|
#define NUM_SAMPLES 10
|
|
|
|
// =====================================
|
|
|
|
float readSensorVoltage() {
|
|
uint32_t sum = 0;
|
|
for (int i = 0; i < NUM_SAMPLES; i++) {
|
|
sum += analogRead(ADC_PIN);
|
|
delay(10);
|
|
}
|
|
float adcValue = sum / (float)NUM_SAMPLES;
|
|
float vAdc = (adcValue / ADC_RESOLUTION) * VREF_ADC;
|
|
return vAdc * (R1 + R2) / R2;
|
|
}
|
|
|
|
float calculateRs(float vOut) {
|
|
if (vOut <= 0.0f) return 0.0f;
|
|
return RL_SENSOR * (VCC_SENSOR / vOut - 1.0f);
|
|
}
|
|
|
|
float calculatePPM(float rs) {
|
|
float ratio = rs / R0;
|
|
if (ratio <= 0.0f) return 0.0f;
|
|
return CO_A * pow(ratio, CO_B);
|
|
}
|
|
|
|
void setup() {
|
|
Serial1.begin(115200);
|
|
analogReadResolution(12);
|
|
pinMode(ADC_PIN, INPUT);
|
|
|
|
Serial1.println("MQ7 CO Measurement");
|
|
Serial1.print("R0 = "); Serial1.print(R0); Serial1.println(" ohms");
|
|
Serial1.println("Starting...\n");
|
|
delay(3000);
|
|
}
|
|
|
|
void loop() {
|
|
float vOut = readSensorVoltage();
|
|
float rs = calculateRs(vOut);
|
|
float ppm = calculatePPM(rs);
|
|
Serial1.print("CO = "); Serial1.print(ppm, 1); Serial1.println(" ppm");
|
|
delay(2000);
|
|
} |