Go Down

Topic: Saving data from temperature sensor on microSD card (Read 1 time) previous topic - next topic

drejcek

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 :) please help me!

Here is code:

Code: [Select]

#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);
}

johnwasser

The problem is here:
Code: [Select]

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

    lsb = Wire.receive();


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

Code: [Select]

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


or

Code: [Select]

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

drejcek

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:

Code: [Select]


#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!

johnwasser

You still have the syntax wrong.

You wrote:
Code: [Select]

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

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


What I wrote was:
Code: [Select]

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

    msb = Wire.receive();

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

    lsb = Wire.receive();

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

drejcek

Hello!


Sorry, I feel so ..... :D 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!

Go Up