#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
//#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's
#define co2_pin 2
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
delay(250); // wait for the OLED to power up
display.begin(i2c_Address, true); // Address 0x3C default
//display.setContrast (0); // dim display
display.display();
delay(2000);
// Clear the buffer.
display.clearDisplay();
}
void loop() {
int rawValue = analogRead(co2_pin);
float voltaje = rawValue * (5.0 / 1023.0);
int ppm = 500 * (voltaje / 4.5);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0,0);
display.print("Cantidad de CO2: ");
display.print(ppm);
display.println(" ppm");
if (ppm<=1000){
display.setTextSize(1,1);
display.setCursor(0,40);
display.print("Buena Calidad de Aire");
display.setTextSize(1,1);
display.setCursor(0,50);
display.print(" ");
}
else if (ppm>=1000 && ppm<=1500){
display.setTextSize(1,1);
display.setCursor(0,40);
display.print("Calidad de Aire \nModerada");
}
else {
display.setTextSize(1,1);
display.setCursor(0,40);
display.print("Mala Calidad de Aire");
}
display.display();
Serial.print("CO2: ");
Serial.print(ppm);
Serial.println(" ppm");
delay(2000);
}