Arduino Data Logger

From MAGLab
Jump to navigation Jump to search

Return to Projects Projects

    • I need an account to add pictures**

Introduction:

This is a data logger I made for my class. I couldn't find any instructions that I could use to make the Arduino record the light brightness and the temperature. Then display the temperature and the light value on the LCD then Save it to the SD card

Project Goals

Collect data from at least 2 sensors
write the data at regular intervals to the SD card, collect it in a comma separated variable file (*.CSV) that can be imported into excel.
display the data to your LCD screen
have the background color of the screen change somehow. Either from the data being collected (green background for nominal values, red for warning), or through the use of a button.

Parts

Here are what I used for parts. From my experience if your trying to follow instructions while learning and are using different parts it is much harder.

If you don't want to buy the stackable header pins for the Datalogger shield you can use some way like how I modified the Datalogger shield in step 2 so that it could be placed in the bread board.

Datalogger shield
Stackable header pins
LCD Display
Experimentation Kit for Arduino (Uno R3)
     or
Arduino Uno
Photo cell
Analog Temperature sensor

Hardware

Modify the Data logger shield

  • Picture
  • Picture

If you have the stackable header pins on the data logger then skip this step. These are the pins that are needed for the SD card to work. you can solder wires to the board or follow my pictures. 3v, 5v, Gnd, A4, A5, 13, 12, 11, 10.

Wiring Diagram

  • picture

Here is the wiring diagram for my data logger. It is drawn as if you are using the stackable header pins. But the arduino pins that the data logger use are marked also.

Here is a table for where each lcd pin goes for my code to work. the LCD pin corresponds to the Arduino pin below it. If it appears out of alignment copy it to notepad.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18, lcd

g,+5,pt, 7, g, 8 , 9, 4, 2, 6,+5, G 5, arduino final change

Coding

Libaries

For the code in the next step to work you will need some libraries added to the Arduino folder. Go to the hyperlinks to get them. well there is just one library not installed already.

  • LiquidCrystal - Already installed
  • SD - Already installed
  • Wire – Already installed
  • RTClib - HERE

Code

  • Picture

This is hopefully the last step. The code should read the Temperature Sensor and the Photo Cell then display the values on the LCD and save them to the SD card. There are a few trouble shooting codes that might display on the serial terminal if something is wrong. When the temperature is hotter the back light should turn red, when the room is brighter the back light should turn blue, if it is hot and bright it should be purple. Cold and dark should be black.

The code is very messy I left lots of lines as notes in case I want to go back and add them. It looks much better then the picture.

Copy the code below into a sketch to use

// include the library code:

  1. include <LiquidCrystal.h>
  2. include <SD.h>
  3. include <Wire.h>
  4. include "RTClib.h"

const int chipSelect = 10; //File dataFile; File logfile;


// initialize the library with the numbers of the interface pins LiquidCrystal lcd(7, 8, 9, 4, 2, 6);


//TMP36 Pin Variables int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to the resolution is 10 mV / degree centigrade(500 mV offset) to make negative temperatures an option

//PhotoResistor Pin int lightPin = 1; //the analog pin the photoresistor is connected to the photoresistor is not calibrated to any units so this is simply a raw sensor value (relative light)

//led for light sensor int lcdRed = 3; //the pin the LED is connected to int lcdBlue = 5; //the pin the LED is connected to


RTC_DS1307 RTC; // define the Real Time Clock object


//========================================================

void setup() {

 // set up the LCD's number of columns and rows:

lcd.begin(16,2); Serial.begin(9600); //Start the serial connection with the copmuter

                      //to view the result open the serial monitor
                      //last button beneath the file bar (looks like a box with an antena

pinMode(lcdRed, INPUT); //sets the led pin to input pinMode(lcdBlue, INPUT); //sets the led pin to input

Wire.begin(); RTC.begin(); //----------------------------------------

 if (! RTC.isrunning()) {
   Serial.println("RTC is NOT running!");
   // following line sets the RTC to the date & time this sketch was compiled
   RTC.adjust(DateTime(__DATE__, __TIME__));
 }

//-------------------------------

 Serial.print("Initializing SD card...");
 // make sure that the default chip select pin is set to
 // output, even if you don't use it:
 pinMode(SS, OUTPUT);

 // 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:
   while (1) ;
 }
 Serial.println("card initialized.");

// Open up the file we're going to log to! // dataFile = SD.open("datalog.txt", FILE_WRITE); // if (! dataFile) { // Serial.println("error opening datalog.txt");

   // Wait forever since we cant write data

// while (1) ; // } //----------------- // create a new file

 char filename[] = "LOGGER00.CSV";
 for (uint8_t i = 0; i < 100; i++) {
   filename[6] = i/10 + '0';
   filename[7] = i%10 + '0';
   if (! SD.exists(filename)) {
     // only open a new file if it doesn't exist
     logfile = SD.open(filename, FILE_WRITE);
     break;  // leave the loop!
   }
 }

Serial.print("Logging to: ");

 Serial.println(filename);

logfile.println(",,,Willy,Reen"); logfile.println("Data Logging File"); logfile.println("Date,Temp(F),Light"); }


//=========================================================

void loop() {

float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor temperature = ((temperature - .5) * 100)*1.8+32; //converting from 10 mv per degree wit 500 mV offset

                                                 //to degrees ((volatge - 500mV) times 100)

// Serial.println(temperature); //printing the result // delay(500); //waiting a second

//------------------------ int lightLevel = analogRead(lightPin); //Read the lightLevel = map(lightLevel, 0, 900, 0, 150); //adjust the value 0 to 900 to span 0 to 255 lightLevel = constrain(lightLevel, 0, 150); //make sure the value is betwween 0 and 255 ////// int tempLevel = analogRead(temperaturePin)*100; //Read the tempLevel = map(tempLevel, 66, 82, 0, 150); //adjust the value 0 to 900 to span 0 to 255 tempLevel = constrain(tempLevel, 0, 150); //make sure the value is betwween 0 and 255 analogWrite(lcdBlue, lightLevel); //write the value analogWrite(lcdRed, tempLevel); //write the value //-----------------------------------------------------------

String dataString = "";

//date time stuff-------------------------------------------

//------------------------------------------------------- //logfile.println("");

DateTime now = RTC.now();

 // log time

// logfile.print(now.unixtime()); // seconds since 1/1/1970 // logfile.print(", ");

 logfile.print('"');
 logfile.print(now.year(), DEC);
 logfile.print("/");
 logfile.print(now.month(), DEC);
 logfile.print("/");
 logfile.print(now.day(), DEC);
 logfile.print(" ");
 logfile.print(now.hour(), DEC);
 logfile.print(":");
 logfile.print(now.minute(), DEC);
 logfile.print(":");
 logfile.print(now.second(), DEC);
 logfile.print('"');

logfile.print(","); // logfile.print(temperature); logfile.print(","); logfile.print(lightLevel); delay(500);

//---------------- /*

 // read sensors and append to the string:
 for (int analogPin = 0; analogPin < 2; analogPin++) {
   int sensor = analogRead(analogPin);
   dataString += String(sensor);
   if (analogPin < 1) {
     dataString += ",";
   }
 }
  • /

// dataFile.println(dataString);

 logfile.println(dataString);

// Serial.println(dataString); // dataFile.flush();

 logfile.flush();
 

//-----------------------------------------------------------

 // set the cursor to (0,0):
 lcd.setCursor(0, 0);
 // print from 0 to 9:

// for (int thisChar = 0; thisChar < 10; thisChar++) {

  lcd.print(temperature);

// lcd.print(myString);

  lcd.setCursor(0, 1);
  lcd.print(lightLevel);
 
  lcd.setCursor(8, 0);
  lcd.print("Temp");   //display temp on the lcd
  lcd.setCursor(8, 1);
  lcd.print("Light");   //display light on the lcd
 
 
  delay(500);
 
  //---------------------lcd backlight

// lightLevel = constrain(lightLevel, 0, 255);//make sure the

                                          //value is betwween
                                          //0 and 255
 }


 //------------------------------------------------------------


 // set the cursor to (16,1): 

// lcd.setCursor(16, 1);

 // set the display to automatically scroll:

// lcd.autoscroll();

 // print from 0 to 9:

// for (int thisChar = 0; thisChar < 10; thisChar++) { // lcd.print(thisChar); // delay(500); // }

 // turn off automatic scrolling

// lcd.noAutoscroll();

 // clear screen for the next loop:

// lcd.clear(); //}

//========================================================== float getVoltage(int pin){ return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range

                                       // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts

}