HELLO THERE!!!!
How are you,guys? I hope everyone is fine and in good mood always. Last week, I had shown you guys on how to write sensor data to a text file in a SD Card. This week, I will be showing you guys on how to read the data that is stored in the text file in the SD Card. Without wasting time, let's get into the experiment.
HARDWARE:
3.Micro SD card.
CODING THE ARDUINO:
The code to read the text file in the SD card is shown below:
#include <SPI.h>
#include <SD.h>
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
File dataFile = SD.open("bgsound.txt", FILE_READ);//open the file for reading purpose.
//change the file name according to the file in the sd card
while (dataFile.available()) {//if data is available
Serial.write(dataFile.read());//read the value
}
}
Upload the Arduino code and open the serial monitor. All the data in the text file will be shown in the serial monitor. Some example of the output is shown in the image below:
How are you,guys? I hope everyone is fine and in good mood always. Last week, I had shown you guys on how to write sensor data to a text file in a SD Card. This week, I will be showing you guys on how to read the data that is stored in the text file in the SD Card. Without wasting time, let's get into the experiment.
HARDWARE:
1.Arduino UNO
2.Ethernet Shield/Micro SD shield/ Any board with Micro SD slot ( I used the ethernet shield for this weekend)3.Micro SD card.
SOFTWARE:
1.Arduino IDE
CIRCUIT ASSEMBLY:
The circuit assembly is fairly simple. Just plug in the Ethernet shield on top the Arduino board and insert the micro SD card into the shield.
BEFORE CODING:
Make sure there is a text file in the SD card name bgsound (or any name that you like actually).
CODING THE ARDUINO:
The code to read the text file in the SD card is shown below:
#include <SPI.h>
#include <SD.h>
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
File dataFile = SD.open("bgsound.txt", FILE_READ);//open the file for reading purpose.
//change the file name according to the file in the sd card
while (dataFile.available()) {//if data is available
Serial.write(dataFile.read());//read the value
}
}
Upload the Arduino code and open the serial monitor. All the data in the text file will be shown in the serial monitor. Some example of the output is shown in the image below:
Well, that's all for this week guys!! I hope to see you guys next week and till then, HAPPY TINKERING!!!
Comments
Post a Comment