SD initialization fails if WiFi is powered on

Hi Experts,

I'm seeking help to work around this problem. My code looks like below. The problem is if I've ever turned on WIFI even later it's closed, SD initialization fails (sd.begin will return false). If WIFI is never turned on, then it's OK. I use ESP8266 wifi module which is connected to my ATMEGA328's Tx/Rx. My MicroSd is SanDisk Ultra 32GB. If using other MicroSD (like kingston 1GB or 128MB version), it's OK as well.

A friend said it's probably due to the high current spike when wifi is turned on, which causes current/voltage disruption to MicroSD which leads to the initialization failure. I want to hear you opinions about this. Especially, what can I do to workaround this without changing the hardware.

Thanks.

void loop()
{
    digitalWrite(WIFI, LOW);
   
    // program works if commenting out this line 
    digitalWrite(WIFI, HIGH);
    delay(5000);
    digitalWrite(WIFI, LOW);
    delay(5000);

    // program fails here if WIFI is ever turned on
    initializeSD();
    ifstream sdin(sdLogFile);
    if (sdin.good()) {
        DEBUG_PRINTLN(F("I'm good"));
    } else {
        DEBUG_PRINTLN(F("I'm bad"));
    }

    delay(5000);
}

boolean initializeSD()
{
    int i = 0;
    while (!sd.begin(chipselect, SPI_FULL_SPEED)) {
        Serial.print(++i); Serial.println(F(" begin fail."));
        digitalWrite(POWER, LOW);
        delay(2000);
        digitalWrite(POWER, HIGH);
        delay(2000);
        if (i > 3) return false;
    }
    return true;
}

I don't know what the WIFI and POWER pins do or what they are. Why do you want to turn the wifi module on and off ? How is the hardware ? Do you have a schematic ?

How are you powering the ESP8266 wifi module? How are you powering the SD card reader, and which SD module are you using?

Hi cattledog and Peter_n,

My atmega328 is powered by 3xAA batteries, which also serve as input power to a LDO which output 3.3v and power both wifi and micro sd module. This LDO has an enable pin, which can be switched on/off by atmega328 pin(POWER). ESP8266 also has an chip power pin (WIFI) so I can use atmega328 to control it as well.

I want to turn them on/off to save power. The attached code is a reduced test case for troubleshooting. In reality, I have 3 modes

  • SD on and wifi off when writing data;
  • both SD and wifi on when uploading data.
  • both off when sleeping

The issue happens when device wakes up and tries to upload. I used the standard latest greiman/SdFat library. And my ESP8266 has firmware at 0.9.5 above. My other hardware are pretty much like this one

Please kindly advise. BTW, I only have time to check forum after 21:00 Australian Eastern Time so please forgive if I’m not responsive.

Thanks.

The problem is if I've ever turned on WIFI even later it's closed, SD initialization fails (sd.begin will return false). If WIFI is never turned on, then it's OK.

If using other MicroSD (like kingston 1GB or 128MB version), it's OK as well.

Are you saying that The Kingston card will work if the WIFI has been switched on and off, but the SanDisc card will not?

Do you have bypass caps on the inputs to the SD and the WIFI modules?

If the sd.begin() comes before the WIFI is turned on and off can you later read and write the card?

I think we are getting closer to the problem. The card might not be compatible. If it works a little, it is not compatible. Is the Arduino SD library compatible with 32GB ? Or the newer sdfat library ?

The ESP8266 needs current (135mA ?), the batteries and/or the LDO might not be able to supply that current.

Could you test it with a good power supply (5V 1A or so) and a LDO of 800mA.