Hi,
I have been trying to get a data logging program to work. First I was able to finally get the code to communicate with the SD and make a file with data which I do not know from where the data came.
There are two base code examples I have been working to try and utilise which I combined. If I remove all SD parts the code prints to serial port the temperatures quite well. I tried posting the seperate original code but file to large.
Lastly is my attempt to combine the code to take the daisy chained temperature sensors and log to an SD card which is not quite there yet!
/* YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
Connections:
DS18B20 Pinout (Left to Right, pins down, flat side toward you)
- Left = Ground
- Center = Signal (Pin 2): (with 3.3K to 4.7K resistor to +5 or 3.3 )
- Right = +5 or +3.3 V
Questions: terry@yourduino.com
V1.01 01/17/2013 ...based on examples from Rik Kretzinger
/*-----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>
//Get DallasTemperature Library here: http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>
#include <SD.h>
#include <SPI.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS_PIN 2
/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
const int chipSelect = 4;
/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress Probe01 = { 0x28, 0x27, 0x26, 0x51, 0x05, 0x00, 0x00, 0x2B };
DeviceAddress Probe02 = { 0x28, 0x1A, 0x9B, 0x40, 0x05, 0x00, 0x00, 0x98 };
DeviceAddress Probe03 = { 0x28, 0x06, 0x2A, 0x41, 0x05, 0x00, 0x00, 0xB3 };
DeviceAddress Probe04 = { 0x28, 0xDB, 0xD9, 0x50, 0x05, 0x00, 0x00, 0x62 };
DeviceAddress Probe05 = { 0x28, 0xED, 0x1C, 0x51, 0x05, 0x00, 0x00, 0x42 };
DeviceAddress Probe06 = { 0x28, 0x16, 0xC5, 0x40, 0x05, 0x00, 0x00, 0xD2 };
DeviceAddress Probe07 = { 0x28, 0xA4, 0x1B, 0x41, 0x05, 0x00, 0x00, 0xC8 };
DeviceAddress Probe08 = { 0x28, 0xF0, 0xDC, 0x40, 0x05, 0x00, 0x00, 0xC6 };
DeviceAddress Probe09 = { 0x28, 0xC1, 0x94, 0x40, 0x05, 0x00, 0x00, 0xCB };
DeviceAddress Probe10 = { 0x28, 0xB5, 0xE6, 0x40, 0x05, 0x00, 0x00, 0x61 };
void setup() /****** SETUP: RUNS ONCE ******/
{
// start serial port to show results
Serial.begin(9600);
//while (!serial){
// ; // wait for serial port to connect. Needed for Leonardo only.
//}
Serial.print("Initializing Temperature Control Library Version ");
Serial.println(DALLASTEMPLIBVERSION);
Serial.print("Initialising SD card...");
// make sure that the default chip select pin is set to
// output, even if you dont use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialised:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// Dont do anything more:
return;
}
Serial.println("Card Initialised.");
// Initialize the Temperature measurement library
sensors.begin();
// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Probe01, 12);
sensors.setResolution(Probe02, 12);
sensors.setResolution(Probe03, 12);
sensors.setResolution(Probe04, 12);
sensors.setResolution(Probe05, 12);
sensors.setResolution(Probe06, 12);
sensors.setResolution(Probe07, 12);
sensors.setResolution(Probe08, 12);
sensors.setResolution(Probe09, 12);
sensors.setResolution(Probe10, 12);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
// make a string for assembling the data to log:
String dataString = "";
// read sensors and append to the string:
for (int digitalPin = 2; digitalPin < 3; digitalPin++)
{
int sensor = digitalRead(digitalPin);
dataString += String(sensor);
if (digitalPin < 2)
{
dataString += ",";
}
}
delay(3000);
Serial.println();
Serial.print("Number of Devices found on bus = ");
Serial.println(sensors.getDeviceCount());
Serial.print("Getting temperatures... ");
Serial.println();
// Command all devices on bus to read temperature
sensors.requestTemperatures();
Serial.print(" Probe 01: "); //Will print accross the line i.e. //Serial.print();
//Serial.print(" Probe 01: "); was changed to Serial.print(""); and results in just the temperature value being printed.
printTemperature(Probe01);
//The code below tells it to print a line then go down to next line.
//Serial.println();
Serial.print(" Probe 02: ");
printTemperature(Probe02);
//Serial.println();
Serial.print(" Probe 03: ");
printTemperature(Probe03);
//Serial.println();
Serial.print(" Probe 04: ");
printTemperature(Probe04);
//Serial.println();
Serial.print(" Probe 05: ");
printTemperature(Probe05);
//Serial.println();
Serial.print(" Probe 06: ");
printTemperature(Probe06);
//Serial.println();
Serial.print(" Probe 07: ");
printTemperature(Probe07);
//Serial.println();
Serial.print(" Probe 08: ");
printTemperature(Probe08);
//Serial.println();
Serial.print(" Probe 09: ");
printTemperature(Probe09);
//Serial.println();
Serial.print(" Probe 10: ");
printTemperature(Probe10);
Serial.println();
// Open the file. Note 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.println(dataString);
}
// If the file isn't open, pop up an error:
else {
Serial.println("Error opening datalog.txt");
//}
}
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
Serial.print("Error getting temperature ");
}
else
{
//Serial.print("C: ");
Serial.print(tempC);
//Serial.print(" F: ");
// Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}// End printTemperature
//*********( THE END )***********
Now the pin I have been using is digital 2 so I amended from the original code (AO) and substituted the analogRead etc for digitalRead etc. This still logged a value but i think it was just from Ao A1 and A2 as per the code. I did try plugging into AO but no change.
So in summation the temperature code example works printing to serial port but am unable to incorporate the SD code to log the data.
If there are any pointers I thank you in advance.
Regards,
Wayne