Arduino air quality display and storage project

Hi everyone, I am new in Arduino project but want to make air quality monitoring for my office. In this project I am using Arduino mega, OLED display, DS1307 RTC, DHT11 sensor, MQ sensor and one SD card module. I am using the below code and able to display time, temperature, humidity, air quality percentage. But problem is here that SD card stops storage after 4MB of data. Again when I format the SD card it's start storage but stop after 4MB of data storage and serial monitor show ""error opening datalog.txt". Would you please support how to solve the issue.
:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeMonoOblique9pt7b.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC

#define OLED_RESET 3 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define sensor A0
#define DHTPIN 2 // Digital pin 2
#define DHTTYPE DHT11 // DHT 11

int gasLevel = 0; //int variable for gas level
String quality ="";
DHT dht(DHTPIN, DHTTYPE);

//Logger
const int chipSelect = 53;

// Create a file to store the data
File myFile;

// RTC
RTC_DS1307 rtc;

void setup() {
Serial.begin(9600);
pinMode(sensor,INPUT);
dht.begin();

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
}
display.clearDisplay();
display.setTextColor(WHITE);

display.setTextSize(2);
display.setCursor(50, 0);
display.println("Air");
display.setTextSize(1);
display.setCursor(23, 20);
display.println("Qulaity monitor");
display.display();
delay(1200);
display.clearDisplay();

display.setTextSize(2);
display.setCursor(20, 20);
display.println("BY Kazi");
display.display();
delay(1000);
display.clearDisplay();
//logger

// setup for the RTC
while(!Serial); // for Leonardo/Micro/Zero
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
//while (1);
}
//else {
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(DATE), F(TIME)));
//}
if(! rtc.isrunning()) {
Serial.println(F("RTC is NOT running!"));
}

// setup for the SD card
Serial.print(F("Initializing SD card..."));

if(!SD.begin(chipSelect)) {
Serial.println(F("initialization failed!"));
return;
}
Serial.println(F("initialization done."));

//open file
myFile=SD.open("DATALOG.txt", FILE_WRITE);

// if the file opened ok, write to it:
if (myFile) {
Serial.println(F("File opened ok"));
// print the headings for our data
myFile.println(F("Date,Time,Temperature ºC"));
}
myFile.close();

}

void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
display.setTextColor(WHITE);
display.setTextSize(1);
display.setFont();
display.setCursor(0, 43);
display.println("Temp :");
display.setCursor(80, 43);
display.println(t);
display.setCursor(114, 43);
display.println("C");
display.setCursor(0, 56);
display.println("RH :");
display.setCursor(80, 56);
display.println(h);
display.setCursor(114, 56);
display.println("%");
//logger
//debugging purposes
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.println(" *C");
//Serial.print(f);
//Serial.println(" *F\t");
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.println(F(" %"));
Serial.print(gasLevel);
Serial.println(F(" %"));

myFile = SD.open("DATALOG.txt", FILE_WRITE);
if (myFile) {
Serial.println(F("open with success"));
display.setCursor(105,23);
display.println("ok");
myFile.print(t);
myFile.println(",");
myFile.print(h);
myFile.println(",");
myFile.print(gasLevel);
myFile.close();
delay(2000);
}
else {
Serial.println(F("error opening datalog.txt"));
display.setCursor(105,23);
display.println("Nok");
}
}

void loggingTime() {
DateTime now = rtc.now();
myFile = SD.open("DATALOG.txt", FILE_WRITE);
if (myFile) {
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(',');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(",");
}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.println(now.day(), DEC);
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
myFile.close();
delay(1000);
}

void air_sensor()
{
DateTime now = rtc.now();
gasLevel = analogRead(sensor);

if(gasLevel<181){
quality = " GOOD!";
}
else if (gasLevel >181 && gasLevel<225){
quality = " Poor!";
}
else if (gasLevel >225 && gasLevel<300){
quality = "Very bad!";
}
else if (gasLevel >300 && gasLevel<350){
quality = "ur dead!";
}
else{
quality = " Toxic";
}

display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(1,5);
display.setFont();
display.println("Date:");
display.setCursor(35,5);
display.println(now.day(), DEC);
display.setCursor(45,5);
display.println('/');
display.setCursor(50,5);
display.println(now.month(), DEC);
display.setCursor(55,5);
display.println('/');
display.setCursor(60,5);
display.println(now.year(), DEC);
display.setCursor(90,5);
display.println(now.hour(), DEC);
display.setCursor(100,5);
display.println(":");
display.setCursor(105,5);
display.println(now.minute(), DEC);
display.setTextSize(1);
display.setCursor(1,23);
display.println("Air Quality:");
display.setCursor(70,23);
display.setFont(&FreeMonoOblique9pt7b);
display.println(gasLevel);
}

void loop() {
display.clearDisplay();
air_sensor();
sendSensor();
loggingTime();
display.display();
}

I moved your topic to an appropriate forum category @atiqul_m_haque123.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I think dat 4GB is the limit of the SD library. You can use the SdFat library (I think it goes to 32GB, I'm using it with a 16GB card).

Please edit your post, select all code and click the <CODE/> button in the composer Window; next save your post.

1 Like

Thanks a lot, really appreciate your support. As per your guidance I used SDfat, now storage continued after 4MB. Thanks arduino forum :+1:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.