[BPI-M3] Use the UniPi - 8Relays(MCP23008) Module Demo

OS: BPI-M3 Ubuntu15.10 (Kernel3.4) Version: 1.0 HDMI UniPi Introduction: http://unipi.technology/

Reference http://unipi.technology/wp-content/uploads/Schéma-soustavy.png


Step 1: Download WiringPI

$ git clone https://github.com/BPI-SINOVOIP/BPI-WiringPi.git11 -b BPI_M3 $ cd BPI-WiringPi $ chmod +x ./build $ sudo ./build

Step 2:Check the 8Relays(MCP23008) device to see if it’s on track.(address 0x20) (I2c port number for M3: 2 )

sudo su i2cdetect -y 2

.0 1 2 3 4 5 6 7 8 9 a b c d e f 00: – -- – -- – -- – -- – -- – -- – 10: – -- – -- – -- – -- 18 – -- – -- – -- – 20: 20 – -- – -- – -- – -- – -- – -- – -- – 30: – -- – -- – -- – -- – -- – -- – -- – -- 40: – -- – -- – -- – -- – -- – -- – -- – -- 50: 50 – -- – -- – -- 57 – -- – -- – -- – -- 60: – -- – -- – -- – -- 68 – -- – -- – -- 6f 70: – -- – -- – -- – --

Step 3 : Copy smaple code to mcp23008.c file

$ sudo vi mcp23008.c

Step 4 : Compile mcp23008.c

$ gcc -o mcp23008 mcp23008.c -l wiringPi

Step 5: Run mcp23008

$ sudo ./mcp23008


Video Demo:


Sample Code:

#include <stdio.h>
#include <wiringPi.h>
#include <mcp23008.h>

#define PIN_BASE 65
#define ADDRESS 0x20

int main(int argc, char *argv[])
{
  int s;
  int count=65;
  wiringPiSetup();
  mcp23008Setup(PIN_BASE, ADDRESS);


  for(int i=0 ; i<8 ;i++){
   pinMode(count , OUTPUT); 
   count=(count+1);
   }


  while(1){
    s = digitalRead(PIN_BASE); 
    printf("Status switch is %d\n", s);

    count=PIN_BASE;
    for(int i=0 ; i<8 ;i++){
      digitalWrite(count , 1);
      count=count+1;
    }
    delay(1000);

    s = digitalRead(PIN_BASE); 
    printf("Status switch is %d\n", s);
 
    count=PIN_BASE;
    for(int i=0 ; i<8 ;i++){
      digitalWrite(count , 0);
      count=count+1;
    }
    delay(1000);

  }

  return 0;
}