2018
February Thursday 22

Bookmark and Share

Related Articles...

MILWAUKEE WI Arduino Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Arduino Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Raspberry Pi Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Raspberry Pi Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Internet of Things (IoT) Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Make Space Acquired!

Full Advanced Fabrication Facility, machine Shop, Welding, Electronics Lab Equipment Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Machine Repair, & Installation  - Tier 1 Business Support

Machine Technician Mechanic 20+ Years  Personal Experienced In Industrial Factory Machine Servicing, Rebuilding, Installation And Repair - Plus A 35+ Years Experienced Computer Engineer Service Technician Team Ready For Your Outsourced Market Advantage Needs!

Hartung Industrial - Member - CGI Science & Technology Group

Email :

COASMailBox @ gmail.com

2018 REVIEW

 

Arduino EEPROM

Using EEPROM To Store Data On An Arduino

2018 JAN | by Gene Casanova

Senior Systems/Network/Internet/Robotics Engineer


Storing Data

Data collected or generated in a program running on a microcontroller IC chip, is loss when power is removed from the chip. This type of memory is referred to as 'volatile memory'.

The microcontroller IC chip, used in the Arduino developer systems, have a special built-in memory designed to hold data after electrical power has been removed from the IC chip. This memory is referred to as 'EEPROM', an abbrivation for 'Electrically Erasable Programmable Read-Only Memory'.


EEPROM

The microcontroller IC chips used on various Arduino development board models, have either 512, 1024, or 4096 bytes of EEPROM memory built into the chip. This memory is non-volatile, which means the data is held when electrical power is removed from the IC chip.

The available EEPROM is an array with each element being one byte in size. When reading-from and writing-to this memory, an address must be specified. This is equivalent to an array index.

BEWARE

An EEPROM has a limited lifetime of ~100,000 write cycles. Beware, writes to EEPROM consums cycles from this limit. Avoid writing to the EEPROM often! Beware, erasing EEPROM memory is a write process, consuming a cycle.


Example Working With Arduino EEPROM Memory

The following example, demostrates using the EEPROM functions 'EEPROM.read()' and 'EEPROM.write()'.

The following source code will produce a program to read a temperature sensor and store the value in the EEPROM once every two seconds. A button is used to print the non-empty part of the EEPROM out the serial interface, and a second button is used to erase the EEPROM content.

Example Arduino EEPROM Write

/*
 * 2018 FEB
 * 
 * Arduino EEPROM EXAMPLE USAGE
 *
 * Data Logger
*/
#include <EEPROM.h>	//	Include the EEPROM librarry.

#define SAMPLE_TIME 2000  // Used to hold the time in milliseconds, between EEPROM writes.

int tempPin = 0;	// Temperature probe connects to Arduino I/O pin 0
int printPin = 2;	// Button Print I/O Pin.
int erasePin = 4;	// Button Erase I/O Pin.

int address = 0;	// EEPROM Address Counter.

unsigned long timer;

float conv_coeff = 0.0;   // Coefficient for converting from 0-1024 to 0-5 range

void printTemp();
void clearEEPROM();
void writeTemp();

void setup(){
  Serial.begin(115200);     // Begin a Serial Interface Object; using 115200 baud rate.
  conv_coeff = 5.0/1024.0;  // Get the Coefficient to do the conversion.
  timer = millis();         // millis() = time since program start, in milliseconds.
}


void loop(){

  if(millis()-timer > SAMPLE_TIME)  // Check if time to do a temp sensor sample.
  {
    writeTemp();
    timer = millis();
  }
 
  if(!digitalRead(printPin))  // Check if Print button is pressed.
  {
    printTemp();
    delay(500);
  }
 
  if(!digitalRead(erasePin)) // Check if Erase button is pressed.
  {
    clearEEPROM();
    delay(500);
  }
   
  delay(1);
}

void printTemp()
{
for (int i = 0 ; i < EEPROM.length() ; i++) {
byte value = EEPROM.read(i); // Read EEPROM data at address i.
if(value != 0) // Skip "empty" addresses.
{
float temp = value*conv_coeff; // Convert ADC values to temperature.
temp = (temp - 0.5)*100; // Calculate reading offset.

Serial.println(temp);
}
}
} void clearEEPROM() { for (int i = 0 ; i < EEPROM.length() ; i++) { if(EEPROM.read(i) != 0) // Skip "empty" addresses. { EEPROM.write(i, 0); // Write 0 to address i. } } Serial.println("EEPROM erased"); address = 0; // Reset address counter. } void writeTemp() { byte value = analogRead(tempPin); // Read sensor value. EEPROM.write(address, value); // Write value to current address counter address. Serial.print("Sensor value stored at address "); Serial.println(address); address++; // Increment address counter. if(address == EEPROM.length()) // Check if address counter has reached the end of EEPROM. { address = 0; // If yes: reset address counter. } }

The 'EEPROM.read()' fiunction takes one parameter, an integer, the address of the byte to read from the EEPROM. The function returns the data at the address specified. "Read" cycles are non-destructive to the EEPROM.

The EEPROM.write() function takes an integer and a byte datatype (uint8_t) as parameters. The byte parameter is the data to store.

When storing more than integer values between 0-255, several addresses must be used for each write and read; or use other available EEPROM functions.

The raw data from the ADC has a range of 0-1024. Typically the values are between 150 and 200 for "room temperature". The raw data is stored in the EEPROM, assuming the temperature will not get higher than 255. A propper program must have safeguards against going out of the 0-255 range.

Instead of storing the actual floating point temperature values in the EEPROM, convert the raw data to understandable data in the "printTemp()" function.

Example notes: this example was made to demostrate the funtions, and not be a production program. For better performance, store the current write address, and start writing data after this stored location. The EEPROM size on the Arduino UNO is 1024 bytes. Use 2 bytes to store metadata.


Other Arduino EEPROM Functions

Update()

The update() function writes to the EEPROM if the data differs from the data already stored at the specified address.

Using this function is wise to use, since the EEPROM write cycle typically takes 3.3 ms to complete and due to the limited memory lifetime.

Syntax:

update(address, my_byte_variable);
    

Put()

The put() function, enables storing larger than single byte data.

Functions like write(). If incrementing the address when sequencially storing data, write something like "address += sizeof(float);" for float variables, instead of "address++;".

Syntax:

put(address, my_int_variable);
    

Get()

The get() function, enables reading datatypes like 'int', 'float' and 'struct variables', from EEPROM.

This function takes the variable the data read will be stored in. The function sets how many bytes will be read from the EEPROM.

Syntax:

get(address, my_float_variable);
    

EEPROM[]

EEPROM[] is an operator; enabling using the EEPROM like an array.

Syntax:

EEPROM[0] = my_byte_variable;		//	Write example.
my_byte_variable = EEPROM[0];		//	Read example.
    

Use The Technology Wisely & Keep It Simple

- Cheers!

Gene Casanova


Cyber Security Since 1979 - Consulting Available By Subscription

CGI Computer Wares | EST 1979

Send E-MailCONTACT: [click]@CGIComputerWares.com