I recently ordered the components
- Arduino Nano
- DHT20 (temperature and humidity sensor)
- MH-Z19C (CO2 sensor)
- SH1106 (128x64 OLED Display)
And after arriving I want to connect the parts like in the picture attached.
However, I am very uncertain about my first code to get the project running.
I basically just copied text from similar projects with different combinations of sensors and can neighter tell if the pins in the code fit to the wiring in the picture, nor do I know if the code is complete to achieve the goal: The display showing the temperature in the first line, the humidity in the second and the co2 content in the third.
For instance, I was wondering that not every pin was defined or has the code "int" in front of it in the other projects I copied code from.
I would be very glad if someone could look over the code. I tried to structure and comment it well.
// CO2
#include <Arduino.h>
#include <MHZ19.h>
#include <SoftwareSerial.h>
// Temp
#include <DFRobot_DHT20.h>
// Display
#include <Adafruit_SH1106.h>
#include <Wire.h>
// CO2
#define RX_PIN 5 // Rx pin which the MHZ19 Tx pin is attached to D2
#define TX_PIN 6 // Tx pin which the MHZ19 Rx pin is attached to D3
#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed)
MHZ19 myMHZ19; // Constructor for library
SoftwareSerial mySerial(RX_PIN, TX_PIN); // (Uno example) create device to MH-Z19 serial
// Temperature
DFRobot_DHT20 dht20;
// Display
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SH1106 display(OLED_RESET);
void setup()
{
Serial.begin(9600);
// Start Co2
mySerial.begin(BAUDRATE); // (Uno example) device to MH-Z19 serial start
myMHZ19.begin(mySerial); // *Serial(Stream) refence must be passed to library begin().
myMHZ19.autoCalibration(); // Turn auto calibration ON (OFF autoCalibration(false))
// Start Temp
while (dht20.begin()) {
Serial.println("Initialize sensor failed");
delay(1000);
}
// Start Display
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
}
void loop()
{
//Get CO2
int CO2;
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm)
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(dht20.getTemperature());
display.print('\n');
display.print(dht20.getHumidity());
display.print("%");
display.print('\n');
display.print(CO2);
display.print(" ppm");
display.display();
delay(2000);
}