EMBEDDED TWEAKS

Embedded Electronics Tutorials, Coding and lot more

Arduino: Accessing the internal EEPROM of Atmega8/328

Sometimes we need some data or variable values to be retained back even after the system is turned off. So as soon as we again start back the system  we need to know the previous values of some elements to identify the state of the system at shut down. So that now we can resume directly from that state only, not the initial state.  In many cases this is required.

SONY DSC

 

  • In GSM based devices,  To save the custom mobile number of user which is being changed sometimes.
  •  For sensor interfacing examples, if we want to create a log of frequent values then we can also use to store values one by one with some by time delay . And so can get whole data logged.

So in these type of cases, it is most suitable to use internal EEPROM in the Arduino. As the arduino’s core is based on the Atmega microcontorller so that, in other words we can say to interface EEPROM of Atmega microcontroller infact.
Now the Arduinos are coming with lot variants such as Atmega8, Atmega16 or Atmega32. But the good thing is that all of them are having EEPROM.  Atmega 8 has 512 Bytes and Atmega168, Atmega328 have 1024 Bytes EEPROM.

So basically EEPROM is just a collection of so many memory locationa only. So we can have 3 main tasks for them.

  1. EEPROM Write
  2. EEPROM Read
  3. EEPROM Clear

We will see them one by one in detail.

1. EEPROM Write:
Here we are writing the memory locations of EEPROM one by one. The all functions are provided by the EEPROM.h library built in for arduino. So we just need to include it in our program.

#include <EEPROM.h>

int addr = 0;

void setup()
{
}

void loop()
{
int val = analogRead(0) / 4;
// need to 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.
EEPROM.write(addr, val);
// this function writes the actual data that we want in the EEPROM
addr = addr + 1;
//increase memory location pointer
if (addr == 512)
addr = 0;
// return after 215 location, so that this program can run on Atmega8,168 as well as 328.
delay(100);
}

2. EEPROM Read:
Now the data has been already written in to EEPROM, so now we need to verify that it is actually written there.  So that for testing we will use serial terminal to check data.

#include <EEPROM.h>

// start from the address 0 of the EEPROM
int address = 0;
byte value;

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”);
Serial.print(value, DEC);
Serial.println();

// advance to the next address of the EEPROM
address = address + 1;

// there are only 512 bytes of EEPROM, from 0 to 511, so if we’re
// on address 512, wrap around to address 0
if (address == 512)
address = 0;
delay(500);
}

3. EEPROM Clear:

Now if we want to clear the content of any location then also this feature is provided by Arduino easily. We just need to write null character ‘\0’ to it.

#include <EEPROM.h>

void setup()
{
// write a 0 to all 512 bytes of the EEPROM
for (int i = 0; i < 512; i++)
EEPROM.write(i, 0);

// turn the LED on when we’re done
digitalWrite(13, HIGH);
}

void loop()
{
}

For this article, some content was taken from “www.arduino.cc” as a reference, which is itself Open Source. You can see their website for other Arduino stuffs.

Sharing is caring

6 Comments

  1. I read this paragraph fully concerning the difference of newest and preceding technologies,
    it’s awesome article.

  2. Appreciate the recommendation. Leet mme trry iit out.

    Feel freee to visit my web site :: help wanted

  3. What’s Happening i am new to this, I stumbled upon this I’ve found It positively helpful and it has helped me out loads. I hope to contribute & aid other users like its helped me. Good job.

  4. I’m glad I found this post. Emailed it to myself to see afterwards from my desktop. Will attempt for connecting with you on Facebook then too.

  5. I read this article completely about the comparison of most recent and previous technologies, it’s remarkable article.

Leave a Reply

Your email address will not be published.

*

© 2018 EMBEDDED TWEAKS

Theme by Anders NorenUp ↑

Follow Me

Get the latest posts right delivered in you mailbox

%d bloggers like this: