Problem on using SPI Extended Mode on NodeMCU connecting an SD Card Module
- Dominant language
- C++
- Stars
- 16.7k
- Forks
- 13.1k
- PR merge metrics
- No merged PRs in 30d
Description
Hello! I am trying to connect a SD card Module to the NodeMCU V1.0 from AMICA by using pins 6, 7 ,8 ,0. In the documentation I found this paragraph that mentions it is possible:
"
SPI library supports the entire Arduino SPI API including transactions, including setting phase (CPHA). Setting the Clock polarity (CPOL) is not supported, yet (SPI_MODE2 and SPI_MODE3 not working).
The usual SPI pins are:
MOSI = GPIO13
MISO = GPIO12
SCLK = GPIO14
There’s an extended mode where you can swap the normal pins to the SPI0 hardware pins. This is enabled by calling SPI.pins(6, 7, 8, 0) before the call to SPI.begin(). The pins would change to:
MOSI = SD1
MISO = SD0
SCLK = CLK
HWCS = GPIO0
This mode shares the SPI pins with the controller that reads the program code from flash and is controlled by a hardware arbiter (the flash has always higher priority). For this mode the CS will be controlled by hardware as you can’t handle the CS line with a GPIO, you never actually know when the arbiter is going to grant you access to the bus so you must let it handle CS automatically. "
I have tried but cannot initialize the SD Card. I have seen in other posts that the Chip Select should be controlled by hardware and included SPI.setHwCs(true); command but it still does not work perhaps because SD.begin(chipselect) assigns CS to pin 0.
Does anyone have an idea on how to achieve SD Card Module implementation on the NodeMCU by using pins 6, 7 ,8 ,0? I cannot use the normal 14,12,13,15 pins because I need to implement an I2C connection to Arduino UNO (so pins 4,5 are used), a Softwareserial communication(so I need 2 free pins) and the SPI communication for the SD card module. Is there a solution to this problem if using the extended mode won't work?
Is there a way to connect the Arduino UNO with NodeMCU other than Softwareserial?
The code is:
#include //lib for I2C
#include //lib for display
#include //lib for display
#include //lib for RTC
#include //lib for SPI comm(SD Card)
#include //lib for SD Card
const int chipSelect = 0;
bool flag_for_SD_Write_TEST=false;
bool century=false;
bool h12Flag=false;//will always be false bc the format I chose is 24h (was chosen when the clock mode was set and will always remain false)
bool pmFlag;
#define SCREEN_WIDTH 128 // display display width, in pixels
#define SCREEN_HEIGHT 64 // display display height, in pixels
#define display_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, display_RESET);//create instance of type...
DS3231 myclock;
File myFile;
void setup() {
//SD Card
SPI.pins(6, 7, 8, 0);
SPI.begin();
SPI.setHwCs(true);
delay(1000);
Serial.begin(115200);
Serial.println("PROGRAM STARTED");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
//return;
} else
Serial.println("card initialized.");
//Display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C on I2C Bus
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Hello, world!");
display.display();
}
void loop() {
display.clearDisplay();
display.setCursor(0, 10);
display.print(myclock.getYear(),DEC);
display.print("-");
display.print(myclock.getMonth(century), DEC);
display.print("-");
display.print(myclock.getDate(), DEC);
display.print(" ");
display.print(myclock.getHour(h12Flag, pmFlag), DEC); //24-hr
display.print(":");
display.print(myclock.getMinute(), DEC);
display.print(":");
display.println(myclock.getSecond(), DEC);
display.display();
delay(1000);
if(flag_for_SD_Write_TEST==false){
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("time_new.txt", FILE_WRITE);
for (int i=0;i<5;i++){
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to time.txt...");
myFile.println("testing 1, 2, 3.");
myFile.print(myclock.getYear(),DEC);
myFile.print("-");
myFile.print(myclock.getMonth(century), DEC);
myFile.print("-");
myFile.print(myclock.getDate(), DEC);
myFile.print(" ");
myFile.print(myclock.getHour(h12Flag, pmFlag), DEC); //24-hr
myFile.print(":");
myFile.print(myclock.getMinute(), DEC);
myFile.print(":");
myFile.println(myclock.getSecond(), DEC);
delay(1000);
} else {
// if the file didn't open, print an error:
Serial.println("error opening time.txt");
}
}
// close the file:
myFile.close();
Serial.println("done.");
}
flag_for_SD_Write_TEST=true;
}
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.