How to write code for DS18B20, RTC, and SD module for Uno

I'm trying to take the temperature using the DS18B20 sensor, and then record that along with the time onto a micro SD card. However the code will not compile and I keep getting a range of different error codes. The code seemed pretty good to me, but I'm a beginner so I guess not. Any ideas or anyone have code that works for these sensors?

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h> 
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);
RTC_DS3231 rtc;
File myFile;


File myFile;

void setup() {
  
  Serial.begin(9600);
    delay(3000); 
  sensors.begin(); 

  while (!Serial) {
    ; 
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

    if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
  }
 
  myFile = SD.open("today.txt", FILE_WRITE);

 
  if (myFile) {
    Serial.print("Writing to today.txt...");

    myFile.print("Year/Month/Day");
    myFile.print(",");
    myFile.print("Hour");
    myFile.print(':');
    myFile.print("Minute");
    myFile.print(",");
    myFile.print("Temperature")
    myFile.close();
    
  } 

}

void loop() {
  DateTime now = rtc.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(",");
  Serial.print(now.hour(),DEC);
  Serial.print(':')
  Serial.print(now.minute(), DEC);
  Serial.print(':')
  Serial.print(now.second(), DEC);
  Serial.println();

  Serial.println("done");

    Serial.print(" Requesting temperatures..."); 
 sensors.requestTemperatures(); 
 Serial.println("DONE"); 

 Serial.print("Temperature is: "); 
 Serial.println(sensors.getTempCByIndex(0));

   myFile = SD.open("today.txt", FILE_WRITE);

if (myFile) {
  Serial.print("Writing to today.txt...");
  
   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.println(sensors.getTempCByIndex(0));
  myFile.close();}
delay(3000);

}

The best way to develop code is making code running the different parts of the project, one at the time.
Assuming You're a clever programmer I expect You made code for just reading the DS18B20 sensor. When that worked You turned to making code for writing to the SD card. After that You've got experience and will integrate those two parts into a final code.
Where in this path does it fail?

You missed to post the error code. Many helpers don't use those libraries and can't generate the error You claim You get.

Given where you are now - comment out sections until it does compile , then you will know where the errors lie.

there is also another error in the libraries but I would suggest to follow the advice you have got

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