SD card (micro and normal one) only write when it is pressed into the reader

Hi folks,
I have recently programmed a bit around my Doppler-radar to log the speed on the street in front of my house.
Anyways, the code works so far. But what does not is writing the speed to an SD.

Here is how it goes:
When the SD (or MicroSD) is in the reader, nothing is written. ONLY when it press the SD beyond the point where the reader clicks and releases the SD (I have to keep it pressed), it will access the SD.

Basically, each of the readers have some mechanism to hold the SD and release it upon pressing. So once I press it beyond, it seems like there is a contact closed or whatever. No idea.
But actually the device is pretty useless when it has to be pressed all the time.

What could be wrong with it?
I'm using it on a nodemcu with some alternative pins and also reassigned the CS pin. (it is originally on the same pin as the UART)
But I use a library which allows for that.

MicroSD

SD (should be a HW 203)
https://www.real.de/product/354746726/?kwd=&source=pla&sid=33374465&gclid=Cj0KCQjwsuP5BRCoARIsAPtX_wEmvsC8bgWhxEaFiN9TBmpMT4nY4jS0DLqNYb4IuI-WM3AS-DtEYnwaAl4iEALw_wcB

Any idea how solve this?
Regards,
Maeffjus

(deleted)

Hey Peter,
I'm fine, I hope you are too!

I did, but only for the MicroSD. Because it happened to be like this, I changed to the large normal SD.

So the MicroSD also only worked with the sketches when being pressed. But I really wondered how this could come. So then I used for my original sketch the HW 203 and things kept being the same.

Actually I wondered if there might be a switch that is pressed upon changing the SD card. This might somehow goes along with my issues... No idea what it could be else.

Because how could to fully independent adaptors be faulty? (not even the same type...)
Regards,
Maeffjus

Hey Peter,
seems like also my brains memory was wrong…:

The MicroSD (HW-215) does work, but:

The NodeMCU does only “boot” (or start fully) when I unplug the SD-reader.
No idea why, but basically it will only start the program when I plug-in power with an unplugged MicroSD reader and then works, but not when the VCC & GND on it are connected right from the start.

Maybe my code is faulty? (Just keep in mind: once it “works”, when not unplugging the USB-power, it even keeps going when reset is pressed)
But in any case the MicroSD needs to be unplugged during power-up.

See the code here:

#define FS_NO_GLOBALS 
#include <TimeLib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <mySD.h>
#include <FS.h>
#include <WiFiClient.h>

// NTP Servers:
static const char ntpServerName[] = "pool.ntp.org";

// VARIABLES
File root;
const int timeZone = 2;     // Central European Time
WiFiUDP Udp;
unsigned int localPort = 8888;  // local port to listen for UDP packets

time_t getNtpTime();
void digitalClockDisplay();
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
boolean newData = false;
float laenge;
String Geschwindigkeit_lang;
float Geschwindigkeit;
float max_geschwindigkeit;
String Richtung;
boolean ankommend;
unsigned long previousMillis = 0;
const long interval = 600000; 
unsigned long currentMillis;
unsigned long old_speed_Millis;
const long speed_interval = 30000;
float totalmax;
float speed[200];


void setup() {
  Serial.begin(115200);
  Serial.swap();
  Wire.begin(D2, D1); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
  WiFi.begin("WIFI-Network", "password");
  lcd.init();
  lcd.backlight();
  lcd.home();
  lcd.clear();
  lcd.setCursor ( 0, 3 );
  lcd.print(WiFi.localIP());
  lcd.setCursor ( 0, 3 ); 
  delay (5000);
  lcd.print(WiFi.localIP());
    
  Udp.begin(localPort);
  setSyncProvider(getNtpTime);
  setSyncInterval(300);  
  pinMode(D6, OUTPUT); //SDCard CS-Pin
  SD.begin(D6, D4, D3, D5);
    }

