Refactor MQ7 sensor implementation: remove old files and introduce MQ7v2 with updated reading logic
This commit is contained in:
@@ -1,8 +0,0 @@
|
|||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
#define HEATER_PIN 16
|
|
||||||
#define SENSOR_PIN 34
|
|
||||||
|
|
||||||
void MQ7_Init();
|
|
||||||
void MQ7_Read(float &sensorValue);
|
|
||||||
void pwmBitBang(int totalMs, int highPct, int cycleMs);
|
|
||||||
10
hardware/include/MQ7v2.hpp
Normal file
10
hardware/include/MQ7v2.hpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#define MQ7_PIN 34
|
||||||
|
#define VCC 5.0
|
||||||
|
#define ADC_RES 1023.0
|
||||||
|
#define RL 10000.0 // 10kΩ
|
||||||
|
#define RO 10000.0 // Resistencia del aire limpio
|
||||||
|
|
||||||
|
void MQ7_Init();
|
||||||
|
void MQ7_Read(float &sensorValue);
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
#include "BME280.hpp"
|
#include "BME280.hpp"
|
||||||
#include "GPS.hpp"
|
#include "GPS.hpp"
|
||||||
#include "MAX7219.hpp"
|
#include "MAX7219.hpp"
|
||||||
#include "MQ7.hpp"
|
#include "MQ7v2.hpp"
|
||||||
|
|
||||||
uint32_t getChipID();
|
uint32_t getChipID();
|
||||||
void prettyReadMQ7();
|
void prettyReadMQ7();
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
#include "MQ7.hpp"
|
|
||||||
|
|
||||||
void MQ7_Init()
|
|
||||||
{
|
|
||||||
pinMode(HEATER_PIN, OUTPUT);
|
|
||||||
pinMode(SENSOR_PIN, INPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MQ7_Read(float &sensorValue)
|
|
||||||
{
|
|
||||||
Serial.println("\t - Calentando MQ7");
|
|
||||||
digitalWrite(HEATER_PIN, HIGH);
|
|
||||||
delay(60000);
|
|
||||||
|
|
||||||
Serial.println("\t - Enfriando MQ7");
|
|
||||||
pwmBitBang(90000, 28, 100);
|
|
||||||
|
|
||||||
const int N = 1;
|
|
||||||
long sum = 0;
|
|
||||||
for (int i = 0; i < N; i++)
|
|
||||||
{
|
|
||||||
sum += analogRead(SENSOR_PIN);
|
|
||||||
delay(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
sensorValue = sum / float(N);
|
|
||||||
//sensorVolt = sensorValue * (3.3 / 4095.0); // ADC 12 bits, 3.3V
|
|
||||||
}
|
|
||||||
|
|
||||||
// generamos PWM por software usando bit banging
|
|
||||||
void pwmBitBang(int totalMs, int highPct, int cycleMs)
|
|
||||||
{
|
|
||||||
int onTime = cycleMs * highPct / 100;
|
|
||||||
int offTime = cycleMs - onTime;
|
|
||||||
int elapsed = 0;
|
|
||||||
|
|
||||||
while (elapsed < totalMs)
|
|
||||||
{
|
|
||||||
digitalWrite(HEATER_PIN, HIGH);
|
|
||||||
delay(onTime);
|
|
||||||
digitalWrite(HEATER_PIN, LOW);
|
|
||||||
delay(offTime);
|
|
||||||
elapsed += cycleMs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
23
hardware/src/lib/sensor/MQ7v2.cpp
Normal file
23
hardware/src/lib/sensor/MQ7v2.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include "MQ7v2.hpp"
|
||||||
|
|
||||||
|
void MQ7_Init()
|
||||||
|
{
|
||||||
|
pinMode(MQ7_PIN, INPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MQ7_Read(float &sensorValue)
|
||||||
|
{
|
||||||
|
int adcValue = analogRead(MQ7_PIN);
|
||||||
|
float voltage = (adcValue / ADC_RES) * VCC;
|
||||||
|
|
||||||
|
if (voltage <= 0.1) {
|
||||||
|
sensorValue = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rs = (VCC - voltage) * RL / voltage;
|
||||||
|
float ratio = rs / RO;
|
||||||
|
float ppm = pow(10, (-1.5 * log10(ratio) + 0.8));
|
||||||
|
|
||||||
|
sensorValue = ppm;
|
||||||
|
}
|
||||||
@@ -11,8 +11,6 @@ extern MD_Parola display; // Display object
|
|||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
/*GPS_Init();
|
|
||||||
Serial.println("GPS inicializado"); */
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
Serial.println("Iniciando...");
|
Serial.println("Iniciando...");
|
||||||
@@ -21,11 +19,20 @@ void setup()
|
|||||||
MAX7219_Init();
|
MAX7219_Init();
|
||||||
Serial.println("Display inicializado");
|
Serial.println("Display inicializado");
|
||||||
MQ7_Init();
|
MQ7_Init();
|
||||||
Serial.println("Sensor MQ7 inicializado");
|
Serial.println("Sensor MQ7 inicializado");
|
||||||
|
|
||||||
|
/*
|
||||||
|
GPS_Init();
|
||||||
|
Serial.println("GPS inicializado");
|
||||||
|
*/
|
||||||
|
|
||||||
prettyReadBME280();
|
prettyReadBME280();
|
||||||
testMatrix();
|
testMatrix();
|
||||||
prettyReadMQ7();
|
prettyReadMQ7();
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
@@ -40,7 +47,6 @@ void prettyReadMQ7()
|
|||||||
{
|
{
|
||||||
Serial.println("Leyendo sensor MQ7...");
|
Serial.println("Leyendo sensor MQ7...");
|
||||||
MQ7_Read(sensorValue);
|
MQ7_Read(sensorValue);
|
||||||
//Serial.print("\t - Voltaje: "); Serial.print(sensorVolt); Serial.print("V\r\n");
|
|
||||||
Serial.print("\t - Valor sensor: "); Serial.print(sensorValue); Serial.print("\r\n");
|
Serial.print("\t - Valor sensor: "); Serial.print(sensorValue); Serial.print("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user