Using EEPROM  

  RSS

Pugwash
(@pugwash)
Reputable Member
Joined: 5 months ago
Posts: 492
2019-10-13 4:23 pm  

Welcome back to the big screen Bill!

I started looking into using EEPROM recently to hold values while the Arduino was shut down.

So you managed to brighten up a wet Sunday afternoon with a subject that has been nagging me for a while.

Thanks for the compliment but "brilliant" is not really the first adjective that comes to mind when logged in to the forum! 🤣 

Did you find a suitable laser protection safety glasses, which is another subject I am looking forward to, immensely!

 

 

I work all day at the factory,
Building a machine that's not for me,
There must be a reason that I can't see,
You've got to humanise yourself!

SteveC - Let there be light(God) - Let there be more light(Pink Floyd)


Quote
codecage
(@codecage)
Member Moderator
Joined: 5 months ago
Posts: 296
2019-10-13 6:02 pm  

Bill mentions two different modules with the I2C EEPROM on board.  I found some of the one with the Berg jumpers for addressing on Amazon (5 for about $8.50, so less than $2.00 a piece), but has anyone found a supplier for the one with the 4 position DIP switch?

SteveG


ReplyQuote
DroneBot Workshop
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 months ago
Posts: 303
2019-10-13 8:01 pm  
Posted by: @codecage

but has anyone found a supplier for the one with the 4 position DIP switch?

I picked those ones up at RobotShop Steve, of course, I shop at the Canadian store but I suspect that the US one will have them too.

https://www.robotshop.com/ca/en/at24c256-eeprom-board.html

🙂

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


ReplyQuote
codecage
(@codecage)
Member Moderator
Joined: 5 months ago
Posts: 296
2019-10-13 9:00 pm  

@dronebot-workshop

They were available at RobotShop in the US as well.  Have several on the way. Thanks!

SteveG


ReplyQuote
Rouverius
(@rouverius)
New Member
Joined: 1 month ago
Posts: 4
2019-10-14 3:32 am  

Wow, I forgot that that the EEPROM wasn't just for code storage.

Yeah, the update method seems like a great first step to reduce wear.

Thinking about basic wear leveling when using the external EEPROM....

What about selecting a random starting address before logging data? (Of course, the start address would then need to be stored on the onboard EEPROM) Wouldn't the randomization spread out the wear and not have it always focus on the first addresses of the EEPROM?

Are there other simple wear leveling techniques that you use?


ReplyQuote
Pugwash
(@pugwash)
Reputable Member
Joined: 5 months ago
Posts: 492
2019-10-22 11:09 am  

You may not realise it, but you might just have some EEPROM lying around unused in your modules box. 

Just take the DS3231 RTC module, for instance, it not only has a clock/alarm register, and the ability to supply 32k, 8k, 4k, 2k and 1k square waves but it also has an onboard 32kBit (4kByte) EEPROM storage chip. When I bought mine, I got a pack of 5 modules of which I have only one in use and didn't realise that the module was capable of more than just timekeeping, until I looked at the smaller chip under a magnifying glass.

IMG 4231

With the address pads left unconnected, as shown above, the I2C address for the EEPROM IC is 0x57 and the I2C address for the time register is 0x68.

I ran the following code from the DBWS site, with just a potentiometer and it worked perfectly.

/*
  External EEPROM Recording & Playback Demo
  ext_eeprom_demo.ino
  Uses AT24LC256 External I2C EEPROM

  DroneBot Workshop 2019
*/

// Include the I2C Wire Library
#include "Wire.h"

// Include the Servo Library
#include "Servo.h"

// EEPROM I2C Address
#define EEPROM_I2C_ADDRESS 0x57

// Analog pin for potentiometer
int analogPin = 0;

// Integer to hold potentiometer value
int val = 0;

// Byte to hold data read from EEPROM
int readVal = 0;

// Integer to hold number of addresses to fill
int maxaddress = 500;

// Create a Servo object
Servo myservo;


// Function to write to EEPROOM
void writeEEPROM(int address, byte val, int i2c_address)
{
  // Begin transmission to I2C EEPROM
  Wire.beginTransmission(i2c_address);

  // Send memory address as two 8-bit bytes
  Wire.write((int)(address >> 8));   // MSB
  Wire.write((int)(address & 0xFF)); // LSB

  // Send data to be stored
  Wire.write(val);

  // End the transmission
  Wire.endTransmission();

  // Add 5ms delay for EEPROM
  delay(5);
}

// Function to read from EEPROM
byte readEEPROM(int address, int i2c_address)
{
  // Define byte for received data
  byte rcvData = 0xFF;

  // Begin transmission to I2C EEPROM
  Wire.beginTransmission(i2c_address);

  // Send memory address as two 8-bit bytes
  Wire.write((int)(address >> 8));   // MSB
  Wire.write((int)(address & 0xFF)); // LSB

  // End the transmission
  Wire.endTransmission();

  // Request one byte of data at current memory address
  Wire.requestFrom(i2c_address, 1);

  // Read the data and assign to variable
  rcvData =  Wire.read();

  // Return the data as function output
  return rcvData;
}


void setup()
{
  // Connect to I2C bus as master
  Wire.begin();

  // Setup Serial Monitor
  Serial.begin(9600);
  Serial.println("ext_eeprom_demo.ino");

  // Attach servo on pin 9 to the servo object
  myservo.attach(9);

  // Print to Serial Monitor
  Serial.println("Start Recording...");

  // Run until maximum address is reached

  for (int address = 0; address <= maxaddress; address++) {

    // Read pot and map to range of 0-180 for servo
    val = map(analogRead(analogPin), 0, 1023, 0, 180);

    // Write to the servo
    // Delay to allow servo to settle in position
    myservo.write(val);
    delay(15);

    // Record the position in the external EEPROM
    writeEEPROM(address, val, EEPROM_I2C_ADDRESS);

    // Print to Serial Monitor
    Serial.print("ADDR = ");
    Serial.print(address);
    Serial.print("\t");
    Serial.println(val);

  }

  // Print to Serial Monitor
  Serial.println("Recording Finished!");

  // Delay 5 Seconds
  delay(5000);

  // Print to Serial Monitor
  Serial.println("Begin Playback...");

  // Run until maximum address is reached

  for (int address = 0; address <= maxaddress; address++) {

    // Read value from EEPROM
    readVal = readEEPROM(address, EEPROM_I2C_ADDRESS);


    // Write to the servo
    // Delay to allow servo to settle in position
    // Convert value to integer for servo
    myservo.write(readVal);
    delay(15);

    // Print to Serial Monitor
    Serial.print("ADDR = ");
    Serial.print(address);
    Serial.print("\t");
    Serial.println(readVal);

  }

  // Print to Serial Monitor
  Serial.println("Playback Finished!");

}

void loop()
{

  // Nothing in loop

}

For testing there was no need to connect a servo or comment out the servo code.

SteveC - Let there be light(God) - Let there be more light(Pink Floyd)


ReplyQuote
Pugwash
(@pugwash)
Reputable Member
Joined: 5 months ago
Posts: 492

ReplyQuote

Please Login or Register