Program hangs when SD begin is called.


I’m compiling a program for a mega, but have unfortunately overloaded the voltage regulator, so while I’m awaiting a replacement board I’ve continued developing my program using my uno.

I will include a snippet from my code, where I am having trouble:

#include <SD.h>                 // SD Card functions
#include <Wire.h>               // I2C Comms
#include <SPI.h>                // SPI bus 
#include "RTClib.h"             // Realtime Clock

//RTC_DS1307 rtc; //Instance of RTClib

File dataFile; 

const int chipSelect = 4;

unsigned long PreviousMillis = 0;      // Variable for milliseconds since last update
long Interval1 = 2000;                 // Variable for sequence step 1 in milliseconds
long Interval2 = 2000;                 // Variable for sequence step 2 in milliseconds
//long Interval3 = 2000;                 // Variable for sequence step 3 in milliseconds
int PSensorVal = 0;                    // Variable for raw sensor value
int LastPVal = 0;                      // Variable for last pressure reading
int PVal = 0;                          // Variable for current pressure value
int CycleStage = 0;                    // Variable for handling run cycle position
int ErrorHandler = 0;                  // Variable for error handling
int ValveState = LOW;                  // Variable for valve state

const int AnalogInPin = A0;
const int ValveRly = 12;

void setup() {

  pinMode(ValveRly, OUTPUT);
  pinMode(SS, OUTPUT);

    Serial.println("Serial started... ");

 // Initialize SD Card

  //if (!SD.begin(10, 11, 12, 13)) { //Use this for MEGA
    //if (!SD.begin(chipSelect)) { // Use this for UNO
    //lcd.print("CARD FAILED!");
    //Serial.println("Card failed, or not present");
    // don't do anything more:
    //while (true) {}

void loop() {

 unsigned long CurrentMillis = millis();
  //int PMax = 400;

if ((ValveState == LOW) && (CurrentMillis - PreviousMillis >= Interval1)) {
    //CycleStage = 1;
    PreviousMillis = CurrentMillis;  // Remember the time
    ValveState = HIGH;
    digitalWrite(ValveRly, ValveState); 
     //while (PVal < PMax){ 
      //CycleStage = 2;
      //if (CurrentMillis - PreviousMillis >= Interval3);
      //ErrorHandler = 10;
      //RunState = 3;
    }else if ((ValveState == HIGH) && (CurrentMillis - PreviousMillis >= Interval2)) {
    //CycleStage = 0;
    PreviousMillis = CurrentMillis;   // Remember the time
    ValveState = LOW;
    digitalWrite(ValveRly, ValveState);

    //LastPVal = PVal;
    //Start_counts ++;
    //if(Start_counts % 10 == 0) {
      //Total_counts = (Total_counts + Start_counts);  // Update Total counts
      //Start_counts = 0;                             // Reset start counts
      //dataFile =, FILE_WRITE); // Open todays file
      //if(dataFile) {                            // If file opens
        //Serial.println("Writing to: ");
       // dataFile.print(dateArray);
        //dataFile.print(", ");
       // dataFile.print(", Counter: ");
        //dataFile.print(", Pressure:");
        //dataFile.print(" bar");
        //dataFile.close();           // Close the file
        //Serial.print("   Write Complete");
       // Serial.println("");
     // }
     // else {
     // Serial.print("Write Failed");

What I’m trying to achieve is a data logger that activates a hydraulic valve until a pressure reading is achieved, then hold at pressure at interval (2), then release and wait interval (1). If the pressure value isn’t achieved within a given interval(2), allow another interval (3). I have included various bits of code to handle errors and emergency stop routines. For every 10th cycle the data will be recorded onto an sd card.

The problem I’m having is that when I run the code above the relay output is true to the interval delays, but if I include the sd begin function the program will only run for a couple of seconds, then hang. However, the program will record every 10th cycle, but no relay switching…

I’m wondering if my program isn’t optimised for efficient sram usage? I intend to use a mega for the final program, but I have a fair bit more code going on as well.

Any help would be appreciated.

Many thanks!

ValveRly can't be on pin 12 if you're going to use the SD on the UNO. 12 is one of the SPI pins that the SD card will use. On MEGA the SPI pins are different.

Hi Delta G,

Thank you for your reply! That makes sense, I will change the pins over and see what I get. :slight_smile: