127 lines
5.5 KiB
C++
127 lines
5.5 KiB
C++
#include <Arduino.h>
|
||
#include "Voltage_Reader.h"
|
||
|
||
// ========== پارامترهای مدار (مقادیر دقیق اندازهگیریشده) ==========
|
||
#define VREF_ADC 3.3f // ولتاژ مرجع ADC (معمولاً 3.3V)
|
||
#define ADC_RESOLUTION 4095.0f // رزولوشن ADC (12 بیت)
|
||
#define R1 2190.0f // مقاومت سری اول (اهم) - اندازهگیری شده
|
||
#define R2 3270.0f // مقاومت به زمین (اهم) - اندازهگیری شده
|
||
#define RSERIES 98.0f // مقاومت سری ۱۰۰ اهم (تأثیر ناچیز، میتوان صفر گذاشت)
|
||
//#define VCC_SENSOR 4.67f // ولتاژ تغذیه سنسور MQ7
|
||
#define RL_SENSOR 990.0f // مقاومت بار ماژول (معمولاً ۱۰kΩ)
|
||
#define ADC_PIN A2 // پین ADC (PA2 در STM32)
|
||
#define NUM_SAMPLES 10 // تعداد نمونه برای میانگینگیری
|
||
|
||
|
||
|
||
float VCC_SENSOR = 4.67f; // ولتاژ تغذیه سنسور MQ7
|
||
float MQ7_R0 = 21000.0; // مقدار R0 (پس از کالیبراسیون) – تقریب اولیه ۲۰ کیلواهم
|
||
bool calibrationComplete = false;
|
||
unsigned long lastReadTime = 0;
|
||
const unsigned long readInterval = 3000;
|
||
|
||
// ================================================================
|
||
|
||
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;
|
||
// محاسبه ولتاژ خروجی سنسور با جبران تقسیم ولتاژ (R1 و R2)
|
||
float vOut = vAdc * (R1 + R2) / R2;
|
||
return vOut;
|
||
}
|
||
|
||
float calculateRs(float vOut) {
|
||
if (vOut <= 0.0f) return 0.0f;
|
||
return RL_SENSOR * (VCC_SENSOR / vOut - 1.0f);
|
||
}
|
||
|
||
void setup() {
|
||
Serial1.begin(115200); // راهاندازی Serial1 با نرخ ۱۱۵۲۰۰
|
||
analogReadResolution(12); // تنظیم ADC روی ۱۲ بیت
|
||
pinMode(ADC_PIN, INPUT);
|
||
delay(2000);
|
||
voltageReader.debugInfo();
|
||
VCC_SENSOR=voltageReader.readVoltage();
|
||
|
||
Serial1.println("MQ7 Calibration Mode");
|
||
Serial1.print("R1 = "); Serial1.print(R1); Serial1.println(" ohms");
|
||
Serial1.print("R2 = "); Serial1.print(R2); Serial1.println(" ohms");
|
||
Serial1.print("VCC_SENSOR = "); Serial1.print(VCC_SENSOR); Serial1.println(" V");
|
||
Serial1.print("RL_SENSOR = "); Serial1.print(RL_SENSOR); Serial1.println(" ohms");
|
||
Serial1.println("\nPlace sensor in clean air and wait 2 minutes to stabilize...");
|
||
delay(120000); // ۲ دقیقه زمان برای پایدار شدن سنسور
|
||
Serial1.println("Now measuring. Record Rs value as R0 when stable.\n");
|
||
}
|
||
|
||
void loop() {
|
||
VCC_SENSOR=voltageReader.readVoltage();
|
||
|
||
float vOut = readSensorVoltage();
|
||
float rs = calculateRs(vOut);
|
||
Serial1.print("Vout_sensor = "); Serial1.print(vOut, 4); Serial1.print(" V, Rs = ");
|
||
Serial1.print(rs, 1); Serial1.println(" ohms");
|
||
delay(5000);
|
||
}
|
||
// #include <Arduino.h>
|
||
|
||
// // ========== پارامترهای مدار (بر اساس آخرین اندازهگیری) ==========
|
||
// #define VREF_ADC 3.3f // ولتاژ مرجع ADC (معمولاً 3.3V)
|
||
// #define ADC_RESOLUTION 4095.0f // رزولوشن ADC (12 بیت)
|
||
// #define R1 2190.0f // مقاومت سری اول (اهم) - اندازهگیری شده
|
||
// #define R2 3270.0f // مقاومت به زمین (اهم) - اندازهگیری شده
|
||
// // مقاومت سری ۹۹ اهم تأثیر ناچیزی دارد، در محاسبات لحاظ نمیشود
|
||
// #define VCC_SENSOR 4.9f // ولتاژ تغذیه سنسور MQ7
|
||
// #define RL_SENSOR 839.0f // مقاومت بار ماژول (مقاومت بین خروجی آنالوگ و GND)
|
||
// #define R0 20000.0f // مقدار R0 از کالیبراسیون (بر حسب اهم) – حدود ۲۰ کیلو اهم
|
||
// #define CO_A 1.9f // ضریب A در فرمول ppm = A * (Rs/R0)^B
|
||
// #define CO_B -0.6f // ضریب B (معمولاً منفی)
|
||
// #define ADC_PIN PA2 // پین ADC (PA2 در STM32)
|
||
// #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;
|
||
// // بازگشت ولتاژ خروجی سنسور با جبران تقسیم ولتاژ (R1 و R2)
|
||
// 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);
|
||
// }
|