Library for I2C EEPROM for ATtiny85

Hi

I need to use an external EEPROM chip with an ATtiny85, and I want to use I2C communications with it. I have found the TinyWireM library GitHub - adafruit/TinyWireM: I2C library for Trinket and Gemma, adapted from BroHogan's code on Arduino Playground , and the base library for the I2C EEPROM chip I want to use GitHub - CascoLogix/CAT24M01 which uses the Wire library.

I am having a difficult time making this work however and was wondering if anyone might be able to provide some insight. I attached the modified CAT24M01 and TinyWireM library files, and an example sketch I am working with:

#include <CAT24M01_TinyWireM.h>
#include <TinyWireM.h>

unsigned char testdata[5] = {0,1,2,3,4};

CAT24M01_TinyWireM EEPROM1(0);    

void setup() 
{
  pinMode(1, OUTPUT);

  EEPROM1.begin();   
  EEPROM1.write(0, testdata, 5);
  digitalWrite(1, HIGH);
  EEPROM1.read(0, testdata, 5);
}

void loop() 
{                
}

The sketch compiles without errors but the LED does not turn on. If the digital write command is moved before the eeprom write command it lights.

I think there is an issue with the TinyWireM library but I cannot sort it out. I would highly appreciate any insights you can provide.

CAT24M01_TinyWireM.h (993 Bytes)

CAT24M01_TinyWireM.cpp (3.42 KB)

TinyWireM.h (3.78 KB)

TinyWireM.cpp (4.08 KB)

I had to make some corrections in the library - apologies there were some bugs that apparently had never been flushed out.

You should be able to download the newly checked in library files to update your local copy. After doing that, please try the updates to your sketch shown here.

#include <CAT24M01.h>
//#include <TinyWireM.h>

unsigned char testdata[5] = {0,1,2,3,4};

CAT24M01 myEEPROM(0x0A);    

void setup() 
{
 pinMode(1, OUTPUT);

 myEEPROM.begin();   
 myEEPROM.write(0, testdata, 5);
 digitalWrite(1, HIGH);
 myEEPROM.read(0, testdata, 5);
}

void loop() 
{                
}

Note: I changed the way the EEPROM instance is used, both the name and the fact that we don’t need an array of instances here. Also had to add the I2C address to the constructor. Also, I commented out the TinyWireM include, as you should be able to just use the standard Wire library. The include for TinyWireM was causing a lot of compile errors for me, which I didn’t try to resolve. Just use the Wire library.

I thought I had this resolved and was about to post that the problem had been solved, but I am still having some issues with getting the LED to come on. It is getting hung up in the write function and not returning to the main program. I’ll have to keep working on this tomorrow with actual EEPROM hardware connected to see what is happening. Sorry no solution yet…

Thanks. I thought TinyWire was required for I2C coms with ATtiny devices?

When I compile I get an error related to Wire.cpp:
"
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\Wire.cpp:85:3: error: 'TWBR' was not declared in this scope
TWBR = ((F_CPU / frequency) - 16) / 2;
"

I modified microcas updated libraries to try to work with the ATtiny85. I used SoftwareSerial, and TinyWireM. I attached the modified libraries. However, I am getting exactly the same result as I did with the original libraries.

The serial monitor output shows:
"

Uno Ready
str1 length is: 19
19 bytes written.
64 bytes read.

Done.

"

There are several problems with these results:

  1. The line from setup: " TinySerial.println(“ATtiny85 ready”); " is not being printed.
  2. It hangs after printing “str1 length is: 19”. It will hang here, believe it or not, until I touch the SCL breadboard wire insulation, after which it continues to print the rest of the output.
  3. It does not print the data read from the EEPROM, that should be printed in the blank line after “64 bytes read”.

I think there is a problem with SoftwareSerial and the ATtiny85 or TinyWireM. I am using an Uno as a Serial to USB converter to send the serial data to the PC. Here is the code I am running in the Uno to forward the data. Does there seem to be any problem with this? I have no idea why it’s not printing the first line from the ATtiny85. I tried adding delays also.

//  receive serial from ATtiny85 and print to serial monitor

#include <SoftwareSerial.h>
SoftwareSerial ATtiny85Serial(2, 3); // RX, TX

void setup() {
  Serial.begin(9600);
  ATtiny85Serial.begin(9600);
  Serial.println("Uno Ready");
}


// send serial monitor data to ATtiny85, send ATtiny85 data to serial monitor.
void loop() {
  if (ATtiny85Serial.available()) {
    Serial.write(ATtiny85Serial.read());
  }
}

CAT24M01_N_TinyWireM.cpp (3.48 KB)

CAT24M01_N_TinyWireM.h (1.01 KB)

Ok problem #2 is fixed by changing the instantiation from EEPROM to EEPROM1, now it runs smoothly every time.

Update:

Changing the Uno code solved problem #1:

    while (ATtiny85Serial.available() > 0)
    {
        char received = ATtiny85Serial.read();
        inData += received; 

        // Process message when new line character is recieved
        if (received == '\n')
        {
            Serial.println(inData);
            inData = " "; // Clear received buffer
        }
    }

So now I’m left with why doesn’t it print the data from the EEPROM read?