1

I'm working on a project to output the temperature from a DS18B20 to a 16x2 LCD while logging the temperature to a CSV file using the Adafruit Data Logging Shield. I keep getting this error when verifying the code:

RTClib-master/RTClib.cpp.o: In function `RTC_DS1307::now()':
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:252: undefined reference to 'Wire'
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:252: undefined reference to 'Wire'
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:252: undefined reference to 
'TwoWire::beginTransmission(int)'RTClib-master/RTClib.cpp.o: In function 'TwoWire::write(int)':
/Applications/Arduino.app/Contents/Resources/Java/libraries/Wire/Wire.h:72: undefined reference to 'TwoWire::write(unsigned char)'
RTClib-master/RTClib.cpp.o: In function 'RTC_DS1307::now()':
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:254: undefined reference to 'TwoWire::endTransmission()'
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:256: undefined reference to 'TwoWire::requestFrom(int, int)'
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:257: undefined reference to 'TwoWire::read()'
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:258: undefined reference to 'TwoWire::read()'
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:259: undefined reference to 'TwoWire::read()'
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:260: undefined reference to 'TwoWire::read()'
/Users/g/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:261: undefined reference to 'TwoWire::read()' RTClib-master/RTClib.cpp.o:/Users/grafton/Documents/Arduino/libraries/RTClib-master/RTClib.cpp:262: more undefined references to 'TwoWire::read()' follow

When I comment RTC_DS1307 RTC; in the code (below) these errors go away, but of course I get errors related to an undefined RTC. I'm a newbie cobbling together code from various sources, so any help is greatly appreciated. Code follows:

#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
#include <SD.h>
#include "RTClib.h"

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const int chipSelect = 10;
File tempLog;
RTC_DS1307 RTC;

LiquidCrystal lcd(6, 7, 8, 9, 11, 12);

void setup() { 
  Serial.begin(9600);
   while (!Serial) {
    ; 
  lcd.begin(16, 2);
  //         ----------------  
  lcd.print("Current temp (F)");
   sensors.begin();
   }

Serial.print("Initializing SD card...");
  pinMode(SS, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println("Initialization failed!");
    while (1);
  }
  Serial.println("Initialization done.");

tempLog = SD.open("templog.txt", FILE_WRITE);  
if (! tempLog) {
  Serial.println("Error opening templog.txt");
  while(1);
}
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
      tempLog = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }

  Serial.print("Writing to: ");
  Serial.println(filename);
  tempLog.println("Data Logging File");
  tempLog.println("Date,Temp(F)");
  } 
} 

void loop() {
  sensors.requestTemperatures();
  float temperature = sensors.getTempFByIndex(0);
  lcd.setCursor(6, 1);
  lcd.print(temperature);
  Serial.println(temperature);
  //-----------------------------------------------------------

String dataString = "";

//date time stuff-------------------------------------------

DateTime now = RTC.now();
  // log time
// tempLog.print(now.unixtime()); // seconds since 1/1/1970
//  tempLog.print(", ");
  tempLog.print('"');
  tempLog.print(now.year(), DEC);
  tempLog.print("/");
  tempLog.print(now.month(), DEC);
  tempLog.print("/");
  tempLog.print(now.day(), DEC);
  tempLog.print(" ");
  tempLog.print(now.hour(), DEC);
  tempLog.print(":");
  tempLog.print(now.minute(), DEC);
  tempLog.print(":");
  tempLog.print(now.second(), DEC);
  tempLog.print('"');

  tempLog.print(",");
  tempLog.print(temperature);
  tempLog.print(",");
  delay(500);

tempLog.println(dataString);
tempLog.flush();

}
1

The RTC library requires the Wire library. Just put #include <Wire.h> in front of the line #include "RTClib.h"

  • I have tried to simply #include <Wire.h> in RTClib.h, rather than putting #include <Wire.h> above #include "RTClib.h" in my sketch, but this does not work. Do you know why? – Zilliput Jan 31 '15 at 0:23
  • That indeed doesn't work. I don't know the reason; sorry. – Gerben Jan 31 '15 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.