MCP23017 Port Expander
Posted: Mon Jan 23, 2017 11:49 pm
I am trying to use a MCP23017 port expander in c via i2c with the ESP32. The only example code for i2c I have been able to find is https://github.com/espressif/esp-idf/tr ... herals/i2c. This code is for a light sensor.
Arduino has some code for using this IC on the aduino uno, but I have been unable to get any code working.
Any help would be greatly appreciated.
Arduino has some code for using this IC on the aduino uno, but I have been unable to get any code working.
Code: Select all
//Arduino Code for the MCP23017
#include <Wire.h>
void setup() { Wire.begin(); //creates a Wire object
// set I/O pins to outputs
Wire.beginTransmission(0x20); //begins talking to the slave device
Wire.write(0x00); //selects the IODIRA register
Wire.write(0x00); //this sets all port A pins to outputs
Wire.endTransmission(); //stops talking to device
Wire.beginTransmission(0x20);//begins talking again to slave device
Wire.write(0x01); //selects the IODIRB register
Wire.write(0x00); // sets all port B pins to outputs
Wire.endTransmission(); //ends communication with slave device
}
void loop()
{
Wire.beginTransmission(0x20); //starts talking to slave device
Wire.write(0x12); //selects the GPIOA pins
Wire.write(00000011); // turns on pins 0 and 1 of GPIOA
Wire.endTransmission(); //ends communication with the device
Wire.beginTransmission(0x20); //starts talking to slave device
Wire.write(0x13); //selects the GPIOB pins
Wire.write(00000001); //turns on pin 0 of GPIOA
Wire.endTransmission();//ends communication with the device
}