HCMCP23017 - Library for MCP23017 Serial 16bit Expander IC

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 865
Joined: Sun Aug 05, 2012 4:02 pm

HCMCP23017 - Library for MCP23017 Serial 16bit Expander IC

Post by admin » Fri Jun 24, 2016 1:57 pm

Image



Arduino library for the Microchip Technology MCP23017 (I2C) serial 16 bit I/O expander IC. Currently supported products:

MCP23017 Serial 16bit Expander IC (HCCOIC0048) available from hobbycomponents.com

This library allows a suitable Arduino to set and read the state of the 16 I/O available on the MCP23017 I/O expander IC via its I2C interface.



You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/


Using the HCMCP23017 library

To use the library just include the HCMCP23017.h header file and then create an instance of the library. E.g:

Code: Select all

#include "HCMCP23017.h"
HCMCP23017 HCMCP23017(I2CAdd);

Where I2CAdd is the I2C slave address of the device. The MCP23017 can be set to one of 8 different slave addresses depnding on the state of the 3 slave address pins (A0-A2).


To initialise the library add the following line to the setup() section of your sketch:

Code: Select all

HCMCP23017.Init();




The following functions are available with this library:

Code: Select all

HCMCP23017.pinDir(Pin, Dir);
Sets the direction (input/output) of one of the 16 I/O pins where:

Pin is the pin number to set the direction for. Valid values for Pin are
0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

Dir is the required pin direction. Valid values are
PIN_INPUT (sets the specified pin to an input)
PIN_OUTPUT (sets the specified pin to an output)



Code: Select all

HCMCP23017.pinPullup(Pin, State);
Sets the state (enabled/disabled) of one of the 16 internal pullups where:

Pin is the pin number to set the pullup state for. Valid values for Pin are
0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

State is the required pullup state. Valid values are
PULLUP_ENABLED (enables the pullup for the specified pin)
PULLUP_DISABLED (disables the pullup for the specified pin)



Code: Select all

HCMCP23017.pinWrite(Pin, State);
Sets the output state (high/low) of one of the 16 I/O pins where:

Pin is the pin number to set the output state for. Valid values for Pin are
0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

State is the required output state. Valid values for State are
HIGH (pin is driven high)
LOW (pin is driven low)



Code: Select all

boolean State = HCMCP23017.pinRead(Pin);
Gets the input state (high/low) of one of the 16 I/O pins where:

Pin is the pin number to get the input state for. Valid values for Pin are
0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

Returns a boolean value representing the current state of the pin.



Code: Select all

HCMCP23017.dirA(Dir);
Sets the direction (input/output) of all 8 pins assigned to port A where:

Dir is the 8 bit pattern which sets the direction of all 8 pins. Valid values for Dir are:

0x00 to 0xFF (setting any bit to zero will set the appropriate pin to an output, setting it to a 1 will set the pin to an input)
ALL_INPUTS (sets all 8 pins of port A to inputs)
ALL_OUTPUTS (sets all 8 pins of port A to outputs)



Code: Select all

HCMCP23017.dirB(Dir);
Sets the direction (input/output) of all 8 pins assigned to port B where:

Dir is the 8 bit pattern which sets the direction of all 8 pins. Valid values for Dir are:

0x00 to 0xFF (setting any bit to zero will set the appropriate pin to an output, setting it to a 1 will set the pin to an input)
ALL_INPUTS (sets all 8 pins of port B to inputs)
ALL_OUTPUTS (sets all 8 pins of port B to outputs)


Code: Select all

HCMCP23017.pullupA(byte State);
Sets the state (enabled/disabled) of all 8 internal pullups assigned to port
A where:

State is the 8 bit pattern which sets the state of all 8 pullups. Valid values for State are:

0x00 to 0xFF (setting any bit to zero will disable the pullup for the corresponding pin, setting it to a 1 will will enable the pullup)
ALL_PULLUPS_ENABLED (enables all 8 pullups of port A)
ALL_PULLUPS_DISABLED (disables all 8 pullups of port A)


Code: Select all

HCMCP23017.pullupB(byte State);
Sets the state (enabled/disabled) of all 8 internal pullups assigned to port
A where:

State is the 8 bit pattern which sets the state of all 8 pullups. Valid values for State are:

0x00 to 0xFF (setting any bit to zero will disable the pullup for the corresponding pin, setting it to a 1 will will enable the pullup)
ALL_PULLUPS_ENABLED (enables all 8 pullups of port B)
ALL_PULLUPS_DISABLED (disables all 8 pullups of port B)



Code: Select all

HCMCP23017.outA(Data);
Sets the output state (high/low) of all 8 pins assigned to port A where:

Data is the 8 bit pattern which sets the state of all 8 pins. Valid values for Data are:

0x00 to 0xFF (setting any bit to zero will set the appropriate pin output to a low state, setting it to a 1 will set the output to a high state).



Code: Select all

HCMCP23017.outB(Data);
Sets the output state (high/low) of all 8 pins assigned to port B where:

Data is the 8 bit pattern which sets the state of all 8 pins. Valid values for Data are:

0x00 to 0xFF (setting any bit to zero will set the appropriate pin output to a low state, setting it to a 1 will set the output to a high state).


Code: Select all

HCMCP23017.out(Data);
Sets the output pin state for all 16 pins (port A & B) where:

Data is a 16 bit pattern representing the state off all 16 pins (bit 0 = port
A pin 0, bit 15 = port b pin 7). Valid values for Data are:
0x0000 to 0xFFFF



