Hi ,
I am new to Arduino, and currently working on a project that requires reading from a lux sensor and storing readings to a SD card; meanwhile displaying reading to via Bluetooth. At this point I am having issue initializing the SD card. Everything seems to work properly whenever my Arduino board is connected to my PC through USB port (able to initialize SD card, reading from lux sensor, storing readings). However, if I try to use it without connecting to my PC, the SD card will not initialize, while everything else still work. I am wondering what the issue is causing this.
Some information regarding hardware I am using: Arduino Pro mini, TSL2591 lux sensor, HC-05 bluetooth module, 6 pin micro SD card module.
currently, bluetooth rx and tx is connected to pin 7,8 using software Serial. SD card module's CS, SCK, MOSI, MISO is connected to pin 10,13,12,11, respectively.
Below is the code I am currently working on
/*This program is intended for ME498 DIP Sensor*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
/* Golbal Variable and Software Serial pin setup*/
File myFile; //SD card file
// set up Rx,Tx pins
SoftwareSerial mySerial(8,7);
// SD Card Globle Variable
unsigned long myTime;
int count = 0;
const int chipSelect = 10;
const byte STRING_LENGTH = 8;
char input_date[STRING_LENGTH];
char fileName[80];
/* lux sensor set up*/
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // Sensor identifier
/* Below is used to configuring TSL2591 lux sensor. comment or uncomment
* lines for desired gain and integration time for the sensor
*/
void configureSensor(void)
{
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
//tsl.setGain(TSL2591_GAIN_MED); // 25x gain
//tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
//tsl.setGain(TSL2591_GAIN_MAX); // 9K GAIN
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
// tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
// tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
/* Display the gain and integration time for reference sake */
Serial.println(F("------------------------------------"));
Serial.print (F("Gain: "));
tsl2591Gain_t gain = tsl.getGain();
switch (gain)
{
case TSL2591_GAIN_LOW:
Serial.println(F("1x (Low)"));
break;
case TSL2591_GAIN_MED:
Serial.println(F("25x (Medium)"));
break;
case TSL2591_GAIN_HIGH:
Serial.println(F("428x (High)"));
break;
case TSL2591_GAIN_MAX:
Serial.println(F("9876x (Max)"));
break;
}
Serial.print (F("Timing: "));
Serial.print((tsl.getTiming() + 1) * 100, DEC);
Serial.println(F(" ms"));
Serial.println(F("Date: Monday, *****"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
}
/**************************************************************************/
/*
Program entry point for the Arduino sketch
*/
/**************************************************************************/
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
mySerial.begin(9600);
// Ask the user to input the current date//
mySerial.print(F("Please insert current date dd/mm/yyyy"));
while(!mySerial.available()) {
// wait for input
}
if (mySerial.available()){
delay(20);
mySerial.println(input_date);
}
mySerial.print(input_date);
sprintf(fileName, "Data%s.txt",input_date[8]);
//setting up TSL2591 Sensor//
mySerial.println(F("Starting Adafruit TSL2591 Test!"));
if (tsl.begin())
{
mySerial.println(F("Found a TSL2591 sensor"));
}
else
{
mySerial.println(F("No sensor found ... check your wiring?"));
while (1);
}
/* Display some basic information on this sensor */
//displaySensorDetails();
/* Configure the sensor */
configureSensor();
// SD Card Setup
while (!Serial) {
}
mySerial.print(F("Initializing SD card..."));
if (!SD.begin(10)) {
mySerial.println("initialization failed!");
while (1);
}
mySerial.println("initialization done.");
myFile = SD.open(fileName, FILE_WRITE);
//SD Card file prints
myFile.print("No.");
myFile.print("\t");
myFile.print("Time (s)");
myFile.print("\t");
myFile.println("Lux");
myFile.println("Date:");
}
/**************************************************************************/
/*
Show how to read IR and Full Spectrum at once and convert to lux
*/
/**************************************************************************/
float advancedRead(void)
{
float out_advread;
// More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
// That way you can do whatever math and comparisons you want!
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
full = lum & 0xFFFF;
out_advread = tsl.calculateLux(full, ir);
return out_advread;
}
void loop(void)
{
float var;
var = advancedRead();
//simpleRead();
// unifiedSensorAPIRead();
// software Serial
mySerial.println(var, 6);
// SD Card command
if (myFile) {
myTime = millis()/1000;
count = count + 1;
myFile.print(count);
myFile.print("\t");
myFile.print(myTime);
myFile.print("\t");
myFile.println(var, 6);
Serial.print(count);
Serial.print("\t");
Serial.print(myTime);
Serial.print("\t");
Serial.println(var, 6);
myFile.close();
myFile = SD.open(fileName, FILE_WRITE); // Change file number for every test run
}
delay(2000);
}