Introduction: Application of MCP-23008 Using Relay Interface (I2C):

Hello

Good Greetings..!!

I (Somanshu Choudhary) on the behalf of Dcube tech ventures going to control Relays via I2C protocol using Arduino nano and MCP23008.

Step 1: Overview

  • The MCP23X08 device provides 8-bit, general purpose, parallel I/O expansion for I2C bus or SPI applications.
  • The MCP23X08 consists of multiple 8-bit configuration registers for input, output and polarity selection. The system master can enable the I/Os as either inputs or outputs by writing the I/O configuration bits. The data for each input or output is kept in the corresponding Input or Output register. The polarity of the Input Port register can be inverted with the Polarity Inversion register. All registers can be read by the system master.
  • DATASHEET LINK : http://ww1.microchip.com/downloads/en/DeviceDoc/21...

Step 2: What You Need / Links

Step 3: Circuit Diagram

Step 4: Programming - I

  • In this code, I use Function Programming Paradigm
  • I used different tabs for functions definition and function calling

CODE UNDER TAB q :

// Simple function calling code

#include
void setup()

{

// I2C address of the MCP23008

#define MCP_ADDR 0x20

// Join I2C Bus as master

Wire.begin();

// Start serial communication and set baud rate

Serial.begin(9600);

// Begin transmission with given device on I2C bus

Wire.beginTransmission(MCP_ADDR);

// Select IODIR – I/O DIRECTION REGISTER register

Wire.write(0x00);

// Select required operation (output)

Wire.write(0x00);

// Select CONFIGURATION register

Wire.write(0x05);

// Select required operation

Wire.write(0x0E);

// end transmission

Wire.endTransmission();

}

void loop()

{

a1_on();

delay(1000);

a1_off();

delay(1000);

a2_on();

delay(1000);

a2_off();

delay(1000);

a3_on();

delay(1000);

a3_off();

delay(1000);

a4_on();

delay(1000);

a4_off();

delay(1000);

a5_on();

delay(1000);

a5_off();

delay(1000);

a6_on();

delay(1000);

a6_off();

delay(1000);

a7_on();

delay(1000);

a7_off();

delay(1000);

a8_on();

delay(1000);

a8_off();

}

CODE UNDER TAB q1 :

// This code is to on and off relay 1 on board

void a1_on()
{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x01);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

void a1_off()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x00);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

CODE UNDER TAB q2 :

// This code is to on and off relay 2 on board

void a2_on()
{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x02);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

void a2_off()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x00);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

CODE UNDER TAB q3:
// This code is to on and off relay 3 on board

void a3_on()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x04);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

void a3_off()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x00);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

Step 5: Programming - II

CODE UNDER TAB q4:

// This code is to on and off relay 4 on board

void a4_on()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x08);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

void a4_off()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x00);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

CODE UNDER TAB q5:

// This code is to on and off relay 5 on board

void a5_on()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x10);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

void a5_off()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x00);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

CODE UNDER TAB q6:
// This code is to on and off relay 6 on board

void a6_on()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x20);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

void a6_off()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x00);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

CODE UNDER TAB q7:
// This code is to on and off relay 7 on board

void a7_on()
{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x40);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

void a7_off()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x00);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

CODE UNDER TAB q8:
// This code is to on and off relay 8 on board

void a8_on()
{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x80);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

void a8_off()

{

// Begin transmission

Wire.beginTransmission(MCP_ADDR);

Wire.write(0x09);

Wire.write(0x00);

delay(1800);

Wire.requestFrom(MCP_ADDR, 1 );

int GPIO = Wire.read();

Wire.endTransmission();

// Output to the screen

Serial.print("GPIO value :");

Serial.println(GPIO,BIN);

}

Step 6: Video

For further quires Feel free to visit our site:

www.dcubetechnologies.com

Comments