Advertisement
microrobotics

MCP23017 I2C 16-bit I/O expander

Apr 4th, 2023
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This code turns on and off each output of the MCP23017 I2C 16-bit I/O expander one by one, with a 500ms delay between each output change.
  3.  
  4. Connect the MCP23017 I2C 16-bit I/O expander to the Arduino as follows:
  5.  
  6. VCC pin to 5V (or 3.3V, depending on your module's voltage level)
  7. GND pin to GND
  8. SDA pin to the SDA pin on your Arduino (A4 on the Uno, Leonardo, or Nano; pin 20 on the Mega)
  9. SCL pin to the SCL pin on your Arduino (A5 on the Uno, Leonardo, or Nano; pin 21 on the Mega)
  10. If you have a different I2C address for your MCP23017, you can change the mcp23017Address constant accordingly.
  11.  
  12. Please note that you may need to use pull-up resistors (typically 4.7kΩ) for the SDA and SCL lines. Some MCP23017 modules might already have pull-up resistors on board; in that case, you don't need to add external ones.
  13. */
  14.  
  15. #include <Arduino.h>
  16. #include <Wire.h>
  17.  
  18. const byte mcp23017Address = 0x20; // I2C address of the MCP23017
  19.  
  20. // MCP23017 register addresses
  21. const byte IODIRA = 0x00;
  22. const byte IODIRB = 0x01;
  23. const byte GPIOA  = 0x12;
  24. const byte GPIOB  = 0x13;
  25.  
  26. void setup() {
  27.   Wire.begin();
  28.   Serial.begin(9600);
  29.   Serial.println("MCP23017 I2C 16-bit I/O Expander Example");
  30.  
  31.   // Set all pins as outputs
  32.   writeRegister(mcp23017Address, IODIRA, 0x00);
  33.   writeRegister(mcp23017Address, IODIRB, 0x00);
  34. }
  35.  
  36. void loop() {
  37.   // Turn on and off each output one by one
  38.   for (byte output = 0; output < 16; output++) {
  39.     setOutput(output, HIGH);
  40.     delay(500);
  41.     setOutput(output, LOW);
  42.     delay(500);
  43.   }
  44. }
  45.  
  46. void setOutput(byte output, byte state) {
  47.   byte registerAddress;
  48.   byte bitPosition;
  49.  
  50.   if (output < 8) {
  51.     registerAddress = GPIOA;
  52.     bitPosition = output;
  53.   } else {
  54.     registerAddress = GPIOB;
  55.     bitPosition = output - 8;
  56.   }
  57.  
  58.   static byte portAStates = 0;
  59.   static byte portBStates = 0;
  60.  
  61.   if (registerAddress == GPIOA) {
  62.     if (state == HIGH) {
  63.       portAStates |= (1 << bitPosition);
  64.     } else {
  65.       portAStates &= ~(1 << bitPosition);
  66.     }
  67.     writeRegister(mcp23017Address, GPIOA, portAStates);
  68.   } else {
  69.     if (state == HIGH) {
  70.       portBStates |= (1 << bitPosition);
  71.     } else {
  72.       portBStates &= ~(1 << bitPosition);
  73.     }
  74.     writeRegister(mcp23017Address, GPIOB, portBStates);
  75.   }
  76. }
  77.  
  78. void writeRegister(byte deviceAddress, byte registerAddress, byte data) {
  79.   Wire.beginTransmission(deviceAddress);
  80.   Wire.write(registerAddress);
  81.   Wire.write(data);
  82.   Wire.endTransmission();
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement