Hay fellas,
I´ve a problem with my code.
i´m trying to read and to log the temperature datas from the DS18B20.
The serial print isnt the problem, but when i try to log the temperature the serial print says that it isn´t possible to open the txt-file.
When i´m logging the temperature from the DS3231 everything is fine. It doesnt work with the DS18B20.
Is there someone who could help me?
Thanks
//========Set the time for the RealTimeClock (RTC) with Set_RTC_Time.ino first==========
//For the RTC and the SD-Card-Connector
#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
//For the DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
File myFile;
DS3231 rtc(SDA, SCL);
int pinCS = 53; // Pin 10 on Arduino Uno
//=====DS18B20======
// Data wire from the DS18B20 is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a OneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass OneWire reference to Dallas Temperature
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
rtc.begin();
//=======DS18B20=======
pinMode(ONE_WIRE_BUS, OUTPUT);
sensors.begin(); //Start up the library
}
void loop() {
Serial.print(rtc.getDateStr());
Serial.print(",");
Serial.print(rtc.getTimeStr());
Serial.print(",");
//======DS18B20=========
sensors.requestTemperatures(); //Call sensor to request the Temperature
Serial.println(String(sensors.getTempCByIndex(0)) + "°C"); //Output the values on the monitor
myFile = SD.open("test_ds13b20.txt", FILE_WRITE);
if (myFile) {
myFile.print(rtc.getDateStr());
myFile.print(",");
myFile.print(rtc.getTimeStr());
myFile.print(",");
sensors.requestTemperatures(); //Call sensor to request the Temperature
myFile.println(String(sensors.getTempCByIndex(0)) + "°C"); //Save the values on the SD-Card
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening txt-file");
}
delay(3000); //Wait for 3 sec
}
//========Set the time for the RealTimeClock (RTC) with Set_RTC_Time.ino first==========
//For the RTC and the SD-Card-Connector
#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
//For the DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
File myFile;
DS3231 rtc(SDA, SCL);
int pinCS = 53; // Pin 10 on Arduino Uno
//=====DS18B20======
// Data wire from the DS18B20 is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a OneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass OneWire reference to Dallas Temperature
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
// SD Card Initialization
if (SD.begin()) Serial.println("SD card is ready to use.");
else Serial.println("SD card initialization failed");
rtc.begin();
//=======DS18B20=======
pinMode(ONE_WIRE_BUS, OUTPUT);
sensors.begin(); //Start up the library
}
void loop() {
Serial.print(rtc.getDateStr());
Serial.print(",");
Serial.print(rtc.getTimeStr());
Serial.print(",");
//======DS18B20=========
sensors.requestTemperatures(); //Call sensor to request the Temperature
String DalasTemp=String(sensors.getTempCByIndex(0)) + "°C";
Serial.println(DalasTemp); //Output the values on the monitor
myFile = SD.open("test_ds13b20.txt", FILE_WRITE);
if (myFile) {
myFile.print(rtc.getDateStr());
myFile.print(",");
myFile.print(rtc.getTimeStr());
myFile.print(",");
myFile.println(DalasTemp); //Save the values on the SD-Card
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening txt-file");
}
delay(3000); //Wait for 3 sec
}
your SD don't work, at all, not even for DS3231, bc
must be: SD.begin(pinCS)
//========Set the time for the RealTimeClock (RTC) with Set_RTC_Time.ino first==========
//For the RTC and the SD-Card-Connector
#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
//For the DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
DS3231 rtc(SDA, SCL);
int pinCS = 53; // Pin 10 on Arduino Uno
//=====DS18B20======
// Data wire from the DS18B20 is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a OneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass OneWire reference to Dallas Temperature
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
// SD Card Initialization
if (SD.begin(pinCS)) Serial.println("SD card is ready to use.");
else Serial.println("SD card initialization failed");
rtc.begin();
//=======DS18B20=======
pinMode(ONE_WIRE_BUS, OUTPUT);
sensors.begin(); //Start up the library
}
void loop() {
//======DS18B20=========
sensors.requestTemperatures(); //Call sensor to request the Temperature
char Log[40];
sprintf(Log, "%s,%s,%.01f°C", rtc.getDateStr(), rtc.getTimeStr(), sensors.getTempCByIndex(0));
Serial.println(Log); //Output the values on the monitor
File myFile = SD.open("test_ds13b20.txt", FILE_WRITE);
if (myFile) {
myFile.println(Log); //Save the values on the SD-Card
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening txt-file");
}
delay(3000); //Wait for 3 sec
}
See the simulated project here.
I added a routine to read and print data recorded on SD cards (readSD()).
//========Set the time for the RealTimeClock (RTC) with Set_RTC_Time.ino first==========
//For the RTC and the SD-Card-Connector
#include <SD.h>
#include <SPI.h>
#include "DS3231.h"
//For the DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
File myFile;
DS3231 rtc(SDA, SCL);
int pinCS = 53; // Pin 10 on Arduino Uno
//=====DS18B20======
// Data wire from the DS18B20 is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a OneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass OneWire reference to Dallas Temperature
DallasTemperature sensors(&oneWire);
//-------------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
if (SD.begin()) { // SD Card Initialization
Serial.println("SD card is ready to use.");
}
else {
Serial.println("SD card initialization failed");
return;
}
rtc.begin();
//=======DS18B20=======
pinMode(ONE_WIRE_BUS, OUTPUT);
sensors.begin(); //Start up the library
}
//---------------------------------------------------------------
void loop() {
Serial.print(rtc.getDateStr());
Serial.print(",");
Serial.print(rtc.getTimeStr());
Serial.print(",");
//======DS18B20=========
sensors.requestTemperatures(); //Call sensor to request the Temperature
Serial.println(String(sensors.getTempCByIndex(0)) + "°C"); //Output the values on the monitor
myFile = SD.open("test_ds.txt", FILE_WRITE);
if (myFile) {
myFile.print(rtc.getDateStr());
myFile.print(",");
myFile.print(rtc.getTimeStr());
myFile.print(",");
sensors.requestTemperatures(); //Call sensor to request the Temperature
myFile.println(String(sensors.getTempCByIndex(0)) + "°C"); //Save the values on the SD-Card
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening txt-file");
}
delay(3000); //Wait for 3 sec
readSD();
}
//----------------------------------------------------------
void readSD() {
myFile = SD.open("test_ds.txt", O_READ); //If no exist, create file
if (myFile) {
Serial.println("test_ds.txt listing");
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test_ds.txt");
}
}