Sunday, 9 August 2015

Datalogging with SD card - Writing data to sd card !

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:
  1. MOSI ( Master Out Slave In )
  2. MISO (Master In Slave Out)
  3. SCLK ( Shared Serial Clock)
  4. CS/SS (Chip Select/Slave Select)      
  MOSI: Used to send serial data from master to slave device.
  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
     
The first two lines #include<SPI.h> and #include<SD.h> are used to include the SPI and SD libraries that are already available in arduino environment. We have used many commands in the code which requires these libraries for their proper functioning. So we have used these two header files.
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.
   

Friday, 7 August 2015

How to simulate arduino uno in proteus?

How to simulate arduino uno in Proteus?

What is proteus?

Proteus is the simulation software for microcontroller. It is very useful since we can do software simulation without even using the hardware. We can write the code in IDE( Integrated Development Environment) and then use the same code in proteus for simulation. Proteus supports variety of microcontroller boards but unfortunately there is no arduino uno board in proteus. But we have solution for the same.Before procedding further you need to download the proteus software. There are many versions of proteus available. I would recommend you to use the Proteus 8 software.You can download the library files for the arduino using the link given below and then follow the steps mentioned below to add arduino uno simulator in proteus.
Click on the link to download the arduino library files: http://www.zer07even.com/download/125-arduino-library-proteus-untuk-simulasi 
A rar filed named as ARDUINOLIBS will be downloaded.
Follow the steps mentioned below

Step 1 : Unzip the rar file using winrar or any other software.

Step 2: There will be two files named as ARDUINO.IDX and ARDUINO.LIB. Copy these two files. Now go to C: drive (If   you have chosen different directory for installation of proteus then you have to search for the folder named as library inside labcentre electronics folder). Open program data. Then open labcenter electronics. Then Proteus 8 professional. Open the LIBRARY FOLDER and paste these two files.

Step 3:There will be a rar file named as AVR2 inside the ARDUINOLIBS folder. Unzip the AVR2 rar folder.

Step 4:Copy AVR2.IDX and AVR2.lib files.Now go to C: drive. Open program data. Then open labcenter electronics. Then Proteus 8 professional. Open the LIBRARY FOLDER and paste these two files.

Step 5:There will be a file named as AVR2.DLL inside ARDUINOLIBS folder. Copy this file.Now go to C: drive. Open program data. Then open labcenter electronics.Then Proteus 8 professional. Open the MODEL FOLDER and               paste this file.


Now arduino library files has been installed and is ready to use. Note: The path specified for MODEL and LIBRARY folder may vary for different versions of windows and for different proteus versions. It is important to copy the specified files in MODEL and LIBRARY folder only.
In my next post i will write about the basic concepts of arduino and the programming to code the arduino.
              



Thursday, 6 August 2015

Intorduction to myself and my new blog!

Hi guys ! Akib here. I am currently pursuing Bachelor of Technology degree in Vellore Institute of Technology in Electronics and Communication. I love to use technology to make things that would be beneficial for the society. I have made various projects using arduino , atmega 16 etc. So, i will be writing about the concepts and technology involved in the field of microcontrollers and robotics. Many people think that robotics is rocket science but believe me it is not. Anyone can learn robotics if he/she has basic knowledge of High level language programming like C,C++,JAVA etc and basic electronics. Even full or deep knowledge of programming is not require to learn microcontrollers or robotics. In this blog i ll start from basics of microcontrollers. I will be strating from Arduino and ATMEGA programming.

WHAT IS ARDUINO?

Arduino board is a microcontroller board which is a small circuit that contains a whole computer on a small chip.There are different versions of arduino available in market  in different sizes and for different purposes. Examples are arduino uno, arduino lilypad, freeduino. For our purpose we will use arduino uno as it is cheap and it has sufficient memory and i/o pins for our projects
 You have lot of microcontrollers available in market like 8051, arduino, atmega 16 etc but why i am choosing arduino. There are lot of reason behind this. 

WHY ARDUINO?



  • It is an open source project, software/hardware is extremely accessible and very flexible to be customized and extended.
  • It is flexible, offers a variety of digital and analog inputs, SPI and serial interface and digital and PWM outputs.
  • It is easy to use, connects to computer via USB and communicates using standard serial protocol, runs in standalone mode and as interface connected to PC/Macintosh computers.
  • It is inexpensive, around Rs 500/- per arduino uno board and comes with free authoring software.
  • Arduino is backed up by a growing online community, lots of source code is already available and we can share and post our examples for others to use, too!                                                                                  
  • Arduino is a great tool to make interactive objects as it can take various inputs from motors, sensors, swtiches, gps module etc and perform the operations as it is instructed by the user to give desired outputs. You can make intellegent things using arduino as you can program the arduino as your needs.

    Now you have understood why we are using arduino uno , now we will be learning it programming. In my next post i will be writing about the basics of the arduino along with introduction to proteus and how to simulate arduino using proteus software.