good day everyone
ive been struggling using making an datalogger using ds1307 and sd card module. the problem is that when im using the ds18b20 sensors only, it works perfectly fine, but incorporating it with other sensors such as 2 dht22, the text file cannot be open based on the serial monitor's output. thanks in advance
here is the code for the program that i used.
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "RTClib.h"
#include <DHT.h>
#define ONE_WIRE_BUS_PIN 5
#define DHTPIN1 3
#define DHTPIN2 6
#define DHTTYPE1 DHT22 // DHT 22 (AM2302)
#define DHTTYPE2 DHT22 // DHT 22 (AM2302)
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif
OneWire oneWire(ONE_WIRE_BUS_PIN);
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
File myFile;
int pinCS = 10; // Pin 10 on Arduino Uno
int chk;
float hum1; //Stores humidity value
float temp1; //Stores temperature value
float hum2; //Stores humidity value
float temp2; //Stores temperature value
DeviceAddress Probe01 = { 0x28, 0xFF, 0xBC, 0x3F, 0x71, 0x17, 0x03, 0xD0};
DeviceAddress Probe02 = { 0x28, 0xFF, 0x49, 0x42, 0x71, 0x17, 0x03, 0x91 };
DeviceAddress Probe03 = { 0x28, 0xFF, 0xE5, 0x8C, 0x83, 0x16, 0x03, 0xA9 };
DeviceAddress Probe04 = { 0x28, 0xFF, 0x79, 0x1D, 0x71, 0x17, 0x03, 0x57 };
DeviceAddress Probe05 = { 0x28, 0xFF, 0xB9, 0x3C, 0x71, 0x17, 0x03, 0x8A };
DeviceAddress Probe06 = { 0x28, 0xFF, 0x24, 0x19, 0xA0, 0x16, 0x05, 0x8B };
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
// SD Card Initialization
if (SD.begin())
{ sensors.begin();
dht1.begin();
dht2.begin();
rtc.begin();
Serial.println("SD card is ready to use.");
// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);
sensors.setResolution(Probe03, 10);
sensors.setResolution(Probe04, 10);
sensors.setResolution(Probe05, 10);
sensors.setResolution(Probe06, 10);
} else
{
Serial.println("SD card initialization failed");
return;
}
}
void loop() {
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
DateTime now = rtc.now();
sensors.requestTemperatures();
delay(2000);
//Read data and store it to variables hum and temp
hum1 = dht1.readHumidity();
temp1= dht1.readTemperature();
hum2 = dht2.readHumidity();
temp2= dht2.readTemperature();
// if the file opened okay, write to it:
if (myFile) {
myFile.println(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(" (");
myFile.print(daysOfTheWeek[now.dayOfTheWeek()]);
myFile.print(") ");
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
Serial.print("Probe 01 temperature is: ");
printTemperature(Probe01);
Serial.println();
Serial.print("Probe 02 temperature is: ");
printTemperature(Probe02);
Serial.println();
Serial.print("Probe 03 temperature is: ");
printTemperature(Probe03);
Serial.println();
Serial.print("Probe 04 temperature is: ");
printTemperature(Probe04);
Serial.println();
Serial.print("Probe 05 temperature is: ");
printTemperature(Probe05);
Serial.println();
Serial.print("Probe 06 temperature is: ");
printTemperature(Probe06);
Serial.println();
myFile.print("Humidity1: ");
myFile.print(hum1);
myFile.print(" %, Temp1: ");
myFile.print(temp1);
myFile.print(" Celsius");
myFile.print("Humidity2: ");
myFile.print(hum2);
myFile.print(" %, Temp2: ");
myFile.print(temp2);
myFile.print(" Celsius");
myFile.println();
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
// Reading the file
delay(5000);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
Serial.print("Error getting temperature ");
}
else
{
Serial.print(tempC);
Serial.print(",");
myFile.print(tempC);
myFile.print(",");
}
}