I2C EEPROM on uPy/ESP8266?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

I2C EEPROM on uPy/ESP8266?

Post by warren » Tue Jan 10, 2017 11:32 am

Hi,

I want to add an i2c EEPROM to an ESP sensors - is there any existing code / library available that I can use/modify?

Thanks

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: I2C EEPROM on uPy/ESP8266?

Post by Roberthh » Tue Jan 10, 2017 12:15 pm

If you just want to read/write data, the existing I2C lib works well. At least, I used that for a quick hack, and just followed the examples in the doc, believe it or not, here:
http://docs.micropython.org/en/latest/e ... hlight=i2c
and here:
http://docs.micropython.org/en/latest/e ... ml#i2c-bus

User avatar
ioukos
Posts: 34
Joined: Wed Oct 19, 2016 11:31 am
Location: Alsace, Europe

Re: I2C EEPROM on uPy/ESP8266?

Post by ioukos » Wed Jan 11, 2017 10:42 am

Hi warren,

I've found this library on Github but didn't try it :
https://github.com/dda/MicroPython/blo ... /EEPROM.py

Share the result if you give it a try !

warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: I2C EEPROM on uPy/ESP8266?

Post by warren » Thu Jan 12, 2017 1:10 pm

Roberthh wrote:If you just want to read/write data, the existing I2C lib works well. At least, I used that for a quick hack, and just followed the examples in the doc, believe it or not, here:
http://docs.micropython.org/en/latest/e ... hlight=i2c
and here:
http://docs.micropython.org/en/latest/e ... ml#i2c-bus
Hmmm OK - thank you that is helpful.

Just waiting for my EEPROM to arrive.....

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: I2C EEPROM on uPy/ESP8266?

Post by mcauser » Fri Jan 13, 2017 6:40 am

i2c.readfrom_mem + i2c.writeto_mem is what you are after.
http://docs.micropython.org/en/latest/e ... e.I2C.html

Code: Select all

>>> from machine import I2C, Pin
>>> i2c = I2C(scl=Pin(5), sda=Pin(4), freq=50000)
>>> i2c.scan()

Read 11 bytes starting at memory address 0
>>> i2c.readfrom_mem(0x50, 0, 11)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Write "hello " at the beginning
>>> i2c.writeto_mem(0x50, 0, bytearray([104, 101, 108, 108, 111, 32]))

What is 104? It's the ascii value for "h".
>>> ord('h')
104

Read 11 bytes
>>> i2c.readfrom_mem(0x50, 0, 11)
b'hello \x00\x00\x00\x00\x00'

# write "world" starting at memory address 6
>>> i2c.writeto_mem(0x50, 6, bytearray('world'))

# read 11 bytes
>>> i2c.readfrom_mem(0x50, 0, 11)
b'hello world'

warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: I2C EEPROM on uPy/ESP8266?

Post by warren » Tue Jan 17, 2017 6:59 pm

mcauser wrote:i2c.readfrom_mem + i2c.writeto_mem is what you are after.
http://docs.micropython.org/en/latest/e ... e.I2C.html

Code: Select all

>>> from machine import I2C, Pin
>>> i2c = I2C(scl=Pin(5), sda=Pin(4), freq=50000)
>>> i2c.scan()

Read 11 bytes starting at memory address 0
>>> i2c.readfrom_mem(0x50, 0, 11)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Thanks for the detailed reply. It is close to working I think!

There is a 'write protect' pin on the chip. This has to be 'low' to enable writing - it is wired to ground.

i2c.scan() yields a reply of "(80)".

This reply changes if/when I change the A0/A1/A2 address pins on the chip between high/low...

If I then follow your code example, I get the following:

Code: Select all

>>> i2c.writeto_mem(0x50, 0, bytearray([104, 101, 108, 108, 111, 32]))
>>> i2c.readfrom_mem(0x50, 0, 11)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'

The EEPROM is a 24AA512. The data sheet says that random reads muct be preceeded with a write operation to set the start address within the chip... I have zero experience with EEPROMs sadly...

Any thoughts?

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: I2C EEPROM on uPy/ESP8266?

Post by mcauser » Tue Jan 17, 2017 10:11 pm

Was there a pause between writeto_mem() and readfrom_mem(). It may have not finished writing before being read.
Either run the write and read lines separately, or use the time module to add a pause.
import time
time.sleep_ms(100)

The readfrom_mem() internally performs a SPI write to set the start address before making a SPI read of n bytes. You don't have to do anything extra here.

Those A0/A1/A2 address pins will change the 0x50 (80) to 0x51 (81), 0x52 (82) etc.
i2c.scan() returns the base10 value 80, which is 0x50 in base16 (hex).
You can use either i2c.writeto_mem(0x50, ...) or i2c.writeto_mem(80, ...). Makes no difference.
Most people use the hex value as it matches the datasheet.

warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: I2C EEPROM on uPy/ESP8266?

Post by warren » Tue Jan 17, 2017 10:26 pm

Thanks for the extra info - helpful..
mcauser wrote:Was there a pause between writeto_mem() and readfrom_mem().
Yes, I was entering the lines manually and waited between write and read instructions.

If I use the freq=50000 as in your code, it generates errors when reading. I had to reduce the freq to 20000

Does that provide any clues?

I have reduced it right down to 5000 and get exactly the same results..

User avatar
ioukos
Posts: 34
Joined: Wed Oct 19, 2016 11:31 am
Location: Alsace, Europe

Re: I2C EEPROM on uPy/ESP8266?

Post by ioukos » Sat Jan 28, 2017 6:26 pm

Hi Guys,

I've just received my 24c356 and I'm unable to use it with mcauser code.

I use a Wemos D1 mini, VCC is connected to 3.3, when I scan i got : 80.

Something's weird, when I try to write values in the EEPROM and then I try to read twice the results I got 2 different results :

Code: Select all

>>> i2c.writeto_mem(0x50, 0, bytearray([104, 101, 108, 108, 111, 32]))          
>>> i2c.readfrom_mem(0x50, 0, 6)                                                
b'Qc\x83`\xc9<'                                                                 
>>> i2c.readfrom_mem(0x50, 0, 6)                                                
b')\xcd\x96\xd2\x95\xa3'                                                        
 
Do you have a solution ?

Warren, did you succeed ?

My goal is to store on eeprom a timestamp (unix) and a value from a sensor, do you think it's possible ?

mikruth
Posts: 19
Joined: Wed Jan 11, 2017 6:00 pm

Re: I2C EEPROM on uPy/ESP8266?

Post by mikruth » Sun Jan 29, 2017 1:30 pm

Seems to be a problem as I am unable to read an eeprom on my ESP-12.
I can read/write ok on Arduino but can't read it back on ESP.
I get the result... xff from every location.

Post Reply