Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

MicroSD 2587

Hi I am interested on item 2587. I want to use it, but I don’t find any documentation about the circuit that has to exist for the communication with an arduino uno. I would like to save on it all that I print in the serial monitor (I am using an arduino uno to drive a servo basing on the inclination of an IMU). Can you send me some good links, please?

Hello.

You should not need extra circuitry, since the #2587 has built in voltage dividers. I recommend using the Arduino SD library, which has several helpful examples. The communication between the SD card and the Arduino Uno is done through SPI, and the connections are mentioned in the SD Library documentation and comments of the example codes.

SCLK on the MicroSD breakout board should go to SCK (pin 13) on the Arduino Uno.
DO on the MicroSD breakout board should go to MISO (pin 12) on the Arduino Uno.
DI on the MicroSD breakout board should go to MOSI (pin 11) on the Arduino Uno.
nCS on the MicroSD breakout board depends on your code, but pin 10 is what the SD Library examples use.

Along with those SPI connections, you will need to supply the VDD pin on the board with 5V to power it, and have a common ground connection.

The CD pin can optionally be used as an input to the Arduino to detect whether there is a card inserted. When a card is inserted, it is high; when no card is inserted, it is shorted to ground.

Brandon

Hi @BrandonM I used your setup, paying attention to the connection and the code but it doesn’t work. Where could be the problem? the output is a failed initialization. I tried more microsd card ( in some cases I also formatted them) but the result is always the same
I am using an artduino uno wifi rev 2 and the wiring is:

  • gnd-gnd
  • vcc-5V
  • di-11
  • do-12
  • sclk-13
  • cs-10

and the code is

#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// 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("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("This is a test file :)");
myFile.println("testing 1, 2, 3.");
for (int i = 0; i < 20; i++) {
myFile.println(i);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}

but it doesn’t work.
Btw the goal for me is to use it with a portenta h7 where the SPI connection is with pin 7,8,9,10. Can you confirm it, please?
PS: I supposed that with my arduino it can work well.


Thank you for the additional information. It looks like you are using an Arduino WiFi Rev 2 board, which is different than a normal Arduino Uno. Could you try connecting to the ISP pins on the 2x3 ICSP header instead of pins 11-13? You can refer to this pinout diagram of that header for reference:

So, SCLK should connect to ICSP pin 3, DO to ICSP pin 1, and DI to COPI. You can leave the other connections how they were.

By the way, the library only supports FAT16 or FAT32 file systems, so if you haven’t formatted your card to use one of those, that might also be a problem. I recommend reading through the notes on the library here, which also goes through how to format the card properly. Also, as far as code, I recommend sticking to the examples for now; particularly, the Card Info example, as that one is a good way to confirm that the communication is working.

I am not very familiar with the Portenta H7, but unlike the Arduino Uno, it looks like it operates at 3.3V. For 3.3V applications, we carry a smaller Breakout Board for MicroSD Card without the 3.3 V regulator, level shifters, and mounting holes. As far as the connections, it looks like Arduino’s documentation lists pins 7, 8, 9, and 10 as SPI pins CS, COPI, CK, and CIPO, respectively.

Brandon

Hi @BrandonM I hope you spent nice holidays.
Thank you so much with the connection you said me the board find the card. By the way, after reading notes, I formatted my sd. The problem now is that it didn’t find (in CardInfo.ino) the right partition even if it is a default characteristics of my sd. Do you have some tip for me?


CardInfo

You might double check that you are using the latest version of the SD library (you can update it from the library manager within the Arduino IDE).

I have also seen people claiming that switching to this SdFat library, which has a similar SdInfo example, solved the problem.

If you have any other micro SD cards you could try (preferably lower capacity like 2GB), that would be a good test too.

Brandon

Hi Brandon and thank you so much. I finally figure out with SD library. Right now I have another problem unfortunately. I hope that you kindly can explain me and help me to solve it. If I create a file named “salva.txt”, the output in the serial monitor is repeated only once (right), but if I later go to the saved file the output over there is written twice. I found other problems like this but no solution.

#include <SPI.h>
#include <SD.h>
File myFile;
int CS_PIN = 10;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// 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("salva.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("This is a test file :)");
myFile.println("testing 1, 2, 3.");
for (int i = 0; i < 20; i++) {
myFile.println(i);
Serial.println(i);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}

salva.txt:
This is a test file :slight_smile:
testing 1, 2, 3.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This is a test file :slight_smile:
testing 1, 2, 3.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

The Arduino Uno boards reset when you open the Serial Monitor, so what I suspect is happening is that when you connect/power your Arduino, it writes to the SD card, then when you open the Serial Monitor, it writes everything again. You can test this by adding while(!Serial); after your Serial.begin(9600); command to make the code only progress once you open the Serial Monitor. (correction in post below)

Also, please note that the file will just be added onto if you are not manually erasing it after each time you run the program.

Brandon

Hello, again.

Actually, another engineer here pointed out that while(!Serial); will probably not work with the Arduino Uno the way it works with the Leonardo (and our A-Star boards), but I still suspect resetting when the Serial Monitor starts up is what you are running into. Probably the easiest way to test this would be to add a line of code to turn on the user LED and indicate when the program is done writing to the microSD card. Then you can just power it up, wait for the LED to come on, and power it down to eject the microSD card and check what was written to it (without ever opening the Serial Monitor).

Brandon

Thank you for your disponibility and your kindness. I confirm your problem’s theory. I am thinking that, in order to have a debug in the serial monitor, I shall open it and drive with a remote control the initialization and writing of the sd. In this way, at the first power on the sd card shall still waiting for a command.

Matteo

1 Like