Saving data from temperature sensor on microSD card

Hey everyone!

I am new here and new with using Arduino. Anyway, I am using 12-Bit + Sign Temperature Sensor and Thermal Window Comparator with Two-Wire Interface (http://www.national.com/mpf/LM/LM92.html#Overview), Arduino Uno with Ethernet Shield with microSD slot attached.

First I wanted just read data from sensor and then show results on Serial monitor. With that I didn't have no problem. I think results are good. But then I wanted to save this data on microSD card via Ethernet Shield.

I used one of the example on Arduino home page wich shows how to log data from three analog sensors to an SD card using the SD library. When I run program and then I see, that temperature data are not right.

If anybody knows where is problem or problems :slight_smile: please help me!

Here is code:

#include <SD.h>
#include <Wire.h>

const int chipSelect = 4;
int msb,lsb;


void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";
  Wire.requestFrom(0x4b, 2);
  while(Wire.available())

  
  msb = Wire.receive();
  lsb = Wire.receive();
  float temp = (msb<<8|lsb);
  int a =(temp*50)/6400;
  dataString += String(a);
  dataString += ",";
      

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.print("temperature = ");
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
  delay(500);
}

The problem is here:

    while(Wire.available())
        msb = Wire.receive();

    lsb = Wire.receive();

You want to wait for TWO characters and then read them:

    while(Wire.available() == 0)
        /* WAITING FOR A CHARACTER */;
    msb = Wire.receive();
    while(Wire.available() == 0)
        /* WAITING FOR A CHARACTER */;
    lsb = Wire.receive();

or

    while(Wire.available() < 2)
        /* WAITING FOR TWO CHARACTERS */;
    msb = Wire.receive();
    lsb = Wire.receive();

Hey hey!

First I would like to say thank you very much for your reply. I really don't know who to ask about it.

I tried like you suggest. I changed mine code with your suggestions. I tried both your suggestions but in both cases is the same result. On monitor serial I see output results 0. Why is that so??

The code is:

#include <SD.h>
#include <Wire.h>

int msb,lsb;
float tempt;
int a;
int dataString;


const int chipSelect = 4;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  Wire.requestFrom(0x4b, 2);
  while(Wire.available() == 0)
  
 
  
  
  msb = Wire.receive();
  while(Wire.available() == 0)
  lsb = Wire.receive();
  
  float temp = (msb<<8|lsb);
  int a =(temp*50)/6400;

   // make a string for assembling the data to log:
  String dataString = "temperature= ";
  // read three sensors and append to the string:
    dataString += String(a);
      dataString += ""; 
 
  

  File dataFile = SD.open("datalog.txt", FILE_WRITE); 
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
  delay(500);
}

Please help me! I really want to solve this problem!

You still have the syntax wrong.

You wrote:

    while(Wire.available() == 0)
        msb = Wire.receive();

    while(Wire.available() == 0)
        lsb = Wire.receive();

What I wrote was:

    while(Wire.available() == 0)
        ;

    msb = Wire.receive();

    while(Wire.available() == 0)
        ;

    lsb = Wire.receive();

Hello!

Sorry, I feel so ..... :smiley: I am beginner with Arduino and I forget on this details. I repair code and is working great! It shows me the value in whole numbers, but I also want to see the decimal value. Is this because of function int??

Thanks a lot for your help and time! I appreciate your help!

I got the same shield, how did you get the SD lib to compile? Or which version do you use? Tried 2 versions and all i get is 'SD.h:26: error: expected class-name before '{' token'.

Hey!

I used the second option proposed by Johnwasser. At the end it was only problem because I forget to add ";" at the end of the line. After that it works fine for me. Now I wanted to see also decimal value. I need to make some more studies about it.

I don't know much about your code, but I think you have problem in 26 line and maybe is "{" missing or is too much.

Greetings,

Andrej

This should print out the temperature to three decimal places:

  int temperature = msb<<8 | lsb;

  // make a string for assembling the data to log:
  String dataString = "temperature= ";
  // read three sensors and append to the string:
    dataString += String(temperature/128);
    temperature %= 128;  //  Strip off the integer degrees
    dataString += ".";  // Decimal point 
    for (int i=0; i<3; i++)
       {
       temperature *= 10;  // "shift" the number one digit to the left
       dataString += "0" + (temperature/128);  // Add that one digit to the string
       temperature %= 128;  // Strip off that digit
       }

   dataString += "";

Hey!

Thank you a lot johnwasser. Your information was very usefull! Thanks again!

to drejcek
I'm a student and I wanted to ask if you can publish the project of 'interface between LM92 and ARDUINO UNO board.
I ask this because my professor asked me this task.
If you do me this favor, I would be very grateful.
Thanks