Page 1 of 5
I2C EEPROM on uPy/ESP8266?
Posted: Tue Jan 10, 2017 11:32 am
by warren
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
Re: I2C EEPROM on uPy/ESP8266?
Posted: Tue Jan 10, 2017 12:15 pm
by Roberthh
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
Re: I2C EEPROM on uPy/ESP8266?
Posted: Wed Jan 11, 2017 10:42 am
by ioukos
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 !
Re: I2C EEPROM on uPy/ESP8266?
Posted: Thu Jan 12, 2017 1:10 pm
by warren
Hmmm OK - thank you that is helpful.
Just waiting for my EEPROM to arrive.....
Re: I2C EEPROM on uPy/ESP8266?
Posted: Fri Jan 13, 2017 6:40 am
by mcauser
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'
Re: I2C EEPROM on uPy/ESP8266?
Posted: Tue Jan 17, 2017 6:59 pm
by warren
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?
Re: I2C EEPROM on uPy/ESP8266?
Posted: Tue Jan 17, 2017 10:11 pm
by mcauser
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.
Re: I2C EEPROM on uPy/ESP8266?
Posted: Tue Jan 17, 2017 10:26 pm
by warren
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..
Re: I2C EEPROM on uPy/ESP8266?
Posted: Sat Jan 28, 2017 6:26 pm
by ioukos
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 ?
Re: I2C EEPROM on uPy/ESP8266?
Posted: Sun Jan 29, 2017 1:30 pm
by mikruth
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.