void loop() {
    recvWithEndMarker();
    umrechnung();
    maximum();
    showNewData();
    
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
     
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
      
       //Serial.println(receivedChars);
       lcd.clear();
       lcd.setCursor ( 0, 0 );        // go to the next line
       lcd.print(max_geschwindigkeit);
       lcd.setCursor ( 0, 1 ); 
       lcd.print(Richtung);
       lcd.setCursor ( 0, 2 );
       lcd.print(ankommend);
       lcd.print("  ");
       lcd.print(totalmax);
       lcd.print("km/h");

       lcd.setCursor ( 0, 3 );
       lcd.print(year());
       //lcd.setCursor ( 4, 3 );
       lcd.print("-");
       //lcd.setCursor ( 5, 3 );
       lcd.print(month());
       //lcd.setCursor ( 7, 3 );
       lcd.print("-");
       //lcd.setCursor ( 8, 3 );
       lcd.print(day());
       //lcd.setCursor ( 10, 3 );
       lcd.print(" ");
       //lcd.setCursor ( 11, 3 );
       lcd.print(hour());
       //lcd.setCursor ( 13, 3 );
       lcd.print(":");
       //lcd.setCursor ( 14, 3 );
       lcd.print(minute());
       //lcd.setCursor ( 16, 3 );
       lcd.print(" ");
       //lcd.setCursor ( 17, 3 );
       lcd.print(max_geschwindigkeit);
       //delay (2000);
       //lcd.print(timeClient.getFormattedTime());
       //delay (2000);
   
       newData = false;
       
    }
}

void umrechnung() {
 if (newData == true) {
    Geschwindigkeit_lang = receivedChars;
    Geschwindigkeit_lang.remove(0, 12);
    Geschwindigkeit_lang.remove(5);
    Geschwindigkeit = Geschwindigkeit_lang.toFloat();
    Richtung = receivedChars;
    Richtung.remove(0, 19);
    Richtung.remove(1);
    if (Richtung == "+"){
      ankommend = false;}
    if (Richtung == "-"){
      ankommend = true;}
 
 }
}


void maximum(){
  currentMillis = millis();

 if ((newData == true)&(currentMillis - old_speed_Millis > speed_interval)){
  old_speed_Millis = currentMillis;
  if (max_geschwindigkeit >5){
  sdwrite();
  }
  max_geschwindigkeit = 0;
  return;
  }  
   
 if ((newData == true)&(currentMillis - old_speed_Millis <= speed_interval)){
  
  if (Geschwindigkeit > max_geschwindigkeit){
      max_geschwindigkeit = Geschwindigkeit;
      }
       if (totalmax < max_geschwindigkeit){
      totalmax = max_geschwindigkeit;
      }
 }
 }

 void sdwrite(){
  root = SD.open("speed.txt", FILE_WRITE);
  root.print(year());
  root.print("-");
  root.print(month());
  root.print("-");
  root.print(day());
  root.print(",");
  root.print(hour());
  root.print(":");
  root.print(minute());
  root.print(":");
  root.print(second());
  root.print(",");
  root.print(max_geschwindigkeit);
  root.print(",");
  root.println(ankommend);
  root.flush();
  root.close();
}


/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  IPAddress ntpServerIP; // NTP server's ip address

  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  // get a random server from the pool
  WiFi.hostByName(ntpServerName, ntpServerIP);
  Serial.print(ntpServerName);
  Serial.print(": ");
  Serial.println(ntpServerIP);
  sendNTPpacket(ntpServerIP);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  Serial.println("No NTP Response :-(");
  return 0; // return 0 if unable to get the time
}


// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12] = 49;
  packetBuffer[13] = 0x4E;
  packetBuffer[14] = 49;
  packetBuffer[15] = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

This is the only issue now preventing me from using the device.
(Oh and I’d like to add some server to remotely get the speed.txt ;-))
Any answer is highly appreciated!

Regards,
Maeffjus

Found it:

I have connected one wire from the SD-reader to D§, but this is a pin which, when high during boot (which it might be?!) does force the NodeMCU to boot from SD instead of internal memory.
I have tried yesterday for a longer time and finally ended-up with having the only pin usable (free) is RX (GPIO3).
Once I'm beyond the stage of debugging and building, I will remove the I2C display, which will then let me use some "real" and designated digital pins.