Having trouble join strings together for datalogging

I am trying to modify code so that it outputs to a txt file on a sd card,

  • the timestamp from ds1307
  • temp from sht15
  • humidity from sht15
    My project so far is using a - ds1307 rtc module w/eeprom(not working yet)
  • sht15 temp/humidity sensor
  • yl-30 sd card module
    using the following libraries:
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    #include “RTClib.h”
    #include <SHT1x.h>

I am able to use the standard datalogging code (from 3 analog sensors to sd)
I’ve attempted to modify the part of the code which joins the 3 varibles into a string
on the datalog.txt file seperated by commas. Im trying to modify the code so that it joins
a timestamp with temp and humidity values. so far no luck.
im sure this is very simple, but i am relatively new to arduino, so i find the syntax confusing.

so far the code i have created is just generating compiler errors

// make a string for assembling the data to log:
String dataString = “”;
// read three sensors and append to the string:
dataString += String(now.hour());
dataString += “:”;
dataString += (now.minute());
lcd.print datastring();

I am using an arduino uno board powered by usb programming cable.
if you require photos / full source code, just say an i’ll post

so far the code i have created is just generating compiler errors

There is no reason to use the String class. The lcd.print() function can be called more than once. There is no benefit to be gained from trying to do all the output in one step.

If you feel that you must, anyway, use sprintf() to populate an array of chars.

ok thanks for your reply, my re-jigged code looks like:

  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 

  dataFile.print(" ");
  dataFile.print(" ");

the analog pin part is irrelevant for my project. i have included it as it shows how my code behaves.

output in txt file is:

21:21:33 304,326,306
21:21:41 293,314,294

which is what i was looking for, so that i can do some graphing/analysis in excel or something at some stage

my re-jigged code looks like

There is still no reason to be using the String class. The SD class buffers character data, until the buffer is full or the file is to be closed. If you call the File::print() method with a String, that method needs to unwrap the String to get the embedded data, duplicating the storage needed, wasting precious resources. Learn to use char arrays and the fact that File derives from Print so that print() doesn't need for you to pass it strings or Strings.

Could you provide some sample code so that i can understand how it works in my example?
due to my limited programming knowledge, 'm doing just what works at the moment, obviously once i've finished a prototype i would then use my new found experience to optimize the code. at the moment my code takes 4 secs to loop, that's with only a total of 2000ms , "delay(2000)"

Could you provide some sample code so that i can understand how it works in my example?

Get rid of dataString. Instead, in the loop, of appending to dataString, write to the file.

this is my enitire code so far, it works pretty well, however my problem now is that
when i remove usb power supply , and power it from a wall transformer the 1st line of the display reads, "Time 16516585 ", which is not ideal if i want to use it in a “standalone” manner

//Compatible with the Arduino IDE 1.0
//Library version:1.1
//A4/SDA and A5/SCL
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <SHT1x.h>

// Specify data and clock connections and instantiate SHT1x object
#define dataPin  4
#define clockPin 2

SHT1x sht1x(dataPin, clockPin);
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  lcd.init();                      // initialize the lcd 
  lcd.backlight();     //lcd.noBacklight();.

void loop()
  lcd.setCursor(0, 0);  
  lcd.print("Time ");
    DateTime now = RTC.now();
    lcd.print(now.hour(), DEC);
    lcd.print(":  ");
    lcd.print(now.minute(), DEC);
    lcd.print(":  ");
    lcd.print(now.second(), DEC); 
  float temp_c;
  float humidity;

  // Read values from the sensor
  temp_c = sht1x.readTemperatureC();
  humidity = sht1x.readHumidity();

  // Print the values to the serial port
  lcd.print("Temp ");
  lcd.print(temp_c, DEC);
  lcd.print(" Humid ");
  Serial.print("Temperature: ");
  Serial.print(temp_c, DEC);
  Serial.print("F. Humidity: ");

I think I have seen all those 165s before, and it was a power supply problem - to the Arduino, not the clock. This must be pretty hard to do if you are moving from a USB cable to a wall wart but you might check that you are getting a decent 7+ volts out of it.

I doubt that the data output you show is really what you are looking for as the timestamp isn't comma separated. All you need do is ditch the strings and print all the data, just like to the LCD, but with a comma inserted between each with the last one being a println to get a new line. This might do wonders for your loop-cycle time too.

The approach is the same whether you are doing a serial.print to a PC via cable or bluetooth, or a myfile.print to an on-board SD card. One difference is that there is no need to send time to the PC, you can use its own clock for that. The same applies when sending data to the Internet.

You might get value from PLX-DAQ, which is an Excel macro. This allows you to send data from Arduino directly into an on-screen Excel .csv file. It also offers graphs in real-time.