Running a MCP23017 on 5V

apuder
Posts: 53
Joined: Mon Mar 26, 2018 7:27 pm

Re: Running a MCP23017 on 5V

Postby apuder » Fri Aug 14, 2020 12:10 am

here you go. Again, I am using the SPI variant; not I2C:

Code: Select all

 // Configure SPI bus
  spi_bus.flags = SPICOMMON_BUSFLAG_MASTER;
  spi_bus.sclk_io_num = SPI_PIN_NUM_CLK;
  spi_bus.mosi_io_num = SPI_PIN_NUM_MOSI;
  spi_bus.miso_io_num = SPI_PIN_NUM_MISO;
  spi_bus.quadwp_io_num = -1;
  spi_bus.quadhd_io_num = -1;
  spi_bus.max_transfer_sz = 32;
  esp_err_t ret = spi_bus_initialize(HSPI_HOST, &spi_bus, 0);
  ESP_ERROR_CHECK(ret);

// Configure SPI device for MCP23S08
  spi_mcp23S08.address_bits = 0;
  spi_mcp23S08.command_bits = 0;
  spi_mcp23S08.dummy_bits = 0;
  spi_mcp23S08.mode = 0;
  spi_mcp23S08.duty_cycle_pos = 0;
  spi_mcp23S08.cs_ena_posttrans = 0;
  spi_mcp23S08.cs_ena_pretrans = 0;
  spi_mcp23S08.clock_speed_hz = SPI_PORT_EXP_SPEED_MHZ * 1000 * 1000;
  spi_mcp23S08.spics_io_num = SPI_PIN_NUM_CS_MCP23S08;
  spi_mcp23S08.flags = 0;
  spi_mcp23S08.queue_size = 1;
  spi_mcp23S08.pre_cb = NULL;
  spi_mcp23S08.post_cb = NULL;
  ret = spi_bus_add_device(HSPI_HOST, &spi_mcp23S08, &spi_mcp23S08_h);
  ESP_ERROR_CHECK(ret);
 

Code: Select all

void writePortExpander(spi_device_handle_t dev, uint8_t cmd, uint8_t data)
{
  spi_transaction_t trans;

  memset(&trans, 0, sizeof(spi_transaction_t));
  trans.flags = SPI_TRANS_USE_TXDATA;
  trans.length = 3 * 8;   		// 3 bytes
  trans.tx_data[0] = MCP23S_CHIP_ADDRESS | MCP23S_WRITE;
  trans.tx_data[1] = cmd;
  trans.tx_data[2] = data;

  esp_err_t ret = spi_device_transmit(dev, &trans);
  ESP_ERROR_CHECK(ret);
}

uint8_t readPortExpander(spi_device_handle_t dev, uint8_t reg)
{
  spi_transaction_t trans;

  memset(&trans, 0, sizeof(spi_transaction_t));
  trans.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
  trans.length = 3 * 8;   		// 2 bytes
  trans.rxlength = 0;
  trans.tx_data[0] = MCP23S_CHIP_ADDRESS | MCP23S_READ;
  trans.tx_data[1] = reg;
  trans.tx_data[2] = 0;

  esp_err_t ret = spi_device_transmit(dev, &trans);
  ESP_ERROR_CHECK(ret);

  return trans.rx_data[2];
}

Code: Select all

// Configure INT as open drain
  writePortExpander(MCP23S08, MCP23S08_IOCON, MCP23S08_ODR);
  // Generate interrupt on pin change
  writePortExpander(MCP23S08, MCP23S08_INTCON, 0);
  // Enable interrupt-on-change
  writePortExpander(MCP23S08, MCP23S08_GPINTEN, 0xff);
  // Dummy read to clear INT
  readPortExpander(MCP23S08, MCP23S08_INTCAP);
 

mikronauts
Posts: 23
Joined: Wed Dec 09, 2015 8:11 pm

Re: Running a MCP23017 on 5V

Postby mikronauts » Fri Aug 14, 2020 4:45 am

I would be VERY surprised if the ADS1115 would not work @ 1.7Mhz i2c clock.
kissinno wrote:
Thu Aug 13, 2020 8:06 am
Thanks you very much for all your valuable comments.

My I2C:
ADS1115 : 100kHz, 400kHz, 3.4MHz
ADS1115 : 100kHz, 400kHz, 3.4MHz
MCP23017 :100kHz, 400kHz, 1.7MHz
I don't want to apply several I2C bus's, so I will have to limit to 400kHz. For 400kHz with 5cm bus, internal R's should be OK. At least, it is worth to try.

I was not aware of this open-drain INT output but I like it! I will re-investigate Datasheet....

Apuder, can't you share an SW exemple how to configure MCP23017 INT open-drain at boot? I guess this is only few line inside void setup().

kissinno
Posts: 10
Joined: Tue Apr 14, 2020 11:31 am

Re: Running a MCP23017 on 5V

Postby kissinno » Fri Aug 14, 2020 10:55 am

Cool, thanks Apuder :-)

BTW, what MCP23017 lib are your using? They are several but I haven't find yet any "advanced" lib" incuding Debounce, Longpress, Rizing & falling-edge detection.

So far, with classical button connected to GPIO, I was really in love with YABL, Yet Another Button Library (for Arduino).

This library augments the popular debounce library Bounce2 with the ability to set callbacks or easily check for the following button events: Press, Release, Hold, Tap, Double-tap...
https://github.com/yergin/yabl

I'm particulary looking for button double-click detection from MCP23017 input. Anyone has ever done this?

apuder
Posts: 53
Joined: Mon Mar 26, 2018 7:27 pm

Re: Running a MCP23017 on 5V

Postby apuder » Fri Aug 14, 2020 4:56 pm

kissinno wrote:
Fri Aug 14, 2020 10:55 am
BTW, what MCP23017 lib are your using?
I am not using any library; only the standard ESP-IDF SPI API.

DOugL-
Posts: 21
Joined: Sun Dec 18, 2022 6:00 pm

Re: Running a MCP23017 on 5V

Postby DOugL- » Thu Feb 16, 2023 1:35 am

Long in short: is it OK to connect an ESP via I2C to a MCP23017 running on 5V without any level-shifters?
If you have 4.7K pullups to +5V then put 10K pull downs to ground will get you a 3.4V level which could work without level shifters.
A quick test on an old Adafruit LCD shield(has MCP23017 onboard to handle buttons and 16x2 display) and worked fine.

Who is online

Users browsing this forum: MicroController and 94 guests