TransWikia.com

Storing value in EEPROM in Arduino Uno

Arduino Asked by Jaivishnu on January 22, 2021

Hi I am a beginner in Arduino Uno. I wanted to store a value of 77880 in EEPROM and fetch it later. I made the code as follows to check my logic(I am using Arduino Uno) :

#include <EEPROM.h>
int addr = 0;
byte value;
void setup() {
  // put your setup code here, to run once:
  int val = 77880;
  EEPROM.write(addr, val);
  Serial.begin(57600);
}

void loop() {
  // put your main code here, to run repeatedly:
  value = EEPROM.read(addr);

  Serial.print(addr);
  Serial.print("t");
  Serial.print(value);
  Serial.println();
  delay(1000);
}

but I am not getting the value of 77880 when I read the EEPROM address. instead I am getting a value of 56 in my serial output. Can anybody pls explain how should I implement the same?

2 Answers

EEPROM can only store the values 0-255 in any one address. That's the maximum range of an 8-bit value, and EEPROM "cells" are 8-bit in size.

You will have to split your value up into multiple 8-bit chunks and store each chunk at a different address - for example for 32-bit values you can use:

uint32_t val = 77880; // In hex is 0x13038
int addr = 0;
EEPROM.write(addr, val & 0xFF); // Stores 0x38 in address 0
EEPROM.write(addr + 1, (val >> 8) & 0xFF); // Stores 0x30 in address 1 
EEPROM.write(addr + 2, (val >> 16) & 0xFF); // Stores 0x01 in address 2 
EEPROM.write(addr + 3, (val >> 24) & 0xFF); // Stores 0x00 in address 3 

Then to read you do the opposite and shift the values and OR them:

uint32_t val = (uint32_t)EEPROM.read(addr) |
               ((uint32_t)EEPROM.read(addr + 1) << 8) |
               ((uint32_t)EEPROM.read(addr + 2) << 16) |
               ((uint32_t)EEPROM.read(addr + 3) << 24);

Alternatively the EEPROM library has a "store anything" helper to do it for you:

EEPROM.put(addr, val); 

That will store the variable val across enough cells to fit it (4 in the case of a 32-bit variable), so you must know not to write to the other cells used by it (0, 1, 2 and 3 in this case) or you will corrupt your data.

You recall it with:

EEPROM.get(addr, val);

Correct answer by Majenko on January 22, 2021

See Majenko's answer for why you have to problem. This is alternative solution to the problem.

Using the Arduino library for EEPROM is very limited. It can only read/write one byte at a time. Your variable val is of the type int which is a 16 bit integer on the uno. So two bytes, which is also called a word.

I use the AVR EEPROM "library" directly.

#include <avr/eeprom.h>

EEMEM int eepVariable; // using EEMEM will make the compiler handle the location in EEPROM for you
                           // the compiler makes sure no data will ever overlap inside the EEPROM.

int value; //should be an `int` too.
void setup() {
  // put your setup code here, to run once:
  int val = 77880;
  eeprom_write_word(&eepVariable, val);
  Serial.begin(57600);
}

void loop() {
  // put your main code here, to run repeatedly:
  value = eeprom_read_word(&eepVariable);

  Serial.print(value);
  Serial.println();
  delay(1000);
}

Answered by Gerben on January 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP