Go Down

Topic: Simple Datalogger and garbage in data (Read 951 times) previous topic - next topic


I have cobbled together a simple dataloggger to log temperature data from the TMP36 sensor that came with my Sparkfun kit. I finally got ot to log data to an SDcard however I get garbage every so often. Seems like I am lossing communications sporaticly to the card or something like that.This is what the ouput on the card look like, sorry for the wide table but that is how the files looks...
Amb 66.8 probe 65.9
Amb 67.7 probe 64.2
Amb 67.7 probe 66.8
Amb 67.7 probe 6?ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿAmb 67.7 probe 66.8
Amb 66.8 probe 64.2
Amb 66.8 probe 63.3
Amb 67.7 probe 64.2
Amb 68.6 probe 65.9
Amb.?ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿâe 62.4
Amb 66.8 probe 65.0
Amb 65.9 probe 64.2
Amb 60 ?ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ·.7 probe 63.3

Here is my code
Code: [Select]

#include <SD.h>
// We'll use analog input 0 to measure the temperature sensor's
// signal pin.
const int chipSelect = 4;
const int temperature0Pin = 0;
const int temperature1Pin = 1;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

void setup()
  // In this sketch, we'll use the Arduino's serial port
  // to send text back to the main computer. For both sides to
  // communicate properly, they need to be set to the same speed.
  // We use the Serial.begin() function to initialize the port
  // and set the communications speed.
  // The speed is measured in bits per second, also known as
  // "baud rate". 9600 is a very commonly used baud rate,
  // and will transfer about 10 characters per second.
// Open serial communications and wait for port to open:

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);
  card.init();               //Initialize the SD card and configure the I/O pins.
  volume.init(card);          //Initialize a volume on the SD card.
  root.openRoot(volume);      //Open the root directory in the volume
  // 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:
  Serial.println("card initialized.");

void loop()
// Up to now we've only used integer ("int") values in our
// sketches. Integers are always whole numbers (0, 1, 23, etc.).
// In this sketch, we'll use floating-point values ("float").
// Floats can be fractional numbers such as 1.42, 2523.43121, etc.
// We'll declare three floating-point variables
// (We can declare multiple variables of the same type on one line:)
float voltage0, degrees0C, degrees0F, voltage1, degrees1C, degrees1F;
// First we'll measure the voltage at the analog pin. Normally
// we'd use analogRead(), which returns a number from 0 to 1023.
// Here we've written a function (further down) called
// getVoltage() that returns the true voltage (0 to 5 Volts)
// present on an analog input pin.
voltage0 = analogRead(temperature0Pin) * 0.004882814;
voltage1 = analogRead(temperature1Pin) * 0.004882814;
// Now we'll convert the voltage to degrees Celsius.
// This formula comes from the temperature sensor datasheet:
degrees0C = (voltage0 - 0.5) * 100.0;
degrees1C = (voltage1 - 0.5) * 100.0;

// While we're at it, let's convert degrees Celsius to Fahrenheit.
// This is the classic C to F conversion formula:
degrees0F = degrees0C * (9.0/5.0) + 32.0;
degrees1F = degrees1C * (9.0/5.0) + 32.0;

// Now we'll use the serial port to print these values
// to the serial monitor!
// To open the serial monitor window, upload your code,
// then click the "magnifying glass" button at the right edge
// of the Arduino IDE toolbar. The serial monitor window
// will open.
// (NOTE: remember we said that the communication speed
// must be the same on both sides. Ensure that the baud rate
// control at the bottom of the window is set to 9600. If it
// isn't, change it to 9600.)
// Also note that every time you upload a new sketch to the
// Arduino, the serial monitor window will close. It does this
// because the serial port is also used to upload code!
// When the upload is complete, you can re-open the serial
// monitor window.
// To send data from the Arduino to the serial monitor window,
// we use the Serial.print() function. You can print variables
// or text (within quotes).
  //Serial.print("voltage0: ");
  //Serial.print(" deg C: ");
  Serial.print(" deg F0: ");
  //Serial.print(" voltage1: ");
  //Serial.print(" deg C1: ");
  Serial.print(" deg F1: ");

File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
  dataFile.print("Amb ");
  dataFile.print(" probe ");
   Serial.print("datalog.txt ");
   Serial.println("Couldn't open file");

delay(5000); // repeat once per second (change as you wish!)
//float getVoltage(int pin)
// This function has one input parameter, the analog pin number
// to read. You might notice that this function does not have
// "void" in front of it; this is because it returns a floating-
// point value, which is the true voltage on that pin (0 to 5V).
// You can write your own functions that take in parameters
// and return values. Here's how:
// To take in parameters, put their type and name in the
// parenthesis after the function name (see above). You can
// have multiple parameters, separated with commas.
// To return a value, put the type BEFORE the function name
// (see "float", above), and use a return() statement in your code
// to actually return the value (see below).
// If you don't need to get any parameters, you can just put
// "()" after the function name.
// If you don't need to return a value, just write "void" before
// the function name.
// Here's the return statement for this function. We're doing
// all the math we need to do within this statement:
//return (analogRead(pin) * 0.004882814);
// This equation converts the 0 to 1023 value that analogRead()
// returns, into a 0.0 to 5.0 value that is the true voltage
// being read at that pin.
// Other things to try with this code:
// Turn on an LED if the temperature is above or below a value.
// Read that threshold value from a potentiometer - now you've
// created a thermostat!

Any ideas?


Now I am really lost, after surfing but finding nothing htat would seem to help, the sketch seems to working fine. I changed nothing other than fighting my computer as to whether or not it would acknowlege the SDcard at all. After restarting the computer and ejecting the card all is well.

Go Up