In most of our work so far, we have just watched our data go by on the Serial Monitor. In most cases, you will want to have some means to store your data. The easiest way to do this is to use a simple SD card reader. For this example, we use the Virtuabotix SD Card Reader.
In this tutorial, we will need to have some sensor hooked up so we will have some data to store. We will be using the BMP 180 Pressuer and Temperature sensor from adafruit. We have a complete tutorial on this sensor HERE. You will need to go to that lesson and get the sensor hooked up, the library installed, and the software done. All this is explained step-by-step in the LESSON.
The BMP180 is connected to the arduino as follows:
Connecting Up the BMP180 Pressure and Temperature Sensor | |
BMP180 Pin | Arduino Pin |
Vin | 5V |
GND | GND |
SCL | A5 |
SDA | A4 |
Once you have the BMP180 connected, test and make sure your code is working, and you are getting good pressure and temperature readings. Once that is working, you are ready to connect your SD card Reader/Writer.
The SD card reader should be connected as follows:
Connecting the SD Card Reader | ||
Sd Card Reader Pin | Arduino Pin | Details |
GND | GND | Common Ground |
3.3 V – (NOT USED) | ||
+5 | 5V | Power |
CS | 4 | Chip Select |
MOSI | 11 | SPI Data |
SCK | 13 | Clock |
MISO | 12 | SPI Data |
GND | GND | Common Ground |
In the video we will show step-by-step how to develop the software. You should follow along in the video, and not copy and paste the code below. You will never learn to program if you do not write your own code. The code below is to help you in case you get stuck.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <SD.h> //Load SD card library #include<SPI.h> //Load SPI Library #include "Wire.h" // imports the wire library for talking over I2C #include "Adafruit_BMP085.h" // import the Pressure Sensor Library We are using Version one of Adafruit API for this sensor Adafruit_BMP085 mySensor; // create sensor object called mySensor float tempC; // Variable for holding temp in C float tempF; // Variable for holding temp in F float pressure; //Variable for holding pressure reading int chipSelect = 4; //chipSelect pin for the SD card Reader File mySensorData; //Data object you will write your sesnor data to void setup(){ Serial.begin(9600); //turn on serial monitor mySensor.begin(); //initialize pressure sensor mySensor pinMode(10, OUTPUT); //Must declare 10 an output and reserve it SD.begin(4); //Initialize the SD card reader } void loop() { tempC = mySensor.readTemperature(); // Read Temperature from BMP180 tempF = tempC*1.8 + 32.; // Convert degrees C to F pressure=mySensor.readPressure(); //Read Pressure mySensorData = SD.open("PTData.txt", FILE_WRITE); if (mySensorData) { Serial.print("The Temp is: "); //Print Your results Serial.print(tempF); Serial.println(" degrees F"); Serial.print("The Pressure is: "); Serial.print(pressure); Serial.println(" Pa."); Serial.println(""); delay(250); //Pause between readings. mySensorData.print(tempF); //write temperature data to card mySensorData.print(","); //write a commma mySensorData.println(pressure); //write pressure and end the line (println) mySensorData.close(); //close the file } } |
If you have the BMP180 and the SD card connected correctly, this should create a file called PTData.txt on the card, and write comma delimited data to the file. Note that if the file does not exist on the card, the command:
1 | mySensorData = SD.open("PTData.txt", FILE_WRITE); |
will create the file. If the file already exists, this command will append data to the existing file. If you want to start with a clean new data set, erase the old PTData file.
When you run the program, you end up with a PTData.txt file on the SD card. When you have finished logging your data, you can pop the card out, put it into your PC, and then import the data into excel. You should now be able to plot, graph or analyze the data using all the powerful features of Excel.