I just started using/playing around with my Arduino and the Dataloggershield 1 week ago, therefore I don't know too much about it. Additonally I am an mech. engineer and no expert on electronics.
I want to do temperature measurements and resistance measurements at the same time and log everything onto my SD card using the logger shield. For the temperature I use 2 waterproof DS18B20 that are hooked up to digital pin 8. For the resistance I build my own voltage splitter with a known resistance of 2167 Ohmm hooked up to analog pin 0.
Since I am fairly new to the entire think I have just been clueing parts of code together that I was able to find in the toturials for the different parts.
When I run the different measurement independly, they both work like a charm, but when I include the resistance measurement into the temperature and logging sketch, the analogread commant seems not the read the pin. It always returns a value of 1023, resulting in a calculated resistance for the "to-be-measured" resistors of zero. I have manually measurent the voltage form pin 0 to grd with a multimeter and read a value that I am expecting considering the resistor I hooked up as R2 for testing.
I hope this text is somewhat understandable. I am sorry for any mistake, but please consider that english is not my motherlanguage
Here is the code I am using and the results from the Serial monitor:
- Code: Select all
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h>
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
OneWire ds(8); // on pin 8
RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
float temps[3]={0,0,0};
int resPin = 0; //Pin for resistance Measurment
int R1 = 2167; //first resistance in Voltage splitter
int resValue = 500;
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(redLEDpin, HIGH);
while(1);
}
void setup(void)
{
Serial.begin(9600);
Serial.println();
// use debugging LEDs
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START
// initialize the SD card
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)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("millis,stamp,datetime,temp1,temp2,res");
#if ECHO_TO_SERIAL
Serial.println("millis,stamp,datetime,temp1,temp2,res");
#endif //ECHO_TO_SERIAL
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
}
void loop(void)
{
DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
digitalWrite(greenLEDpin, HIGH);
// log milliseconds since starting
uint32_t m = millis();
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
now = RTC.now();
// log time
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(", ");
logfile.print('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
#if ECHO_TO_SERIAL
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.print(", ");
Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print('"');
#endif //ECHO_TO_SERIAL
for(int k=0;k<1;k++)
{
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
//return;
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
digitalWrite(redLEDpin,HIGH);
return;
}
Serial.println();
//the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
//Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
//Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
digitalWrite(greenLEDpin, LOW);
temps[k] = celsius;
//logfile.println(celsius);
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.println(" Celsius, ");
}
for (int t=0;t<2;t++){
logfile.print(",");
logfile.print(temps[t]);
}
Serial.print(resValue);
analogRead(resPin);
delay(20);
resValue = analogRead(resPin);
Serial.println(resValue);
float voltage2 = resValue * 5.0 / 1023.0;
Serial.println(voltage2);
float R2 = R1 * voltage2 / (5.0-voltage2);
logfile.print(",");
logfile.println(R2);
Serial.print("Resistance: ");
Serial.println(R2);
//logfile.println(",");
digitalWrite(greenLEDpin, LOW);
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
// blink LED to show we are syncing data to the card & updating FAT!
digitalWrite(redLEDpin, HIGH);
logfile.flush();
digitalWrite(redLEDpin, LOW);
}
- Code: Select all
Initializing SD card...card initialized.
Logging to: LOGGER21.CSV
millis,stamp,datetime,temp1,temp2,res
1000, 1334054979, "2012/4/10 10:49:39"
Temperature = 20.12 Celsius,
5001023
5.00
Resistance: 0.00
3000, 1334054981, "2012/4/10 10:49:41"No more addresses.
Temperature = 20.12 Celsius,
10231023
5.00
Resistance: 0.00
4999, 1334054983, "2012/4/10 10:49:43"
Temperature = 20.12 Celsius,
10231023
5.00
Resistance: 0.00
6999, 1334054985, "2012/4/10 10:49:45"No more addresses.
Temperature = 20.12 Celsius,
10231023
5.00
Resistance: 0.00
8999, 1334054987, "2012/4/10 10:49:47"
Temperature = 20.12 Celsius,
10231023
5.00
Resistance: 0.00
10998, 1334054989, "2012/4/10 10:49:49"No more addresses.
As you can see "resValue" is 500 at the beginning (as defined at the top) and than is overwritting by the returned analogread of 1023. In this example I am only using 1 DS18B20, but I get the same results with 2 of them.
Does anybody have an idea? I tried differnt analog pin. I tried refering to them with "A0" and declaring them as input. I also found the tip to deploy a short delay after a analogread command and then reading again. Nothing has worked so far.
Thank you!