Code: Select all

byte Result = HCMCP23017.inA();
Gets the input state (high/low) of all 8 pins assigned to port A.

Returns an 8 bit byte containing the state of the pins (bit 0 = port A pin 0,
bit 7 = port A pin 7)



Code: Select all

byte Result = HCMCP23017.inB();
Gets the input state (high/low) of all 8 pins assigned to port B.

Returns an 8 bit byte containing the state of the pins (bit 0 = port B pin 0,
bit 7 = port B pin 7)


Code: Select all

unsigned int Result = HCMCP23017.in();
Gets the input state (high/low) of all 16 pins (port A & B).

Returns a 16 bit unsigned integer containing the state of the pins (bit 0 = port A pin 0, bit 15 = port B pin 7)



Code: Select all

HCMCP23017.I2CWriteByte(Register,  Data);
Writes a single byte of data to one of the devices I2C registers where:

Register is the address of the register to write to. See the HCMCP23017.h
header file for a list of valid addressed

Data is the 8 bit value to be written



Code: Select all

HCMCP23017.I2CWriteInt(Register, Data);
Writes a 16 bit value to two consecutive register pairs where:

Register is the first address of the registers to write to.
See the HCMCP23017.h header file for a list of valid addressed

Data is the 16 bit value to be written



Code: Select all

byte Data = HCMCP23017.I2CReadByte(Register);
Reads a single byte of data from one of the devices I2C registers where:

Register is the address of the register to read from. See the HCMCP23017.h
header file for a list of valid addressed

Returns a byte value containing the current state of the register.



Code: Select all

unsigned int Data = HCMCP23017.I2CReadInt(Register);

Reads a 16 bit value from two consecutive register pairs where:

Register is the first address of the registers to read from.
See the HCMCP23017.h header file for a list of valid addressed

Returns a 16 bit unsigned integer containing the current state of the
registers.


Image

  1. /* FILE:    HCMCP23017_Pin_Read_Example
  2.    DATE:    24/06/16
  3.    VERSION: 0.1
  4.    AUTHOR:  Andrew Davies
  5.  
  6.    Sketch created by Hobby Components Ltd (HOBBYCOMPONENTS.COM)
  7.    
  8. 24/06/16 version 0.1: Original version
  9.  
  10. This example sketch demonstrates how to use the HCMCP2017 library to read the input
  11. state of one of the 16 I/O pins (GPA0/Pin 21) on the MCP23017. The sketch
  12. will continually read and output the state of pin 7 on port B. Currently supported
  13. products:
  14.  
  15. MCP23017 Serial 16 bit Expander IC (HCCOIC0048)
  16.  
  17. Arduino Connections:
  18.  
  19. Uno/Nano............MCP23017 (DIP)
  20. +5V.................VDD (Pin 9)
  21. GND.................VSS (Pin 10)
  22. A4..................SDA (Pin 13)
  23. A5..................SCL (Pin 13)
  24.  
  25. This sketch also assumes the slave 3 address pins (A0-A2) on the MCP23017
  26. are pulled low.
  27.  
  28. You may copy, alter and reuse this code in any way you like, but please leave
  29. reference to HobbyComponents.com in your comments if you redistribute this code.
  30. This software may not be used directly for the purpose of selling products that
  31. directly compete with Hobby Components Ltd's own range of products.
  32.  
  33. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  34. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  35. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  36. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  37. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  38. REASON WHATSOEVER.
  39. */
  40.  
  41. /* Include the HCMCP23017 library */
  42. #include <HCMCP23017.h>
  43.  
  44. /* Create an instance of the library and set the I2C slave address */
  45. HCMCP23017 HCMCP23017(0x20);
  46.  
  47. void setup()
  48. {
  49.   /* Initialise the library and serial interface*/
  50.   HCMCP23017.Init();
  51.   Serial.begin(9600);
  52.  
  53.   /* Set the pin to an input (default state) */
  54.   HCMCP23017.pinDir(GPB7, PIN_INPUT);
  55.  
  56.   /* Enable its internal pullup */
  57.   HCMCP23017.pinPullup(GPB7, PULLUP_ENABLED);
  58. }
  59.  
  60.  
  61. void loop()
  62. {
  63.   Serial.print("Port B pin 7: ");
  64.  
  65.   /* Read the state of the pin */
  66.   if(HCMCP23017.pinRead(GPB7))
  67.   {
  68.     Serial.println("HIGH");
  69.   }else
  70.   {
  71.     Serial.println("LOW");
  72.   }
  73.  
  74.   delay(500);
  75. }


Image
HCMCP23017.zip
You do not have the required permissions to view the files attached to this post.

ChrisSharp
Posts: 30
Joined: Thu Dec 31, 2020 1:22 pm

Re: HCMCP23017 - Library for MCP23017 Serial 16bit Expander IC

Post by ChrisSharp » Sat Nov 27, 2021 1:44 pm

Hi
Gets the output state (high/low) of one of the 16 I/O pins where:

Pin is the pin number to get the input state for. Valid values for Pin are
0 to 15 (0 = pin 0 on port A, 15 = pin 7 on port B)

Returns a boolean value representing the current state of the pin.
I think the first line of this should read:

Gets the input state (high/low) of one of the 16 I/O pins where:

This through me when I was skim reading and looking for how to read a single pin.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMCP23017 - Library for MCP23017 Serial 16bit Expander IC

Post by andrew » Mon Nov 29, 2021 9:13 am

Thanks for pointing it out, it's now been corrected.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “Arduino”