I'm trying to read temperature and humidity (T&H) values with a DHT22 sensor, then store those values in a .txt file with a SD card module (HiLetGo Micro SD TF). It's worth noting the Read/ Write sample code works fine. Also, I'm using the and Arduino Uno R3. Here are the issues:
-
When the code is adjusted to print T&H readings to the .txt file, nothing is saving on the .txt file.
-
There are instances where string values are succesffuly printed and saved to the .txt file but sensor readings are not.
-
The myfile.read(); function is not working for any instance besides in the sample code.
-
There are "remnants" of serial monitor outputs with new sketches uploaded to the Arduino Uno
I would greatly appreciate possible solutions to the issues mentioned above. Thank you!
Here is my code:
#include <SPI.h>
#include <SD.h>
#include <DHT.h>
#define DHTPIN 9
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
File myfile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Initializing SD card...");
if (!SD.begin(10))
{
Serial.println("initialization failed!");
while (1);
}
Serial.println("Successful Initialization");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myfile = SD.open("test01.txt", FILE_WRITE);
dht.begin();
if (myfile)
{
Serial.println("Successfully opened test");
Serial.println("");
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
Serial.println("Void loop entered");
myfile.println("Humidity - Temeperature(°F) - Heat Index(°F)");
myfile.println("");
for (int i = 0; i <= 4; i++)
{
// If the file opened okay, write to it:
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(f))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
else
{
//Write to file
myfile.print(h);
myfile.print("%, ");
myfile.print(f);
myfile.print(", ");
myfile.print(hif);
myfile.println("");
//Print to serial, expected values
Serial.print(h);
Serial.print("%, ");
Serial.print(f);
Serial.print(", ");
Serial.print(hif);
Serial.println("");
}
}
Serial.println("Readings succesfully backed up.");
Serial.println("Data points from file:");
while (myfile.available())
{
Serial.write(myfile.read());
Serial.println("Check: Read func");
}
Serial.println("");
// close the file:
myfile.flush();
myfile.close();
Serial.print("File closed");
for (;;) {}
}
Here is the serial monitor:
22:48:18.368 -> Initializing SD card...
22:48:18.403 -> Successful Initialization
22:48:18.403 -> Successfully opened test
22:48:18.437 ->
22:48:18.437 -> Entered void loop
22:48:18.472 -> 27.10%, 81.68, 80.05
22:48:18.472 -> 27.10%, 81.68, 80.05
22:48:18.507 -> 27.10%, 81.68, 80.05
22:48:18.541 -> 27.10%, 81.68, 80.05
22:48:18.541 -> 27.10%, 81.68, 80.05
22:48:18.575 -> Readings succesfully backed up.
22:48:18.609 -> Data points from file:
22:48:18.643 ->
22:48:18.643 -> File closed