18.Arduino learning notebook-- SD card reading and writing experiment

18.Arduino learning notebook-- SD card reading and writing experiment

         Juvtmall ( a company supply PCB PrototypingPCBA service and sell kinds of components, modules and so on) ( Catduino has the same function of Arduino) 


This time I use arduino to drive SD card, read and write in SD card.
It is necessary to note that the SD library file of arduino is not very good for 2G or over 2G, so it is recommended that you use 2G or below 2G, and the file format is in FAT.
This time I use the MicroSD card of kingmax2G.



Look at the hardware connection diagram





Download the following code into arduino board
ARDUINO CODE

1. / *
2. This example shows that if you record three analog pin values to a SD card, use the SD library.
3.          
4. circuit part
5. * analog pins with simulated port 0,1, 2. You can access analog sensors according to the situation.
6. If not, you can also do this experiment, but the numerical value is a constant beating source.
7. * SD card and arduino control board are connected by SPI bus.
8. ** MOSI - pin 11
9. ** MISO - pin 12
10. ** CLK - pin 13
11. ** CS - pin 4
12. */
13. 
14. #include <SD.h>
15. 
16. // CS pin is pin4, and the official default setting is also used here
17. const int chipSelect = 4;// set the CS interface
18. 
19. void setup ()
20. {
21. serial. The begin (9600);// set serial port communication baud rate of 9600
22. serial. Print (" the Initializing SD card...");// a serial port output data Initializing SD card...
23. pinMode (10, the OUTPUT);
24. 
25. 
26. if (!D. Begin (chipSelect) {/ / if you fail to communicate with SD Card from the CS port, the serial port output Card failed, or not present
27. serial. Println ("Card failed, or not present");
28.  return;
29. }
30. serial. Println (" card the initialized. ");// successful communication with SD card, serial port output card initialized.
31. }
32. 
33. void loop ()
34. {
35. // define an array
36. string dataString = "";
37. 
38. // read three sensor values to write an array
39. for (int) = 0;AnalogPin < 3;AnalogPin++) {
40. int sensor = analogRead(analogPin);
41. dataString += String(sensor);
42. If (s)
43. dataString += ",";
44. }
45. }
46. 
47. // open the file, note that only one file can be opened at the same time
48. // if you want to open another file, you need to close the first one
49. File dataFile = SD. Open ("datalog. TXT ", FILE_WRITE);
50. 
51. // open the datalog. TXT file, read and write state, at the end of the file.
52. If (dataFile) {
53. DataFile. Println (dataString);
54. DataFile. The close ();
55. // the array dataString outputs to the serial port
56. Serial. Println (dataString);
57. }
58. // if you cannot open the file, the serial port outputs the error information:error opening datalog. TXT
59.   else {
60. Serial. Println ("error opening datalog. TXT ");
61.  }
62. }

When the download is complete, open the serial port monitor.




The following screen will be seen after the control panel starts to work, on each line in the figure is each record of three sensors values. In this experiment, I did not connect sensor, so the value is the clutter signal of the interference sources. 



Turn off the arduino power, then take out the SD card, and access to a computer, that you can see DATALOG. TXT file in the SD card, open it you can see that the information in this document is to record the value of each sensor.




After the first code experiment finished, DATALOG.TXT in the SD card is not deleted. Download the following code into the arduino and open the serial monitor to see the effect.

ARDUINO CODE

