Arduino with external power sourse


#1

Hi I’m doing a project to log temp and humidity with respect to time. I have completed the basic coding with the help of this website. But the problem is that it work only when it is connected to PC when I give a external power supply ( battery ) it is lot logging any think may be due to serial port (I think so) below i’m giving my code. Plz help me with it.
#include “Arduino.h”
#include “DHT.h”
#include “Wire.h”
#include “RTClib.h”
#include “SD.h”

// Pin Definitions
#define DHT_PIN_DATA 2
#define SDFILE_PIN_CS 10

File sdFile;
DHT dht(DHT_PIN_DATA);
RTC_DS3231 rtc;

void setup()
{

Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println("start");

dht.begin();
if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
pinMode(SDFILE_PIN_CS, OUTPUT);
if (!SD.begin()) {
    Serial.println(F("Card failed, or not present"));
    while(1);
}
Serial.println(F("card initialized."));    

}

void loop()
{
sdFile = SD.open(“datalog2.txt”, FILE_WRITE);
if (sdFile) {
DateTime now = rtc.now();

sdFile.print(now.hour(), DEC);
sdFile.print(':');
sdFile.print(now.minute(), DEC);
sdFile.print(':');
sdFile.print(now.second(), DEC);

sdFile.print(F(" "));

float dhtHumidity = dht.readHumidity();
float dhtTempC = dht.readTempC();
sdFile.print(dhtHumidity);
sdFile.print(F(" "));
sdFile.print(dhtTempC);
sdFile.println();
sdFile.close();
delay(1000);}
else { Serial.println(F(“error opening file.”));
}

}