Hello! I'm trying to set up a datalogger (time with real time clock module, temp, humidity, photocell reading) with an Arduino Uno. I think I've got everything physically connected correctly, which I know because when I test every individual part, it all works. The SPI module works if I use a default test sketch, but it doesn't work with my datalogger sketch. I have formatted the 4gb SD card as recommended on this forum. The sensors all work, the SPI module will write to the SD card using the default sketch, but when I use the same hardware and the same connections with my own sketch, it's not working, which leads me to believe it's a programming problem. I've tried comparing it line by line to the test sketch but I can't see what's different. Please could you advise what I should look for? Thank you!
This is my sketch that is not working:
#include <SPI.h> // include Arduino SPI module (SD card module) library
#include <SD.h> // include Arduino SD library
const int chipSelect = 10; //SD card CS pin connected to pin 10 of Arduino
File myFile;
int photocellPin = A0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LED1 = 7; // LED connected to pin 7
int LED2 = 6;
int LED3 = 5;
int LED4 = 4;
int LED5 = 3;
int LED6 = 8;
int LED_POWER = 1;
void setup(){
Serial.begin(9600);
digitalWrite(LED_POWER, HIGH);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Initialize_SDcard(); // Calls the function Initialize_SDcard()
//pinMode(10, OUTPUT); // Sets the pins for SD card and LEDs as outputs
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(1, OUTPUT);
//digitalWrite(10, HIGH); // Tells Arduino that the SD card pin is an output
}
void Initialize_SDcard()
{
if (!SD.begin(chipSelect)) // Checking if the SD card is missing
{
Serial.println("Card failed, or not present"); // Error message if SD card missing
return;
}
else
{
myFile = SD.open("datalogger.txt", FILE_WRITE); // New file created if SD card not missing
if (myFile) {
myFile.println("Time, Temperature, Humidity, Photocell"); //Write the first row of the file with headings
myFile.close();
}
else {
Serial.println("Error opening datalogger.txt");
}
}
}
void Read_Values()
{
photocellReading = analogRead(photocellPin); // The value of the photocell will be saved as variable photocellReading
Serial.print(" photocell:");
Serial.println(photocellReading);
}
void Write_SDcard()
{
myFile = SD.open("datalogger.txt", FILE_WRITE); // The file name for the SD card file
if (myFile) // The section below will save temp, humidity & photocell value to SD card
{
myFile.println(photocellReading);
myFile.close(); //Close the file
}
else
{
Serial.println("SD card writing failed");
}// Error message if the SD card doesn't work
}
void loop(){
Read_Values(); // Launches the Read_Values() function
Write_SDcard(); // Launches the Write_SDcard() function
if (photocellReading > 900) // If it gets extremely dark, one LED will flash
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
delay (500);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
delay(5000);
}
else if (photocellReading > 800) // This should be the baseline for normal light
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
delay(1000);
}
else if (photocellReading > 700)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
}
else if (photocellReading > 600)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
delay(1000);
}
else if (photocellReading > 500)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
delay(1000);
}
else if (photocellReading > 400)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, LOW);
delay(1000);
}
else if (photocellReading > 300)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
delay(1000);
}
else if (photocellReading > 0) // If light levels are extremely high, all 5 LEDs flash at 0.5 second intervals
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
delay (500);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
delay (500);
}
}
This is the test one I used that IS working:
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("This is a test file 2:)");
myFile.println("testing 1, 2, 3.");
for (int i = 0; i < 20; i++) {
myFile.println(i);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}