user write firmware for arduino mega 2650. Write something that goes to serial. to just test the connection --- assistant Here's a minimal Arduino Mega 2560 sketch that prints to the serial monitor once per second so you can verify the board, USB connection, and serial settings are working: C++ void setup() { Serial.begin(9600); // Give the serial port a moment to initialize delay(1000); Serial.println("Arduino Mega 2560 serial test"); Serial.println("Connection successful!"); } void loop() { Serial.println("Hello from Arduino Mega!"); delay(1000); } To test: Upload the sketch. Open Tools → Serial Monitor in the Arduino IDE. Set the baud rate to 9600. You should see: Arduino Mega 2560 serial test Connection successful! Hello from Arduino Mega! Hello from Arduino Mega! ... If you want a slightly more useful test that also counts seconds since boot: C++ void setup() { Serial.begin(9600); delay(1000); Serial.println("Arduino Mega 2560 started"); } void loop() { Serial.print("Uptime (ms): "); Serial.println(millis()); delay(1000); } This confirms that the microcontroller is actually running code and not just resetting repeatedly. --- user Good. Now add adafruit sd card reader says 5v ready pins are CD CS DI DO CLK GND 3v 5v --- assistant Assuming your SD card module is a typical SPI-based Adafruit-compatible module with onboard level shifting and regulator ("5V ready"), connect it to the Arduino Mega 2560's hardware SPI pins: SD Module Arduino Mega 2560 CS D53 DI (MOSI) D51 DO (MISO) D50 CLK (SCK) D52 GND GND 5V 5V 3V Not connected CD Optional (see below) The CD (Card Detect) pin is optional. If you want to detect card insertion/removal, connect it to a free digital pin, for example D2. Test Sketch This sketch: Initializes Serial. Initializes the SD card. Lists files in the root directory. C++ #include #include const int SD_CS = 53; void printDirectory(File dir, int numTabs) { while (true) { File entry = dir.openNextFile(); if (!entry) { break; } for (uint8_t i = 0; i < numTabs; i++) { Serial.print('\t'); } Serial.print(entry.name()); if (entry.isDirectory()) { Serial.println("/"); printDirectory(entry, numTabs + 1); } else { Serial.print("\t"); Serial.println(entry.size()); } entry.close(); } } void setup() { Serial.begin(9600); delay(1000); Serial.println("SD Card Test"); if (!SD.begin(SD_CS)) { Serial.println("SD initialization failed!"); Serial.println("Check wiring and card format (FAT32)."); return; } Serial.println("SD initialization successful."); File root = SD.open("/"); Serial.println("Files on card:"); printDirectory(root, 0); root.close(); } void loop() { } Expected Serial output: SD Card Test SD initialization successful. Files on card: TEST.TXT 123 DATA.CSV 4567 LOGS/ If it says "SD initialization failed!", tell me: The exact model number printed on the SD module. Whether the card is microSD or full-size SD. The card size (e.g. 8GB, 32GB, 64GB). I can then help diagnose the wiring or formatting issue. --- assistant Good — the CD (Card Detect) → D2 line is perfect for this. We’ll treat CD as: LOW = card inserted (common on most modules, but we’ll verify in code) HIGH = no card We’ll: Monitor state changes Show OLED messages for 1 second on transitions Avoid blocking SD operations unless card is present OLED SD Card Status Monitor (with CD pin) This version: Shows "No SD card" Shows "Card inserted" for 1s Shows "Card removed" for 1s Then returns to idle state Code C++ #include #include #include #define SCREEN_WIDTH 96 #define SCREEN_HEIGHT 16 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Card detect pin const int CD_PIN = 2; // Track state bool lastCardState = false; void showMessage(const char* msg) { display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 4); display.println(msg); display.display(); } bool isCardInserted() { // Most SD modules pull CD LOW when card is present return digitalRead(CD_PIN) == LOW; } void setup() { pinMode(CD_PIN, INPUT_PULLUP); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); showMessage("Checking SD..."); delay(1000); lastCardState = isCardInserted(); if (lastCardState) { showMessage("Card inserted"); } else { showMessage("No SD card"); } delay(1000); } void loop() { bool current = isCardInserted(); if (current != lastCardState) { if (current) { showMessage("Card inserted"); delay(1000); } else { showMessage("Card removed"); delay(1000); } lastCardState = current; } // idle state (optional minimal refresh) if (current) { showMessage("Card ready"); } else { showMessage("No SD card"); } delay(300); } Expected behaviour On boot: Checking SD... Card inserted (or No SD card) When inserting card: Card inserted (1 second) Card ready When removing card: Card removed (1 second) No SD card Important hardware note (very important) If CD behaves “backwards”, just flip this line: C++ return digitalRead(CD_PIN) == LOW; to: C++ return digitalRead(CD_PIN) == HIGH; Different SD modules wire CD differently. Next upgrade (recommended) Now that card detection works cleanly, the next logical step is: 🔥 “Auto-mount file browser” When card inserted → scan SD When removed → clear file list Keep OLED always showing current state Then: integrate your UP/DOWN/SELECT menu only allow browsing when card is present If you want, I can merge everything into a single robust SD file browser + card hot-swap handler + keypad UI + OLED renderer (basically a mini firmware stack). --- user It's the opposite. CD. Could you got back to showing the file system and allowing selection of files? Add a tick to the selected file or something to show which is selected --- assistant Good — we’ll fix CD polarity and rebuild the full UI cleanly. You now want a simple SD file browser with selection state, plus a visual “tick” on the selected file. We’ll combine everything: SD card listing (.GCO, .GCODE) OLED 96×16 (2-line viewport) 3 buttons (UP / DOWN / SELECT) CD pin (now inverted) Selection marker (✓) Serial output on selection Key behaviour Display (2-line viewport): > cube.gco benchy.gco After selecting: ✓ cube.gco benchy.gco (✓ stays on selected file) Final integrated firmware C++ #include #include #include #include #include #define SCREEN_WIDTH 96 #define SCREEN_HEIGHT 16 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // ---------------- SD ---------------- const int SD_CS = 53; const int CD_PIN = 2; // inverted polarity // ---------------- Buttons ---------------- const int R1 = 23; // UP const int R2 = 24; // DOWN const int R3 = 25; // SELECT const int C1 = 26; // ---------------- File storage ---------------- char files[50][32]; int fileCount = 0; // ---------------- UI state ---------------- int cursor = 0; int scroll = 0; int selected = -1; // ---------------- CD (inverted!) ---------------- bool cardInserted() { return digitalRead(CD_PIN) == HIGH; } // ---------------- SD scan ---------------- void scanSD() { fileCount = 0; File root = SD.open("/"); if (!root) return; while (true) { File entry = root.openNextFile(); if (!entry) break; if (!entry.isDirectory()) { String name = entry.name(); if (name.endsWith(".GCO") || name.endsWith(".GCODE")) { if (fileCount < 50) { name.toCharArray(files[fileCount], 32); fileCount++; } } } entry.close(); } root.close(); } // ---------------- OLED render ---------------- void drawMenu() { display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); for (int i = 0; i < 2; i++) { int idx = scroll + i; if (idx >= fileCount) break; display.setCursor(0, i * 8); // selection marker if (idx == selected) { display.print("✓"); } else if (idx == cursor) { display.print(">"); } else { display.print(" "); } String name = files[idx]; if (name.length() > 12) name = name.substring(0, 12); display.print(name); } display.display(); } // ---------------- Button scan ---------------- int readButton() { digitalWrite(C1, LOW); delayMicroseconds(5); if (digitalRead(R1) == LOW) return 1; // UP if (digitalRead(R2) == LOW) return 2; // DOWN if (digitalRead(R3) == LOW) return 3; // SELECT return 0; } void waitRelease() { while (readButton() != 0) delay(10); } // ---------------- Setup ---------------- void setup() { Serial.begin(115200); pinMode(R1, INPUT_PULLUP); pinMode(R2, INPUT_PULLUP); pinMode(R3, INPUT_PULLUP); pinMode(C1, OUTPUT); digitalWrite(C1, HIGH); pinMode(CD_PIN, INPUT_PULLUP); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.println("SD Browser"); display.display(); delay(800); if (cardInserted()) { SD.begin(SD_CS); scanSD(); } drawMenu(); } // ---------------- Loop ---------------- void loop() { // If card removed, do nothing (you can expand later) if (!cardInserted()) { display.clearDisplay(); display.setCursor(0, 0); display.println("NO SD CARD"); display.display(); delay(200); return; } int btn = readButton(); if (btn == 0) return; if (btn == 1) { // UP if (cursor > 0) cursor--; if (cursor < scroll) scroll = cursor; } if (btn == 2) { // DOWN if (cursor < fileCount - 1) cursor++; if (cursor > scroll + 1) scroll = cursor - 1; } if (btn == 3) { // SELECT selected = cursor; Serial.print("Selected file: "); Serial.println(files[selected]); } waitRelease(); drawMenu(); } What you now have OLED UI: Cursor (>) moves Selection mark (✓) stays on chosen file 2-line scrolling viewport Serial: Selected file: CUBE.GCO Important behaviour decisions I made (so you understand the system) 1. Selection is persistent selected is separate from cursor allows browsing after selecting 2. Scroll logic is “windowed” OLED shows only 2 files scroll follows cursor 3. CD pin inverted HIGH = inserted Next upgrade paths (this is where it gets interesting) You are now at the point where this becomes a real controller UI. Next steps you might want: 🚀 1. Execute file (G-code streaming) Open selected file Send line-by-line over Serial Add pause/resume 🚀 2. Long filename support Proper LFN instead of 8.3 names 🚀 3. UI polish blinking cursor invert selected row paging (fast scroll) 🚀 4. “Print screen mode” progress bar (even on 96×16 you can fake it) elapsed time current line number If you tell me your end goal (3D printer controller, CNC sender, file launcher, etc.), I can evolve this into a full embedded firmware architecture instead of just a menu. --- user tick looks like (pi)(pound)(o with accent) There's no "SD removed...", just goes direct to "No SD CARD". I wanna show when removed and inserted. When inserted, it doesn't change automatically. --- assistant You’re hitting two separate issues: OLED “tick” is wrong glyph (font limitation) Card detect + SD init is not event-driven (so no insert/remove transitions) We’ll fix both properly. 1) Fix the “tick” character On SSD1306 96×16 with Adafruit_GFX default font: You do NOT have Unicode So ✓ becomes garbage like (π)(£)(o´) Solution: use ASCII-safe symbols Use one of these instead: * (best readability) > / ] # inverted block style via char 127 (sometimes works, unreliable) Recommendation: Replace: C++ "✓" with: C++ "*" or better UI: Selected file: * Cursor: > Example: * cube.gco (selected) > benchy.gco (cursor) 2) Fix SD insert/remove detection (your real problem) Right now your firmware does this: checks CD once at boot never re-initializes SD properly no state tracking → no insert/remove events Also: “When inserted, it doesn't change automatically” That’s because: SD.begin() is only called once and you are not rescanning after insert 3) Correct architecture (important) You need a state machine: States: NO_CARD CARD_INSERTED CARD_REMOVED_EVENT Rules: detect change in CD pin on insert → init SD + scan files + show message 1s on remove → clear file list + show message 1s 4) Fixed working firmware (insert + remove events + rescan) This replaces your loop logic properly: C++ #include #include #include #include #include #define SCREEN_WIDTH 96 #define SCREEN_HEIGHT 16 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // ---------------- PINS ---------------- const int SD_CS = 53; const int CD_PIN = 2; // Buttons const int R1 = 23; const int R2 = 24; const int R3 = 25; const int C1 = 26; // ---------------- FILES ---------------- char files[50][32]; int fileCount = 0; // ---------------- UI ---------------- int cursor = 0; int scroll = 0; int selected = -1; // ---------------- CARD STATE ---------------- bool lastCardState = false; // CD is inverted in your module bool cardInserted() { return digitalRead(CD_PIN) == HIGH; } // ---------------- OLED ---------------- void msg(const char* text) { display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 4); display.println(text); display.display(); } void drawMenu() { display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); for (int i = 0; i < 2; i++) { int idx = scroll + i; if (idx >= fileCount) break; display.setCursor(0, i * 8); if (idx == selected) display.print("*"); // selected else if (idx == cursor) display.print(">"); // cursor else display.print(" "); String name = files[idx]; if (name.length() > 12) name = name.substring(0, 12); display.print(name); } display.display(); } // ---------------- SD SCAN ---------------- void scanSD() { fileCount = 0; File root = SD.open("/"); if (!root) return; while (true) { File entry = root.openNextFile(); if (!entry) break; if (!entry.isDirectory()) { String name = entry.name(); if (name.endsWith(".GCO") || name.endsWith(".GCODE")) { if (fileCount < 50) { name.toCharArray(files[fileCount], 32); fileCount++; } } } entry.close(); } root.close(); } // ---------------- BUTTONS ---------------- int readButton() { digitalWrite(C1, LOW); delayMicroseconds(5); if (digitalRead(R1) == LOW) return 1; if (digitalRead(R2) == LOW) return 2; if (digitalRead(R3) == LOW) return 3; return 0; } void waitRelease() { while (readButton() != 0) delay(10); } // ---------------- SETUP ---------------- void setup() { Serial.begin(115200); pinMode(R1, INPUT_PULLUP); pinMode(R2, INPUT_PULLUP); pinMode(R3, INPUT_PULLUP); pinMode(C1, OUTPUT); digitalWrite(C1, HIGH); pinMode(CD_PIN, INPUT_PULLUP); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); lastCardState = cardInserted(); if (lastCardState) { msg("SD inserted"); SD.begin(SD_CS); scanSD(); delay(1000); } else { msg("No SD card"); delay(1000); } drawMenu(); } // ---------------- LOOP ---------------- void loop() { bool current = cardInserted(); // ---------------- INSERT EVENT ---------------- if (current && !lastCardState) { msg("Card inserted"); delay(1000); if (SD.begin(SD_CS)) { scanSD(); cursor = 0; scroll = 0; } drawMenu(); } // ---------------- REMOVE EVENT ---------------- if (!current && lastCardState) { msg("Card removed"); delay(1000); fileCount = 0; cursor = 0; scroll = 0; selected = -1; } lastCardState = current; // ---------------- NO CARD ---------------- if (!current) { msg("NO SD CARD"); delay(200); return; } // ---------------- BUTTONS ---------------- int btn = readButton(); if (btn == 0) return; if (btn == 1 && cursor > 0) cursor--; if (btn == 2 && cursor < fileCount - 1) cursor++; if (btn == 3) { selected = cursor; Serial.print("Selected: "); Serial.println(files[selected]); } if (cursor < scroll) scroll = cursor; if (cursor > scroll + 1) scroll = cursor - 1; waitRelease(); drawMenu(); } 5) What is now fixed ✔ Proper insert detection shows “Card inserted” for 1s rescans SD automatically ✔ Proper removal detection shows “Card removed” for 1s clears file list ✔ OLED symbols fixed * = selected file > = cursor ✔ SD reinitialisation handled correctly 6) Important behavioural note (why your previous version failed) SD cards do NOT hot-swap cleanly unless you: re-run SD.begin() re-open root directory clear cached file state Your previous code only checked CD, but never rebuilt internal state. If you want next upgrade You’re now one step away from turning this into: 🚀 real controller firmware: file preview (first G-code line) streaming to serial (printer control) pause/resume state machine progress bar on 96×16 OLED Just tell me your end goal (3D printer / CNC / generic file launcher).