Writing Temp from DS18B20 sensor to SD

Hello all,

I am pretty new to the Arduino world, and have inheritted a larger share of this project than i originally thought.

I built a solar collector and heat exchanger for an aquaponics system and now I am trying to use my Uno to record the temperatures of the fluid streams and write them to my SD card so i can analyze the data later in excel to find the energy i was able to harvest.

I have an Adafruit datalogger shield, as well as 6 DS18B20 digital sensors. I am trying to take readings at 10-15 minute intervals. I have been trying to synthesize information from the resources below into a single script that will do what I want it to do and not getting too far.

Do you have any insight into how i should go about this? Each of the resources seem to accomplish the tasks in slightly different ways, and most go to the serial monitor and not the SD.

I have my probes working using the onewire library's ds18b20 example code, with all 6 of my probes are on the same digital pin (2). They are reading temps and spitting them into the serial monitor pefect, for what it is worth.

OK, sounds good so far.

You can treat the SD card like a Serial port. Just print stuff to it. Usually you want to print it into a format that will make analysis easier later. CSV format is popular because it's easy to open in Excel.

MorganS:
OK, sounds good so far.

You can treat the SD card like a Serial port. Just print stuff to it. Usually you want to print it into a format that will make analysis easier later. CSV format is popular because it's easy to open in Excel.

This is literally my first arduino project, and i have next-to-zero programming experience. How do you change the code properly to print the information like that?

Reading through most of these resources I can get a rough idea of what maybe 1/3 of what any of it means, tbh.

this link is more than enough to put u on the right way

I agree, except that it says "print what you want" which is not helpful. And that is the part that I really need the help with.

I have adapted the instractables, but the sd writing part is killing me.

edit: ok, after reviewing the sample codes files at the end, I think I have this sorted out.

controls.ino (8.1 KB)

@OP

1. This is a setup of a typical Data Logger using 2xDs18B20 sensors and SD Card. In this system, the data logging is stopped when the Button K1 is pressed.
ds18B20-2SD.png

Figure-1: Connection diagram among 2xDS18B20, SD Card, Button, and UNO.

2. Upload the following sketch (untested).

//12-bit default resolution; external power supply
#include<OneWire.h>
#include<SD.h>
#include<SPI.h>

File myFile;
char filename[] = "Data.CSV";
const int chipSelect = 4;
#define OneWireBusPin 2

float Temp01, Temp02;

OneWire ds(OneWireBusPin);  //2
byte addr1[8];         //to hold 64-bit ROM Codes of DS1
byte addr2[8];        //to ho;d 64-bit ROM-code DS2
byte data[9];        //buffer to hold data coming from DS18B20
float celsius;

void setup() 
{
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  pinMode(8, INPUT_PULLUP);
  
  //---initialize SD Card---------
  if (!SD.begin(4)) 
  {
    Serial.print("sd card init failed!");
    return;
  }
  Serial.println("SD Card init. OK!");
  
 // myFile = SD.open(filename, FILE_WRITE);
  SD.remove(filename);
  //----------------------------
  
  ds.reset();
  ds.search(addr1);  //collect 64-bit ROM code from sensor (DS1)
  ds.search(addr2); //auto collec of ROM address of DS-2
  
  Serial.print("Address of DS-1: ");
  for (int i=0; i<8; i++)
  {
    if(addr1[i] < 0x10)
    {
        Serial.print('0');
    }
    Serial.print(addr1[i], HEX);   //show on I2C LCD 
  }
  Serial.println();

  Serial.print("Address of DS-1: ");
   for (int i=0; i<8; i++)
  {
    if(addr2[i] < 0x10)
    {
        Serial.print('0');
    }
    Serial.print(addr2[i], HEX);
  }
  Serial.println();
}

void loop()
{
  probe1();
  probe2();
  recordSDCard();
  if(digitalRead(8) == LOW)
  {
  //  myFile.close();
    readFromSDCard();
    HERE: goto HERE;
  }
  delay(1000);
}

void probe1()
{
 //----------------------------
 ds.reset();       //bring 1-Wire into idle state
 ds.select(addr1); //slect with DS-1 with address addr1
 ds.write(0x44);    //conversion command
 delay(1000);   //data ready withinh DS18B20 or poll status word
 //---------------------------

 ds.reset();
 ds.select(addr1);  //selectimg the desired DS18B20
 ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
 ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[8]
 //---------------------------------

  int16_t raw = (data[1] << 8) | data[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
  celsius = (float)raw / 16.0;  //12-bit resolution
  Temp01 = celsius;
  Serial.print(celsius);
  Serial.println("  DS-1");
}


void probe2()
{
 ds.reset();       //bring 1-Wire into idle state
 ds.select(addr2); //slect with DS-1 with address addr1
 ds.write(0x44);    //conversion command
 delay(1000);   //data ready withinh DS18B20 or poll status word
 //---------------------------

 ds.reset();
 ds.select(addr2);  //selectimg the desired DS18B20
 ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
 ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[8]
  //---------------------------------

  int16_t raw = (data[1] << 8) | data[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
  celsius = (float)raw / 16.0;  //12-bit resolution
  Temp02 = celsius;
  Serial.print(celsius);
  Serial.println("  DS-2");
 }

void recordSDCard()
{
  myFile = SD.open(filename, FILE_WRITE);
  if(myFile)
  {
    myFile.print(Temp01);
    myFile.print(",");
    myFile.println(Temp02);
    myFile.close();
    Serial.println("//----------------");
    Serial.println("Data stored in SD Card! ");
    Serial.println("//----------------");   
  }
  else
  {
    Serial.println("File can't be opened!");  
  }
}

void readFromSDCard()
{
  myFile = SD.open(filename, FILE_READ);
  if(myFile)
  {
      while(myFile.available())
      {
        char x = (char)myFile.read();
        Serial.print(x);
      }
      myFile.close();   
   }
   else
   {
      Serial.println("File can't be opened!");
   }
}

3. Please, report the result.

4. Modify program to suit your needs.

5. Any question/query is welcomed.

ds18B20-2SD.png