Well do you want additional help?
Readings were flat zero.
Well as I said, I can suggest further debugging, just let me know
Well do you want additional help?
Readings were flat zero.
Well as I said, I can suggest further debugging, just let me know
I do not have a proper PSU for 5v…![]()
For now you don't need one.
Hey,
I finally decided to release my sketch…
so if anyone has an idea why i get (roughly) correct AQI values with Desktop power and lower values while on phone charger…
please let me know…
// Include necessary libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include "MQ135.h"
int temperature = 1;
int humidity = 1;
int Polluant = 1;
int PreviousP = 1; // must be 1 at start --- to keep the previous AQI in memory
int i = 0; //counter at the very end (to compare AQI's)
int F = 0; //(for cursor positioning at the very end)
// Define sensor pins
#define DHTTYPE DHT11 // DHT11 or DHT22, depends on the sensor... and I have a DHT11
#define PIN_MQ135 A1 // MQ135 Analog Input Pin (my choice: A1)
//#define DHTPIN 3 // DHT Digital Input Pin (my choice: A3)
MQ135 mq135_sensor(PIN_MQ135);
DHT dht(3, DHT11);
float temp, hum; // Temp and Humid floats, will be measured by the DHT
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
//Various special characters
byte Therm[8] = { 0x04, 0x0A, 0x0A, 0x0E, 0x0E, 0x1F, 0x1F, 0x0E };
byte Humid[8] = { 0x04, 0x04, 0x0A, 0x0A, 0x11, 0x11, 0x11, 0x0E };
byte begin[8] = { 0x1F, 0x1F, 0x0E, 0x04, 0x10, 0x10, 0x1F, 0x00 };
byte arrow[8] = { 0x1F, 0x1F, 0x0E, 0x04, 0x00, 0x00, 0x1F, 0x00 };
byte arrowdot[8] = { 0x1F, 0x1F, 0x0E, 0x04, 0x01, 0x01, 0x1F, 0x00 };
byte linedot[8] = { 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x1F, 0x00 };
byte five[8] = { 0x0E, 0x08, 0x0E, 0x02, 0x0E, 0x00, 0x1F, 0x00 };
byte zero[8] = { 0x0E, 0x0A, 0x0A, 0x0A, 0x0E, 0x00, 0x1F, 0x00 };
void setup() {
Serial.begin(9600); // for DEBUGGING reasons
// Start sensor DHT11
dht.begin();
// Start LCD
lcd.init();
lcd.backlight();
//what follows ... creation of various special characters
lcd.createChar(0, zero);
lcd.createChar(1, Therm);
lcd.createChar(2, Humid);
lcd.createChar(3, arrow);
lcd.createChar(4, begin);
lcd.createChar(5, five);
lcd.createChar(6, linedot);
lcd.createChar(7, arrowdot);
//write line 0 on screen (once for all)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("\xFF""\xFF""\xFF"" Air Quality ""\xFF""v5");
}
void loop() {
//Collect temperature and humidity
float temperature = dht.readTemperature() - 4; //-4 adjustment (probably tolerance of the module)
float humidity = dht.readHumidity() + 10; //+10 adjustment (probably tolerance of the module), have to find a trick to avoid "humidity" to go above 100%
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
//Collect data .... .... ....
float rzero = mq135_sensor.getRZero();
float correctedRZero = mq135_sensor.getCorrectedRZero(temperature, humidity);
float resistance = mq135_sensor.getResistance();
float ppm = mq135_sensor.getPPM();
float correctedPPM = mq135_sensor.getCorrectedPPM(temperature, humidity);
// this line below has to be checked ... might be wrong...
Polluant = correctedPPM / 300; // to check this number
//real life AQI from known sources is always 300x smaller than my calculations. (there must be something I missed... why always "300")
// D E B U G G I N G
Serial.print("MQ135 RZero: ");
Serial.print(rzero);
Serial.print("\t Corr RZero: ");
Serial.print(correctedRZero);
Serial.print("\t Resistance: ");
Serial.print(resistance);
Serial.print("\t PPM: ");
Serial.print(ppm);
Serial.print(" ppm");
Serial.print("\t Corr PPM: ");
Serial.print(correctedPPM, 0);
Serial.print(" ppm");
Serial.print("\t AQI: ");
Serial.print(Polluant);
Serial.print("\t temp: ");
Serial.print(temperature, 0);
Serial.print("\t hum: ");
Serial.println(humidity, 0);
if (Polluant < 50) { // what follows is to display data on the LCD (lines 1 and 2 (line 0 --- see above))
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(1, 1);
lcd.print("\1"
" ");
lcd.print(temperature, 0);
lcd.print("\xDF"
"c");
lcd.setCursor(13, 1);
lcd.print("Good");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(1, 2);
lcd.print("\2"
" ");
lcd.print(humidity, 0);
lcd.print("% AQI ");
lcd.print(Polluant, DEC);
} else if (Polluant >= 50 && Polluant < 100) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(1, 1);
lcd.print("\1"
" ");
lcd.print(temperature, 0);
lcd.print("\xDF"
"c");
lcd.setCursor(10, 1);
lcd.print("Moderate");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(1, 2);
lcd.print("\2"
" ");
lcd.print(humidity, 0);
lcd.print("% AQI ");
lcd.print(Polluant, DEC);
} else if (Polluant >= 100 && Polluant < 200) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(1, 1);
lcd.print("\1"
" ");
lcd.print(temperature, 0);
lcd.print("\xDF"
"c");
lcd.setCursor(13, 1);
lcd.print("Poor");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(1, 2);
lcd.print("\2"
" ");
lcd.print(humidity, 0);
lcd.print("% AQI ");
lcd.print(Polluant, DEC);
} else if (Polluant >= 200 && Polluant < 300) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(1, 1);
lcd.print("\1"
" ");
lcd.print(temperature, 0);
lcd.print("\xDF"
"c");
lcd.setCursor(10, 1);
lcd.print("Unhealthy");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(1, 2);
lcd.print("\2"
" ");
lcd.print(humidity, 0);
lcd.print("% AQI ");
lcd.print(Polluant, DEC);
} else if (Polluant >= 300 && Polluant < 400) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(1, 1);
lcd.print("\1"
" ");
lcd.print(temperature, 0);
lcd.print("\xDF"
"c");
lcd.setCursor(11, 1);
lcd.print("Severe");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(1, 2);
lcd.print("\2"
" ");
lcd.print(humidity, 0);
lcd.print("% AQI ");
lcd.print(Polluant, DEC);
} else if (Polluant >= 400) {
lcd.setCursor(10, 1);
lcd.print("Hazardous");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(1, 2);
lcd.print("\2"
" ");
lcd.print(humidity, 0);
lcd.print("% AQI ");
lcd.print(Polluant, DEC);
}
lcd.setCursor(0, 3); // what follows is the construction of the graphic on line 3 of the LCD... (line 3 is the last line)
lcd.write(0);
lcd.write(6);
lcd.print("_");
lcd.write(6);
lcd.print("___");
lcd.write(6);
lcd.print("___");
lcd.write(6);
lcd.print("___");
lcd.write(6);
lcd.print("_");
lcd.write(5);
lcd.write(0);
lcd.write(0);
if (Polluant < 25) { //what follows: system to make the pointer move left or right on line 3, depending on the value of the polluant
lcd.setCursor(0, 3);
lcd.write(4);
PreviousP = 0;
} else if (Polluant >= 25 && Polluant < 50) {
lcd.setCursor(1, 3);
lcd.write(7);
PreviousP = 1;
}
if (Polluant >= 50 && Polluant < 75) {
lcd.setCursor(2, 3);
lcd.write(3);
PreviousP = 2;
} else if (Polluant >= 75 && Polluant < 100) {
lcd.setCursor(3, 3);
lcd.write(7);
PreviousP = 3;
}
if (Polluant >= 100 && Polluant < 125) {
lcd.setCursor(4, 3);
lcd.write(3);
PreviousP = 4;
} else if (Polluant >= 125 && Polluant < 150) {
lcd.setCursor(5, 3);
lcd.write(3);
PreviousP = 5;
} else if (Polluant >= 150 && Polluant < 175) {
lcd.setCursor(6, 3);
lcd.write(3);
PreviousP = 6;
} else if (Polluant >= 175 && Polluant < 200) {
lcd.setCursor(7, 3);
lcd.write(7);
PreviousP = 7;
}
if (Polluant >= 200 && Polluant < 225) {
lcd.setCursor(8, 3);
lcd.write(3);
PreviousP = 8;
} else if (Polluant >= 225 && Polluant < 250) {
lcd.setCursor(9, 3);
lcd.write(3);
PreviousP = 9;
} else if (Polluant >= 250 && Polluant < 275) {
lcd.setCursor(10, 3);
lcd.write(3);
PreviousP = 10;
} else if (Polluant >= 275 && Polluant < 300) {
lcd.setCursor(11, 3);
lcd.write(7);
PreviousP = 11;
}
if (Polluant >= 300 && Polluant < 325) {
lcd.setCursor(12, 3);
lcd.write(3);
PreviousP = 12;
} else if (Polluant >= 325 && Polluant < 350) {
lcd.setCursor(13, 3);
lcd.write(3);
PreviousP = 13;
} else if (Polluant >= 350 && Polluant < 375) {
lcd.setCursor(14, 3);
lcd.write(3);
PreviousP = 14;
} else if (Polluant >= 375 && Polluant < 400) {
lcd.setCursor(15, 3);
lcd.write(7);
PreviousP = 15;
}
if (Polluant >= 400 && Polluant < 425) {
lcd.setCursor(16, 3);
lcd.write(3);
PreviousP = 16;
} else if (Polluant >= 425 && Polluant < 450) {
lcd.setCursor(17, 3);
lcd.write(3);
PreviousP = 17;
} else if (Polluant >= 450 && Polluant < 475) {
lcd.setCursor(18, 3);
lcd.write(3);
PreviousP = 18;
} else if (Polluant >= 475 && Polluant < 500) {
lcd.setCursor(19, 3);
lcd.write(7);
PreviousP = 19;
}
//below: remembering the past AQI and put a blinker for comparaison
//if (i == 17280) {//17280 cycles is roughly 24h
if (i == 20) { //reduce the time... to be erased and replaced by "if (i == 17280)" when everything works
i = 0;
F = PreviousP;
}
//if (i < 17280) {
if (i < 20) { //reduce the time... to be erased and replaced by "if (i == 17280)" when everything works
i = i + 1;
lcd.setCursor(F, 3);
lcd.blink();
}
delay(5000);
}
I think that the following 2 files might also have a certain role in what I am trying to do…
MQ135.cpp (4.3 KB)
MQ135.h (1.8 KB)
Let's see if the power supply is really the problem:
Connect everything as you have it and also connect A2 to the 3.3V pin.
Run this code when connected to the computer and when connected to the 5V 10W charger.
The output should be in the 670ish range and fairly steady
// Connect the 3.3V pin to analog input A2
const int analogPin = A2;
int val = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
val = analogRead(analogPin);
Serial.println(val);
delay(250);
}
OOPS…
Please later … tomorrow…
I need to help my wife also… she is already moaning that I spent the whole day on my “thing”…
Well I see
Still need help?
BINGO !
Good afternoon everybody…
I think to have found the problem:
To power the setup via my desktop computer, I used a cable equipped with a ferrite.
(below: this is the kind of cable I was using)
and to power the setup with a phone charger, I used another cable, this time without ferrite…
and guess what…
Swapping the cables solved the problem.
Now, I have (more or less) correct readings using the phone charger.
I still have to fiddle a bit to get everything as I want it… but I think the problem is solved.
And… I will scrub the internet a bit more to find why the cable with the ferrite seems to be a better choice.
Here, I wish to thank every forum user who took time to help me…
And if one day, we meet, let me pay for the pizza…
Eric
If you do my test you will find out.
Hello,
I will do the test… no problems… Promised…
(and I will post the result)
Up to you. You don't have to do it if you don't want to. I simply offer it as a way to help debug your problem.