backup 14041130
This commit is contained in:
148
14041130/mq/calibr/Voltage_Reader.h
Normal file
148
14041130/mq/calibr/Voltage_Reader.h
Normal file
@@ -0,0 +1,148 @@
|
||||
#ifndef VOLTAGE_READER_H
|
||||
#define VOLTAGE_READER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define VOLTAGE_DIVIDER_PIN PA0 // پین خواندن ولتاژ
|
||||
#define BATTERY_VOLTAGE_PIN_A3 PA3 // پین جدید برای باتری
|
||||
#define VOLTAGE_DIVIDER_RATIO 2.0 // نسبت تقسیم (R1=R2=10k => نسبت = 2)
|
||||
#define ADC_REF_VOLTAGE 3.3 // ولتاژ مرجع ADC در STM32 (معمولاً 3.3V)
|
||||
#define ADC_RESOLUTION 4096 // رزولوشن ADC 12-bit = 4096
|
||||
|
||||
|
||||
class VoltageReader {
|
||||
private:
|
||||
float filteredVoltage = 0.0;
|
||||
bool firstReading = true; // فلگ برای اولین خواندن
|
||||
const float alpha = 0.3; // ضریب فیلتر را کمی بیشتر کردم
|
||||
|
||||
public:
|
||||
VoltageReader() {
|
||||
init();
|
||||
}
|
||||
|
||||
void init() {
|
||||
pinMode(VOLTAGE_DIVIDER_PIN, INPUT_ANALOG);
|
||||
analogReadResolution(12); // تنظیم رزولوشن ADC به 12-bit
|
||||
delay(100); // کمی بیشتر صبر کن
|
||||
|
||||
// چند بار خواندن برای تخلیه خازنهای داخلی
|
||||
for (int i = 0; i < 10; i++) {
|
||||
analogRead(VOLTAGE_DIVIDER_PIN);
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
|
||||
// خواندن ولتاژ بدون فیلتر
|
||||
float readRawVoltage() {
|
||||
int samples = 32; // تعداد نمونهها را بیشتر کردم
|
||||
long sum = 0;
|
||||
|
||||
for (int i = 0; i < samples; i++) {
|
||||
sum += analogRead(VOLTAGE_DIVIDER_PIN);
|
||||
delayMicroseconds(20);
|
||||
}
|
||||
|
||||
int rawValue = sum / samples;
|
||||
|
||||
// تبدیل به ولتاژ
|
||||
float voltageAtPin = (rawValue * ADC_REF_VOLTAGE) / ADC_RESOLUTION;
|
||||
|
||||
// محاسبه ولتاژ اصلی
|
||||
float actualVoltage = voltageAtPin * VOLTAGE_DIVIDER_RATIO;
|
||||
|
||||
return actualVoltage;
|
||||
}
|
||||
|
||||
// خواندن با فیلتر
|
||||
float readVoltage() {
|
||||
float currentVoltage = readRawVoltage();
|
||||
|
||||
// اگر اولین بار است، فیلتر را با مقدار فعلی مقداردهی کن
|
||||
if (firstReading) {
|
||||
filteredVoltage = currentVoltage;
|
||||
firstReading = false;
|
||||
} else {
|
||||
// اعمال فیلتر Low-pass
|
||||
filteredVoltage = (alpha * currentVoltage) + ((1 - alpha) * filteredVoltage);
|
||||
}
|
||||
|
||||
return currentVoltage; // actual voltage برمیگردانیم
|
||||
}
|
||||
|
||||
// دریافت ولتاژ فیلتر شده
|
||||
float getFilteredVoltage() {
|
||||
return filteredVoltage;
|
||||
}
|
||||
|
||||
// تست سلامت مدار
|
||||
bool testCircuit() {
|
||||
Serial1.println("\n🔌 Testing voltage divider circuit...");
|
||||
|
||||
// چند بار خواندن برای اطمینان
|
||||
float sum = 0;
|
||||
int readings = 10;
|
||||
|
||||
for (int i = 0; i < readings; i++) {
|
||||
sum += readRawVoltage();
|
||||
delay(50);
|
||||
}
|
||||
|
||||
float avgVoltage = sum / readings;
|
||||
|
||||
Serial1.print("Average voltage: ");
|
||||
Serial1.print(avgVoltage, 2);
|
||||
Serial1.println("V");
|
||||
|
||||
Serial1.print("Raw ADC value: ");
|
||||
Serial1.println(analogRead(VOLTAGE_DIVIDER_PIN));
|
||||
|
||||
// ولتاژ در پین
|
||||
float pinVoltage = avgVoltage / VOLTAGE_DIVIDER_RATIO;
|
||||
Serial1.print("Voltage at PA0 pin: ");
|
||||
Serial1.print(pinVoltage, 2);
|
||||
Serial1.println("V");
|
||||
|
||||
if (avgVoltage > 4.5 && avgVoltage < 5.5) {
|
||||
Serial1.println("✅ Circuit OK (within expected 5V ±10%)");
|
||||
return true;
|
||||
} else {
|
||||
Serial1.print("❌ Expected ~5V, got ");
|
||||
Serial1.print(avgVoltage, 2);
|
||||
Serial1.println("V");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// نمایش اطلاعات دیباگ
|
||||
void debugInfo() {
|
||||
float rawVoltage = readRawVoltage();
|
||||
int rawADC = analogRead(VOLTAGE_DIVIDER_PIN);
|
||||
float pinVoltage = rawVoltage / VOLTAGE_DIVIDER_RATIO;
|
||||
|
||||
Serial1.println("\n📊 Voltage Debug Info:");
|
||||
Serial1.print("Raw ADC value: ");
|
||||
Serial1.println(rawADC);
|
||||
Serial1.print("Voltage at PA0 pin: ");
|
||||
Serial1.print(pinVoltage, 3);
|
||||
Serial1.println("V");
|
||||
Serial1.print("Actual voltage (raw): ");
|
||||
Serial1.print(rawVoltage, 3);
|
||||
Serial1.println("V");
|
||||
Serial1.print("Filtered voltage: ");
|
||||
Serial1.print(filteredVoltage, 3);
|
||||
Serial1.println("V");
|
||||
}
|
||||
|
||||
// ریست فیلتر
|
||||
void resetFilter() {
|
||||
filteredVoltage = 0.0;
|
||||
firstReading = true;
|
||||
Serial1.println("✅ Voltage filter reset");
|
||||
}
|
||||
};
|
||||
|
||||
// ایجاد نمونه سراسری
|
||||
VoltageReader voltageReader;
|
||||
|
||||
#endif // VOLTAGE_READER_H
|
||||
127
14041130/mq/calibr/calibr.ino
Normal file
127
14041130/mq/calibr/calibr.ino
Normal file
@@ -0,0 +1,127 @@
|
||||
#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);
|
||||
// }
|
||||
57
14041130/mq/ppm/ppm.ino
Normal file
57
14041130/mq/ppm/ppm.ino
Normal file
@@ -0,0 +1,57 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user