Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Here's an example on how to use a Micro SD card module with a Wemos D1 Mini (ESP8266) to read and write data to a text file. This example uses the SD and SPI libraries that come with the Arduino IDE.
- Connect the Micro SD card module to your Wemos D1 Mini as follows:
- VCC to 3.3V
- GND to GND
- MISO to D6
- MOSI to D7
- SCK to D5
- CS to D8
- Upload the complete code to your Wemos D1 Mini and open the Serial Monitor. The program will create a file called "test.txt",
- */
- #include <ESP8266WiFi.h>
- #include <SPI.h>
- #include <SD.h>
- #define CS_PIN D8
- void setup() {
- Serial.begin(115200);
- Serial.println("Initializing SD card...");
- if (!SD.begin(CS_PIN)) {
- Serial.println("Initialization failed!");
- return;
- }
- Serial.println("Initialization successful.");
- }
- void loop() {
- File dataFile = SD.open("test.txt", FILE_WRITE);
- if (dataFile) {
- dataFile.println("Hello, world!");
- dataFile.close();
- Serial.println("Data written to test.txt");
- } else {
- Serial.println("Failed to create test.txt");
- }
- dataFile = SD.open("test.txt");
- if (dataFile) {
- Serial.println("Reading data from test.txt:");
- while (dataFile.available()) {
- Serial.write(dataFile.read());
- }
- dataFile.close();
- } else {
- Serial.println("Failed to open test.txt for reading");
- }
- if (SD.remove("test.txt")) {
- Serial.println("test.txt has been deleted");
- } else {
- Serial.println("Failed to delete test.txt");
- }
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement