Go Down

Topic: EEPROM Data Logging w/ LM34 Temp Sensor (Read 12963 times) previous topic - next topic

dsmith5167



The code below will log temperature data of a specified time period.

It is a 2 to 3 step process
1. Clear EEPROM memory
      (this step is optional Temp Logger will overwrite any currently stored data)

Code: [Select]
/*
* EEPROM Clear
*
* Sets all of the bytes of the EEPROM to 0.
* This example code is in the public domain.

*/

#include <EEPROM.h>

void setup()
{
  // write a 0 to all 1024 bytes of the EEPROM
  // there are 1024 bytes in the ATmega328 EEPROM
  // 512 bytes on the ATmega168 and ATmega8
  // 4 KB (4096 bytes) on the ATmega1280 and ATmega2560
  for (int i = 0; i < 1024; i++)
    EEPROM.write(i, 0);
   
  // turn the LED on when we're done
  digitalWrite(13, HIGH);
}

void loop()
{
}


2. Load and run LiquidCrystal Temperature Logger.
      The program will display acquired data on the LCD including the number of readings it has made.
      See code comments for more information on using and tweaking the program

NOTE:
      Using an external power source of 7v will give more accurate readings.
      During testing I used a 9v supply but noticed components on the Arduino board getting warm/hot.
      7v is enough for stability without heating up board components.
      It can be powered with the USB supply but it fluctuates too much and will distort readings.

Code: [Select]
/*
  LiquidCrystal Temperature Logger.
Developed using Arduino Uno R3

Pins configuration for standard HD44780 LCD
This sketch is for 16 character x 2 row LCD

* LCD Pin 1 to Ground
* LCD Pin 2 to +5
* LCD Pin 3 to Center Leg Pot 10k
* LCD Pin 4 to digital pin 12
* LCD pin 5 to Ground
* LCD pin 6 to digital pin 10
* LCD pin 11 to digital pin 5
* LCD pin 12 to digital pin 4
* LCD pin 13 to digital pin 3
* LCD pin 14 to digital pin 2
* LCD pin 15 +5 (LED Backlight +) *optional
* LCD pin 16 to Ground (LED Backlight -) *optional

Pin configuration for LM34DZ http://www.ti.com/lit/ds/symlink/lm34.pdf

* Vs   to +5
* Vout to Analog pin A0
* GND  to Ground

Sketch created by David A. Smith 6-22-2012

Modified from
http://reversemidastouch.blogspot.com/2010/03/arduino-lm34-temperature-sensor.html
"eeprom_write" from File > Examples > EEPROM > eeprom_write

*/

// include the library code:
#include <EEPROM.h>
#include <LiquidCrystal.h>

int addr = 0;                   // the current address in the EEPROM we're going to write to
int sensor_pin = 0;             // the analog pin for LM34 temp sensor
int sensor_reading = 0.0;       // variable to store the value coming from the sensor
float vref = 4.85;              // variable to store the voltage reference used
                                // using a separate thermometer tweak vref value to fine tune the displayed temperature
                               
int fahrenheit = 0;             // variable to store the fahrenheit temperature
int centigrade = 0;             // variable to store the centigrade temperature
int acquisition_timer = 5000;   // variable to control the time between updates (in ms)
                                // 300000 = 5 min intervals
int loop_counter = 0;
int bytemax = 1024;             // adjust this value for your eeprom memory limit
                                // there are 1024 bytes in the ATmega328 EEPROM
                                // 512 bytes on the ATmega168 and ATmega8
                                // 4 KB (4096 bytes) on the ATmega1280 and ATmega2560

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16,2);
  pinMode( sensor_pin, INPUT );    // set LM34 temp sensor pin as an input
  analogReference(DEFAULT);        // set the analog reference to the 5V internal reference
  delay(1000);
}

void loop() {
 
  int sensor_reading = analogRead(sensor_pin);               // stores the digitized (0 - 1023) analog reading from the LM34
  float fahrenheit = (100.0 * sensor_reading * vref)/1023;   // calculates the actual fahrenheit temperature
  float centigrade = ((fahrenheit-32)/1.8);                  //conversion to degrees C

// set the cursor to (0,0):
   lcd.setCursor(0,0);
   lcd.print(centigrade);
   lcd.print((char)223);   // degree symbol
   lcd.print("C ");
   lcd.print(fahrenheit);
   lcd.print((char)223);   // degree symbol
   lcd.print("F");

//Print readings counter on the second row
   lcd.setCursor(0, 1);
   lcd.print("Readings: ");
   
// if the eeprom is not full
   if (loop_counter <= bytemax) {
   lcd.print(loop_counter);
   
// divide by 4 because analog inputs range from 0 to 1023 and
// each byte of the EEPROM can only hold a value from 0 to 255.
   int val = sensor_reading / 4;

// write the value to the appropriate byte of the EEPROM.
// these values will remain there when the board is turned off.
   EEPROM.write(addr, val);

// advance to the next address.
// there are 1024 bytes in the ATmega328 EEPROM
// 512 bytes on the ATmega168 and ATmega8, 4 KB (4096 bytes) on the ATmega1280 and ATmega2560
  addr = addr + 1;
  delay (acquisition_timer);
  }
 
    else {
      lcd.print("Full");
      delay(acquisition_timer);          // 300000 = 5 min intervals
    }
    loop_counter++;

// turn off automatic scrolling
   lcd.noAutoscroll();
 
// clear screen for the next loop:
   lcd.clear(); 
}



3. Retriving the data
      The temp logger will tell you when the eeprom is full.
      The code below will retrieve the data and print it to the serial monitor.

NOTE:
      After printing the 1024 data points it will pause for 5 min and then print them again. (better coding needed)
      During the pause simply ctrl-a then ctrl-c to copy the data and past it to Excel or other programs.
      If your chip has different memory limits you will need to tweak the code accordingly.
      Again refer to code comments for more detail.

Code: [Select]
/*
* EEPROM Read
*
* Reads the value of each byte of the EEPROM and prints it
* to the computer.
* This example code is in the public domain.
*/

#include <EEPROM.h>

// start reading from the first byte (address 0) of the EEPROM
int address = 0;
byte value;
float vref = 4.85;              // variable to store the voltage reference used

int bytemax = 1024;             // adjust this value for your eeprom memory limit
                                // there are 1024 bytes in the ATmega328 EEPROM
                                // 512 bytes on the ATmega168 and ATmega8
                                // 4 KB (4096 bytes) on the ATmega1280 and ATmega2560
                               
void setup()
{
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop()
{
  // read a byte from the current address of the EEPROM
  value = EEPROM.read(address);
 
  Serial.print(address);
  Serial.print("\t");
  value = value *4;
  float fahrenheit = (100.0 * value * vref)/1023;   // calculates the actual fahrenheit temperature
  Serial.print(fahrenheit);
  Serial.println();
 
  // advance to the next address of the EEPROM
  address = address + 1;
 
  // there are only 1024 bytes of EEPROM, from 0 to 1023, so if we're
  // on address 1023, wrap around to address 0
  if (address == bytemax){
    delay (300000);     //Delays the loop for 5 min
                        // copy paste from the serial monitor to XL or other program
    address = 0;
  }
  else {
    delay(100);
    }   
}


Any questions, comments, or suggestions are welcome.
Enjoy

Go Up