Data logging with SD Card
Data logging is very useful in most of the projects where you want to store the data or values obtained from different sensors and keep a track of it. By storing data in memory you can later visualise and analyze the data obtained from different sensors. For example if you want to build a weather station for tracking temperature , humidity , light etc. you will need to store it in some memory so that you can later analyze and interpret the data. Due to the small size , minimal power consumption and ease of interfacing with large number of sensors arduinos are good choice for making dataloggers. Dataloggers are the devices that can record and store information over a period of time. Another example is GPS tracker. If you want to keep the track of the path you have followed you will need a datalogger. Hence dataloggers are very useful in all the projects.
You will require an arduino uno board , SD card shield and SD card. Before moving on to the actual programming you will need to know about Serial communication and SPI communication. Our arduino will communicate with sd card through Serial Peripheral Interface (SPI).
SERIAL PERIPHERAL INTERFACE (SPI)
The SPI bus is a syncronous serial communication interface specification used for short distance communication like in microcontrollers. This interface was first developed by MOTOROLA. SPI devices communicate in full duplex mode using master-slave architecture with single master. However multiple slaves can communicate with single master device. Multiple slave devices are supported through selection with individual slave select or chip select line (CS). SPI devices are synchronous i.e. data is transmitted in sync with a shared clock signal(SCLK). Data can be shifted into the slave device on either the rising or falling edge of the clock signal.There are basically four pins which are used for spi communication. They are:
- MOSI ( Master Out Slave In )
- MISO (Master In Slave Out)
- SCLK ( Shared Serial Clock)
- CS/SS (Chip Select/Slave Select)
MISO: Used to send serial data from slave to master device.
SCLK: The signal by which serial data is synchronized with the receiving device
SS: This line is used for slave selection. Pulling it low means you are communication with that device.
We will use the predefined SPI library for SPI communication. In future post i will write about detailed SPI Communication with various codes. This much information about SPI is sufficient to make a datalogger.
SERIAL COMMUNICATION
The Serial Communication is used for communication between the arduino board and a computer or with other devices. All arduino boards have atleast one serial port. You can use arduino environment's buildin serial monitor to communicate with arduino board. With the help of serial communication we can communicate with our arduino through pc and the data retrived by arduino can be displayed on the serial montior in PC. For example: if i used an IR sensor in my project and i want to see the values which are read by the IR sensor then i can use the serial communication to view those values in my PC. Sounds good! Right.
For serial communication we need to know some commands which will be used whenever you use serial communication.
Serial.begin(baud rate) :- This command is used to initialize the serial communication. Baud rate is the rate at which information is transferred in a communication channel. If we set the baud rate value to 9600 which means that the serial port is capable of transferring a maximum of 9600 bits per second. Important thing to note is that the baud rate which you specify here should match with the baud rate that appears on the serial monitor screen otherwise communication will not take place.
Serial.print(message) :- Here message is any string or variable that you want to print. You use these functions to write data to the serial port.
Datalogging
Now you have understood what is serial communication and SPI. Now we can move on to datalogging.For datalogging you will need a SD card shield and SD card. I have bought the following SD card from https://robomart.com/micro-sd-tf-card-memory-for-arduino-shield-module?search=sd&dsearch=sd .
As you can see there are 6 pins in the SD card shield. These 6 pins are basically MISO , MOSI , SCLK , CS , VCC and GROUND. It is important to note that SD cards are 3.3V devices. These six pins are used to communicate with Arduino Uno board through SPI communication. You can see there is a arrow mark on the SD card shield. This is where you will insert your Micro SD card. Make sure you do FAT formatting of your sd card before using it for datalogging. FAT formatting can easily be done by inserting your micro sd card using cardreader to the PC. If you find any difficulty in formatting the SD card refer to this url and follow the steps given there. Note that formatting memory card will erase all the data present in the sd card.http://www.wikihow.com/Format-an-SD-Card . After you format your sd card , your sd card will be ready for datalogging operation.Make the connections as specified below
PIN 10 of your arduino uno to CS pin of sd card shield.
PIN 13 of your arduino uno to SCK pin of sd card shield.
PIN 11 of your arduino uno to MOSI pin of sd card shield.
PIN 12 of your arduino uno to MISO pin of sd card shield.
PIN 8 of your arduino uno to VCC pin of sd card shield.
GND pin of your arduino uno to GND pin of sd card shield.
After you make the connections you are ready to code the arduino uno for datalogging purpose.
Coding the arduino uno
#include <SPI.h>
#include <SD.h>
const int power = 8;
const int cs_pin = 10;
void setup()
{
Serial.begin(9600);
pinMode( power , OUTPUT );
digitalWrite(power , HIGH);
pinMode(cs_pin , OUTPUT);
if(!SD.begin(cs_pin))
{
Serial.println(" Card failure");
return;
}
Serial.print(" Initializing the SD card");
}
void loop()
{
String message=" hello sdcard ! ";
File data=SD.open("data.txt", FILE_WRITE);
if(data)
{
data.println(message);
data.close();
Serial.println(message);
}
delay(1000);
}
Explanation of the code
Sd card shield connected to Arduino uno board |
Serial Monitor Reading |
Next i have used the command const int power = 8 . I could have skip this command but it makes our code easy to understand for other people . I have assigned a constant 8 in a variable named power which is a interger datatype . So whenever i need to use pin 8 i would just write power. Similar thing i have done in the next line.
void setup() is the first function which need to be included in every programming of arduino. The lines or instructions which have to be executed exactly once comes inside this function.
To start serial communication we have used the code Serial.begin(9600). 9600 is the baud rate which i have used.
Next i have used pinMode( power , OUTPUT) .By using this command i am telling to arduino that power i.e. PIN NO 8 is acting as output. Similarly pinMode(cs_pin , OUTPUT) is also used for the same reason to specify my pin 10 as output. You have to specify whether a particular pin is acting as input or output otherwise your code will not work as desired.
Next i am using the command digitalWrite(power , HIGH). By using this command i am writing logic High on pin no 8. I am using this command inside void setup because this pin is connected to vcc of the sd card shield and we have to constantly supply logic High or high voltage to the vcc pin through out the program.
Next i used if(!SD.begin(cs_pin)). By putting if condition i am checking that if this returns false value then the following statements would be executed. SD.begin is used to check if the sd card shield is working properly or not. For some reason if it is not working properly you will get the following message " Card Failure" and the entire loop will terminate due to return statement .If SD card shield is working properly you will get the following message on serial monitor "Initializing the SD card".
Then we put another function void loop(). This function must be included in every programming of arduino. Instructions inside this function will execute infinitely i.e. it will not stop till you unplug arduino from power supply or press the reset button on arduino. Then we used a string. String datatype you can use to handle string. String are the collection of alphabets. I am storing the string in a variable named as message.
Now i will be using a datatype called as File. To support this datatype we have used the SD card inbuld library by using the header file #include<SD.h> above. This command SD.open("data.txt",FILE_WRITE) is used to make a file on SD card which will be named as data.txt. .txt is nothing but extension of the file. You can easily open .txt file using notepad. The second parameter enable us to write data on the data.txt file.
Now i have used if statement to check whether the file is available or not. If the file is available then write the data or message you want to write inside the data.txt file created in SD card. data.println will enable you to write the data in data.txt file and make sure to close the writing command on sd card by using data.close(). You can take a look on what you have printed inside the SD card by printing the same message or data on serial monitor of arduino. You can use same Serial.print command to print the message on the serial monitor. Finally put some delay so that next loop will execute after some time. I have used delay of 1 sec. You can use the command delay(time) where the time would be in milliseconds.
Data.txt file has been created in SD card |
Information inside data.txt file |
In my next blog i will write about how you can take readings from array of sensors and store it in SD card and how can you manipulate and analyze the data using Microsoft excel.
Great job.
ReplyDelete