1. / *
2. This example shows how to read a file from the SD card through the SD library and send the value through a serial port.
3. 
4. * SD card and arduino control board are connected by SPI bus.
5. ** mosi-pin 11
6. ** MISO - pin 12
7. ** clk-pin 13
8. ** cs-pin 4
9. 
10. * /
11. 
12. # include < SD. H >
13. 
14. // CS pin is pin4, and the official default setting is also used here
15. const int chipSelect = 4;// set the CS interface
16. 
17. void setup ()
18. {
19. Serial. The begin (9600);// set serial port communication baud rate of 9600
20. Serial. Print (" the Initializing SD card...");// a serial port output data Initializing SD card...
21. pinMode (10, the OUTPUT);
22. 
23. if (!D. Begin (chipSelect) {/ / if you fail to communicate with SD Card from the CS port, the serial port output Card failed, or not present
24. Serial. Println ("Card failed, or not present");
25. // don't do anything more:
26. return;
27. }
28. Serial. Println (" card the initialized. ");// successful communication with SD card, serial port output card initialized.
29. 
30. File dataFile = SD. Open ("datalog. TXT ");// open the datalog. TXT file
31. 
32. if (dataFile) {
33. while (dataFile. Available ()) {/ check whether dataFile has data or not
34. Serial. Write (dataFile. The read ());// if data is available, the data should be sent to the serial port
35. }
36. DataFile. The close ();/ / close the dataFile
37. }
38. 
39.  else {
40. Serial. Println ("error opening datalog. txt ");// if the file cannot open a serial port, send information:error opening datalog. txt
41.  }
42. }
43. 
44. Void loop ()
45. {
46. }


As you can see, the data in the datalog. TXT file is sent back to the computer via a serial port.The effect is as follows:



Download the following code into the arduino again and open the serial monitor to see the effect.
1. / *
2. 
3. This example shows how to create a deleted file in SD card
4. * SD card and arduino control board are connected by SPI bus
5. ** mosi-pin 11
6. ** MISO - pin 12
7. ** clk-pin 13
8. ** cs-pin 4
9.          
10. * /
11. # include < SD. H >
12. 
13. File myFile.
14. 
15. void setup ()
16. {
17. Serial. The begin (9600);// set serial port communication baud rate of 9600
18. Serial. Print (" the Initializing SD card...");// a serial port output data Initializing SD card...
19. PinMode (10, the OUTPUT);
20. 
21. if (!D. Begin (4)) {/ / if communication failed from CS port and SD card, the serial port output information initialization failed!
22. Serial. Println (" initialization failed!");
23. return;
24. }
25. Serial. Println (" initialization done. ");//CS port and SD card communication successfully, serial port output information initialization done.
26. 
27. If (SD. Exists ("example.txt")) {/ / check whether example.txt file exists or not
28. Serial. Println (" example. TXT exists. ");// if exists, output information: example.txt exists to serial port
29. }
30. else {
31. Serial.Println ("example.txt doesn't exist ");// If doesn’t exist, output information :example.txt doesn't exist to serial port
32. }
33. 
34. Serial. Println (" Creating example. TXT...");
35. myFile = SD. Open ("example.txt", FILE_WRITE);// open example.txt, if there is no auto-create, write the state
36. myFile. Close ();// close the file
37. 
38. if (SD) exists (" example. TXT ")) {
39. Serial. Println (" example. TXT exists. ");
40. }
41. The else {
42. Println ("example.txt doesn't exist ");
43. }
44. 
45. Serial. Println (" o example. TXT...");
46. SD. Remove (" example. TXT ");// delete example.txt
47. 
48. If (SD) exists (" example. TXT ")) {
49. Serial. Println (" example. TXT exists. ");
50. }
51. The else {
52. Println ("example.txt doesn't exist ");
53. }
54. }
55. 
56. Void loop ()
57. {
58. 
59. }



The following graph is the result of the serial port monitor display, which shows that the file was created in the deleted process.




Upload the following code into the arduino board, to see the effect.
ARDUINO CODE
1. #include <SD.h>
2. 
3. File myFile;
4. 
5. void setup()
6. {
7.   Serial.begin(9600);
8.   Serial.print("Initializing SD card...");
9.    pinMode(10, OUTPUT);
10. 
11.   if (!SD.begin(4)) {
12.     Serial.println("initialization failed!");
13.     return;
14.   }
15.   Serial.println("initialization done.");
16. 
17.   myFile = SD.open("test.txt", FILE_WRITE);
18. 
19. 
20.   if (myFile) {
21.   Serial.print("Writing to test.txt...");
22.     myFile.println("testing 1, 2, 3.");
23. 
24.     myFile.close();
25.     Serial.println("done.");
26.   } else {
27.     Serial.println("error opening test.txt");
28.   }
29. 
30.   myFile = SD.open("test.txt");
31.   if (myFile) {
32.     Serial.println("test.txt:");
33. 
34.     while (myFile.available()) {
35.             Serial.write(myFile.read());
36.     }
37. 
38.     myFile.close();
39.   } else {
40. 
41.     Serial.println("error opening test.txt");
42.   }
43. }
44. 
45. void loop()
46. {
47. 
48. }



The following screen can be seen in the serial port monitor




Plug the SD card into the computer, and you can find the test.txt file.  The contents are as follows.




The other Blog

mail:michelle1273110435@gmail.com



评论

此博客中的热门博文

1. Arduino learning notebook -- Button experience (volume 2)

1. Arduino learning notebook -- Button experience (volume 1)

19.Arduino learning notebook--Atmega8 makes the minimum arduino system