Uno: When writing to an SD card, the little red led on the SD card module blinks. How do I emulate this on a larger LED?

Arduino Uno, SD card, random temp sensor (removed from code)

Absolute noob here. I spent a couple hours watching videos and trying to figure this out but I give up lol. It should be easy, right? So, below's my code. I thought it would be a boolean thing. The Tx LED flashes twice, and the red SD card module's LED flashes twice. I'm not sure what it's flashing for though: opening and closing the text file? opening/writing and closing?

Ideally, I want the LED to flash twice brightly to emulate the coolness factor of these onboard LEDs... but I was just trying to get it to turn on in the first place.

I also tried setting sdState = LOW, rather than to false, but that didn't work either.
Any help would be appreciated. Thank you!



#include <SPI.h>
#include <SD.h>

//Global Definitions
uint32_t timeStamp = 0; 

//LED — for photoresistor, this will change
const int ledSD = 2;
bool sdState = false;
// DO NOT USE PIN 10

//SD Card
File myFile; //name of file object
const int cS = 4; //SD card write

void setup() {
  Serial.begin(115200);

  pinMode(ledSD, OUTPUT);
  pinMode(10, OUTPUT); //Required for SD card module, do not use

//SD CARD
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(cS)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
 
 //Print initial startup
 myFile = SD.open("text.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("");
    myFile.close();
    // debug
    Serial.println("");
              }
//debug
    delay(500);
}

void loop() {
  timeStamp = millis(); // this declaration must be in void loop
//LED
/*  unsigned long ms = millis();
  if ( (ms - preTime) >= 1000) {
    preTime = ms;
  */
  if (sdState == true){
    digitalWrite(ledSD, HIGH); 
  } else {
      digitalWrite(ledSD, LOW);
  }
    
 // SD CARD WRITE
  myFile = SD.open("text.txt", FILE_WRITE);
  if (myFile) {
    sdState = true;
//Time
    myFile.print(timeStamp);
    Serial.print(timeStamp);
  
  myFile.close();
  sdState = false;
        }
else {
    Serial.println("error opening templog");
  }
  //END SD card write

delay(5000); 

}


Hello

Try this non-real-time solution by using additional delay() functions.

#include <SPI.h>
#include <SD.h>

//Global Definitions
uint32_t timeStamp = 0;

//LED — for photoresistor, this will change
const int ledSD = 2;
bool sdState = false;
// DO NOT USE PIN 10

//SD Card
File myFile; //name of file object
const int cS = 4; //SD card write

void setup() {
  Serial.begin(115200);

  pinMode(ledSD, OUTPUT);
  pinMode(10, OUTPUT); //Required for SD card module, do not use

  //SD CARD
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(cS)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

  //Print initial startup
  myFile = SD.open("text.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("");
    myFile.close();
    // debug
    Serial.println("");
  }
  //debug
  delay(500);
}

void loop() {
  timeStamp = millis(); // this declaration must be in void loop
  //LED
  /*  unsigned long ms = millis();
    if ( (ms - preTime) >= 1000) {
      preTime = ms;
  */
  if (sdState == true) {
    digitalWrite(ledSD, HIGH);
  } else {
    digitalWrite(ledSD, LOW);
  }

  // SD CARD WRITE
  myFile = SD.open("text.txt", FILE_WRITE);
  if (myFile) {
    sdState = true;
    //Time
    myFile.print(timeStamp);
    Serial.print(timeStamp);

    myFile.close();
    sdState = false;
// ------
    digitalWrite(ledSD,HIGH);
    delay (1000); 
    digitalWrite(ledSD,LOW);
    delay (1000); 
    digitalWrite(ledSD,HIGH);
    delay (1000); 
    digitalWrite(ledSD,LOW);
// ------    
  }
  else {
    Serial.println("error opening templog");
  }
  //END SD card write

  delay(5000);

}

Have a nice day and enjoy coding in C++.

That certainly works, and I'll use it, thank you.

I think my frustration is that I was trying to make it connected to an action, rather than just blinking at the end of the loop.

1 Like

Quick update, this was working beautifully, thank you again

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.