MCP23017 - Arduino - set outputs to active-high

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
brad_mc
 
Posts: 2
Joined: Mon Aug 04, 2014 10:52 am

MCP23017 - Arduino - set outputs to active-high

Post by brad_mc »

Hello all,
I am working on a project that uses an Arduino UNO and an MCP23017 I2C port expander. I am using the most current Adafruit library for the expander. Downstream from the MCP23017 is a set of PNP MOSFETs that operate 12v Automotive relays that in some cases control up to 30A devices. It is not possible in this project to use NPN MOSFETs to switch the ground leg of the automotive relays, so I need the output pins of the MCP23017 to sink current. I have that part working fine after researching and referrring to the datasheet. My problem is that by default the output pins are Active-Low, which in the case of sinking current, means my circuits are all "On" at power on. This is not acceptable in the case of circuits in the vehicle. I added lines to my code to digitalWrite the MCP output pins to HIGH right after they are set for pinmode output. That effectly turns my circuits off, but there is a very brief "On" state (active-low) as the board and code initiallize after power on. Even that brief blip is not acceptable. I've looked through the Adafuit library, and I don't see any commands that will set the output pins to Active-High as a default prior to initialization. I have seen reference to sending the bits to do so in other libraries, but I really like the cleanliness of the Adafruit library, and this is where I got the MCP. :) I'd rather stick with this library.

Thanks in advance,
Brad

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: MCP23017 - Arduino - set outputs to active-high

Post by adafruit_support_rick »

Actually, the library initializes the pins to all input.You can change the library to initialize the pins to output with an initial high value:

Code: Select all

void Adafruit_MCP23017::begin(uint8_t addr) {
	if (addr > 7) {
		addr = 7;
	}
	i2caddr = addr;

	Wire.begin();

	// set defaults!
	// all outputs on ports A and B
	writeRegister(MCP23017_IODIRA,0x00);
	writeRegister(MCP23017_IODIRB,0x00);
	// all outputs HIGH
	writeGPIOAB(0xFFFF);
}

User avatar
brad_mc
 
Posts: 2
Joined: Mon Aug 04, 2014 10:52 am

Re: MCP23017 - Arduino - set outputs to active-high

Post by brad_mc »

Thanks! That got it. It even trimmed a bit off the compiled size since I'm not making 12 unnecessary pinmode declarations. I am running pretty tight on space as it is. One thing to note for others that may stumble upon this thread... When I added the code as Rick posted it, I achieved the goal of setting Active-High state on all outputs, but there was still a very fast blip of it being active-low when it first powered up. I swapped the line order in the library, so it now looks like this:

Code: Select all

void Adafruit_MCP23017::begin(uint8_t addr) {
	if (addr > 7) {
		addr = 7;
	}
	i2caddr = addr;

	Wire.begin();
	// all outputs HIGH
	writeGPIOAB(0xFFFF);
	// set defaults!
	// all inputs on port A and B
	writeRegister(MCP23017_IODIRA,0x00);
	writeRegister(MCP23017_IODIRB,0x00);
	
}
Moving the "writeGPIOAB(0xFFFF);" line up above the register lines sets them to default active-high before they initialize <- (not sure I'm using the right term there).

Thanks again for the help!!
Brad

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: MCP23017 - Arduino - set outputs to active-high

Post by adafruit_support_rick »

Cool :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”