I/O expander

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
joehunt588
Posts: 26
Joined: Wed Jul 27, 2016 5:06 am

I/O expander

Post by joehunt588 » Fri Jul 29, 2016 7:40 am

Hai guy can suggest any working i/o expander for esp8266 that using i2c?

im currently struggle using i2c for mcp23017

[code]
from machine import Pin, I2C

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)

#DEVICE = 0x20 # Device address (A0-A2)
#IODIRA = 0x00 # Pin direction register
#OLATA = 0x14 # Register for outputs
#GPIOA = 0x12 # Register for inputs

#register addr
addr = bytearray([0x00,0x00])
i2c.writeto(0x20, addr) <--- error Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object with buffer protocol required
[/code]

Please help me using mcp23017 on esp8266 (previous using code in rpi) or suggest easy way in using i/o expander
<link>https://cdn-shop.adafruit.com/datasheet ... .pdf</link>

User avatar
pythoncoder
Posts: 4793
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I/O expander

Post by pythoncoder » Sat Jul 30, 2016 5:00 am

I suspect the cause is out of date firmware. That error should not occur. Here I get

Code: Select all

>>> i2c.writeto(0x20, addr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: I2C bus error
which is to be expected as I haven't got the hardware connected. It is accepting the bytearray and trying to write to the nonexistent kit.
Peter Hinch

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

Re: I/O expander

Post by mcauser » Sat Jul 30, 2016 1:21 pm

I'm using a PCF8574 "backpack" with a 1602 LCD and a WeMos D1 mini.

They are also sold as a regular I/O module, without 16 pin LCD specific header.
http://www.aliexpress.com/item/PCF8574- ... 58823.html

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

Re: I/O expander

Post by mcauser » Sat Jul 30, 2016 1:23 pm

Try dropping the I2C frequency and different pull-up resistor values.
I was unable to get an I2C temperature sensor working higher than 50,000 without I2C bus error messages.

shaoziyang
Posts: 347
Joined: Sun Apr 17, 2016 1:55 pm

Re: I/O expander

Post by shaoziyang » Sat Jul 30, 2016 2:22 pm

Good idea.

joehunt588
Posts: 26
Joined: Wed Jul 27, 2016 5:06 am

Re: I/O expander

Post by joehunt588 » Mon Aug 01, 2016 4:39 pm

Hi pythoncoder

i using firmware micropython 1.8.2,what version you use?
Last edited by joehunt588 on Tue Aug 02, 2016 10:47 am, edited 1 time in total.

joehunt588
Posts: 26
Joined: Wed Jul 27, 2016 5:06 am

Re: I/O expander

Post by joehunt588 » Tue Aug 02, 2016 10:34 am

Hi mcauser,

how can i scan i2c port? when i using i2c.scan() it show result in list [4,8 -> 41],by default it 0x20.
can you share me code for PCF8574A python ,i now order the chip :D

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

Re: I/O expander

Post by mcauser » Tue Aug 02, 2016 1:42 pm

i2c.scan() returns results in decimal. 0x20 == 32.

If you are seeing a bunch of unrelated scan results, you may be missing pull-up resistors.
I saw that when I was working with a DHT12 sensor. Added pull-ups and scan then returned a single item.

Here is an example of using WeMos D1 mini + PCF8574 I2C expander + 1602 HD44780 character LCD:
https://github.com/dhylands/python_lcd/pull/2

joehunt588
Posts: 26
Joined: Wed Jul 27, 2016 5:06 am

Re: I/O expander

Post by joehunt588 » Tue Aug 02, 2016 3:41 pm

Hi mcauser

thank you for the code appreciate :D .
may i know where do i need put resistor?do i need to put resistor for SDA and SCL line?Sorry for my noob question do you have schematic where i can follow to put the pull out resistor?

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

Re: I/O expander

Post by warren » Tue Aug 02, 2016 5:30 pm

joehunt588 wrote:Hi mcauser

thank you for the code appreciate :D .
may i know where do i need put resistor?do i need to put resistor for SDA and SCL line?Sorry for my noob question do you have schematic where i can follow to put the pull out resistor?
Yes, on both.

The optimal value for the resistors depends on clock speed and cable type.

If you intend to have any I2C sensors further than a few tens of cm away from the ESP, you may need to reduce the clock speed. In my tests with BMP280s, reducing the clock speed to 10kHz introduces only a slight delay.

There are a couple of helpful links about I2C resistances here:

http://dsscircuits.com/articles/effects ... -resistors

and

https://rheingoldheavy.com/i2c-pull-resistors/

Post Reply